Amazon-CloudFormation [SOLVED]: How to set tags of the root volume of EC2 instance via CloudFormation

Amazon-CloudFormation [SOLVED]: How to set tags of the root volume of EC2 instance via CloudFormation

Home Forums Amazon Web Services Amazon CloudFormation Amazon-CloudFormation [SOLVED]: How to set tags of the root volume of EC2 instance via CloudFormation

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #233150

    Cloudy Point
    Keymaster

    QuestionQuestion

    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

    #233151

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    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}
    

    Source: https://stackoverflow.com/questions/47499722/how-to-set-tags-of-the-root-volume-of-ec2-instance-via-cloudformation
    Author: Rodrigo M
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.