2
votes

I need to setup an EC2 instance and attach an existing EBS Volume via CloudFormation.

Currently there is an AutoScalingGroup for the EC2 instance with minsize=1 and maxsize=1 in place. I am not able to attach the EBS Volume to the LaunchConfiguration for the AutoScalingGroup. The reason is I can't find an option similar to AWS::EC2::VolumeAttachment.

Questions:

  • Does the current setup with min and max 1 make any sense? The goal is to automatically recreate the VM on failure. Is there a better way?
  • Can I attach an EBS Volume to the instance with CloudFormation with the current AutoScaling setup?
3
Are you referring to a new (empty) Amazon EBS volume that you would like to attach, or are you referring to an additional EBS volume that you wish to re-attach to a new instance that is launched?John Rotenstein
Reffering to an already existing, additional EBS Volume. So I am looking for AWS::EC2::VolumeAttachment for a LaunchConfiguration.Ahuevo

3 Answers

4
votes

It appears you wish to attach an existing Amazon EBS volume to the Amazon EC2 instance that is launched by Auto Scaling.

It is a common practice to use Auto Scaling with min/max of 1. This ensures that an instance is running even if it fails or if an Availability Zone fails.

However, I don't think you will find an Auto Scaling or CloudFormation option to attach an existing Amazon EBS volume. An Auto Scaling group is used to launch lots of identical instances for horizontal scaling. They will have their own EBS volumes, but they would not "share" an EBS volume.

Rather than attempting to attach it via an Auto Scaling / CloudFormation configuration, you could use a User Data script that runs when the instance is launched. It could use the AWS CLI to attach a volume to itself. The script would need to retrieve the instance's InstanceId via metadata, then pass that to the attachment call.

3
votes

I did not find volume attachment option for launch config so regarding your second question, I think, it would be much easier to do using userdata.

In userdata, you can extract instanceid by using below command:

$instanceId = Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id 

Then you can use AWS CLI command to attach the EBS volume. Example:

aws ec2 attach-volume --volume-id vol-1234567890abcdef0 --instance-id i-01474ef662b89480 --device /dev/sdf

Regarding debug log, you can always setup userdata execution log or redirect the output of the command to a file for debug info.

1
votes

You say "I am not able to attach the EBS Volume to the LaunchConfiguration" but don't say why. So I'm unsure if you're saying you're unable to get the EC2 instances to provision and mount an EBS Volume or if you're saying you tried to create a LaunchConfiguration for the AutoScalingGroup already and it didn't work. I assume the former but if it was the latter please provide an error message or some debug logs so we can troubleshoot it for that is the way to accomplish your goals. Also, you should know that it is preferred to use the LaunchTemplate over the LaunchConfiguration when possible.

You need to create a CloudFormation entry for a LaunchTemplate that specifies a BlockDeviceMapping. Within which you will reference the ARN of your EBS volume. Which you presumably also created with CloudFormation.