Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set tags of the root volume of EC2 instance via CloudFormation

Create EC2 instance using CloudFormation, but the name (tags) of root volume is empty. How to set it using CloudFormation?

# ec2-instance.yml (CloudFormation template)
MyInstance:
  Type: "AWS::EC2::Instance"
  Properties:
    ImageId: "ami-da9e2cbc"
    InstanceType: "t2.nano"
    KeyName: !Ref "KeyPair"
    Tags: # This is for EC2 instance (not root volume)
      - Key: "Name"
        Value: "my-instance"

I find "Volumes" and "BlockDeviceMappings" properties but it could not.

the name is empty

like image 861
Yohsuke Inoda Avatar asked Nov 26 '17 18:11

Yohsuke Inoda


People also ask

Does AWS CloudFormation support EC2 tags?

Q: Does AWS CloudFormation support Amazon EC2 tagging? Yes. Amazon EC2 resources that support the tagging feature can also be tagged in an AWS template.

How do you add a tag to volume in AWS?

If you're using the Amazon EC2 console, you can apply tags to resources by using the Tags tab on the relevant resource screen, or you can use the Tags screen. Some resource screens enable you to specify tags for a resource when you create the resource; for example, a tag with a key of Name and a value that you specify.

What is the root volume of the Amazon EC2 instance?

You will see this root volume when you launch the instance. It will have a size of 150 GB. Copyright 2019 Actian Corporation.


1 Answers

CloudFormation does not appear to support this currently. However using an instance user data script, you can do this to tag the root volume:

apt-get -y install unzip
unzip awscli-bundle.zip
./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws    
rm -rf  awscli-bundle awscli-bundle.zip
EC2_INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
EC2_REGION=${EC2_AVAIL_ZONE:0:${#EC2_AVAIL_ZONE} - 1}
ROOT_DISK_ID=$(aws ec2 describe-volumes --filters Name=attachment.instance-id,Values={EC2_INSTANCE_ID} Name=attachment.device,Values=/dev/sda1 --query 'Volumes[*].[VolumeId]' --region=${EC2_REGION} --out \"text\" | cut -f 1)
aws ec2 create-tags --resources $ROOT_DISK_ID --tags Key=Name,Value=\"Root Volume my-instance\" --region ${EC2_REGION}

This script will tag the /dev/sda1 EBS volume with Name=Root Volume my-instance

Note that for my Ubuntu AMI I have to install the AWS tools first. The Amazon Linux AMI has those tools installed.

For CloudFormation, you would use:

# ec2-instance.yml (CloudFormation template)
MyInstance:
  Type: "AWS::EC2::Instance"
  Properties:
    ImageId: "ami-da9e2cbc"
    InstanceType: "t2.nano"
    KeyName: !Ref "KeyPair"
    UserData:
      "Fn::Base64": !Sub |
       #!/bin/bash -x
       apt-get -y install unzip
       unzip awscli-bundle.zip
       ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws    
       rm -rf  awscli-bundle awscli-bundle.zip
       EC2_INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
       EC2_REGION=${EC2_AVAIL_ZONE:0:${#EC2_AVAIL_ZONE} - 1}
       ROOT_DISK_ID=$(aws ec2 describe-volumes --filters Name=attachment.instance-id,Values={EC2_INSTANCE_ID} Name=attachment.device,Values=/dev/sda1 --query 'Volumes[*].[VolumeId]' --region=${EC2_REGION} --out \"text\" | cut -f 1)
       aws ec2 create-tags --resources $ROOT_DISK_ID --tags Key=Name,Value=\"Root Volume my-instance\" --region ${EC2_REGION}
like image 141
Rodrigo Murillo Avatar answered Oct 01 '22 22:10

Rodrigo Murillo