0
votes

I am using Auto Scaling with a Load Balancer and have attached 2 EBS volumes.

Now whenever an instance is terminated it stores the snapshot of the EBS volumes.

I have gone through several links but cannot find how to retrieve/mount the EBS volume when a Launch Configuration launches a new instance.

Can I can get any reference or PowerShell script to identify a volume via tag name from the volume list and mount it when the instance is initiating?

2
Rather than asking somebody to write code for you, it is best to show what you have and to ask a question for any problems you are experiencing. For tips on asking a good question, see: How do I ask a good question? - John Rotenstein

2 Answers

4
votes

There is no automatic facility to mount an existing EBS snapshot or volume when Auto Scaling launches an instance.

Best practice for Auto Scaling is to store data off-instance, such as in Amazon S3 or Amazon EFS. This way, the data is accessible to all instances simultaneously and can be used by new instances that are launched.

There is also no automatic facility to create an EBS snapshot when an Auto Scaling instance is terminated. Rather, there is the option to Delete on Termination, which controls whether the EBS volume should be deleted when the instance is terminated. If this option is off, then the EBS volumes will remain after an instance is terminated. You could write some code (eg in a User Data script) that re-attached an EBS volume to a new instance launched by Auto Scaling but this can get messy. (For example: Which instance to attach? What happens if more instances are launched?)

Bottom line: Yes, you could write a script to do this, but it is a poor architectural design.

0
votes

Yes, you can attach (mount) an EBS volume to an EC2 instance using the AWS CLI command line tool. You run this command in the EC2 User Data at instance launch.

Running Commands on Your Linux Instance at Launch

AWS CLI attach-volume

Note: There is a problem with this strategy. The ASG Launch Configuration is for creating new EC2 instances that are identical. This would mean that you would be attempting to attach the same EBS volume to each instance which will fail. You may want to consider using EFS instead.

Amazon Elastic File System

Mount EFS on EC2 using the AWS CLI

Note: Use IAM roles to provide your instances with credentials instead of storing credentials on the EC2 instance.

Once you have configured your "master" EC2 instance create a new AMI for your ASG launch configuration.

When mounted on Amazon EC2 instances, an Amazon EFS file system provides a standard file system interface and file system access semantics, allowing you to seamlessly integrate Amazon EFS with your existing applications and tools. Multiple Amazon EC2 instances can access an Amazon EFS file system at the same time, allowing Amazon EFS to provide a common data source for workloads and applications running on more than one Amazon EC2 instance.