0

I want to tag ebs volumes attached with ec2 instances using cloud formation template. I am able to create tags on new ec2 instances and ebs volumes.

I also tried user data method in cloud formation. but that didn't work.. Can someone please help to fix this issue.

Please let me know what i am missing.

I tried below url as well to fix the issue, but it didn't work.

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

I tried below codes :-

"MyInstance" : {
    "Type" : "AWS::EC2::Instance",
    "Properties" : {
        "SecurityGroups" : "MySecurityGroup",
        "AvailabilityZone" : "us-east-1a",
        "ImageId" : "ami-20b65349",
        "Volumes" : [
            { "VolumeId" : "MyEBS",
                       "Device" : "/dev/sdk" }
        ],
        "Tags" : [
            {
                "Key" : "Stage",
                "Value" : "QA"
            }
         ]
       }
    },

and also use userdata as well....

  Tags:
    - Key: Application
      Value: !Ref 'AWS::StackId'
    - Key: Name
      Value: MNPMGMT-SPMASTER
  NetworkInterfaces:
    - NetworkInterfaceId: !GetAtt 'MgmtNetworkingStack.Outputs.niSplunkMstrIp'
      DeviceIndex: '0'
  BlockDeviceMappings:
    - DeviceName: /dev/sdb
      Ebs:
        Encrypted: 'true'
        VolumeSize: '250'
  UserData: 
    Fn::Base64: |
      #!/bin/bash
      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}

I want tags will be created on existing ec2 instance and ebs volumes as well, and it should be named with stack name and environment name.

Kindly help me to fix this.

JJIqbal
  • 630
  • 1
  • 8
  • 23
Dharmendra jha
  • 101
  • 2
  • 11
  • Why do you wish to use CloudFormation to tag an existing volume? If the volume was created by another CloudFormation template, then you should modify that template and then UPDATE the stack. If the volume was not created via CloudFormation, then there is no reason to tag it with CloudFormation — just do it yourself via the console. If there is another reason why you wish to do it, please edit your question to add more details. Thank you! – John Rotenstein Jul 09 '19 at 12:28
  • Thank you for your clarification.. can you just let me know how i can create an instance using cloud formation where i want to use tag name as stack name and instance name. is there a way to do this..? – Dharmendra jha Jul 10 '19 at 17:30
  • When CloudFormation deploys resources, it automatically adds some tags, including Stack Name and Stack ID. See: [Resource Tag - AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-resource-tags.html) You can specify tags when you launch an instance, such as a `Name` tag: [AWS::EC2::Instance - AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-tags) – John Rotenstein Jul 11 '19 at 06:19

2 Answers2

0

@John Rotenstein, correct it should have stack name and ID however it is not tagging the vol. Thats what he is trying to do

Devops
  • 3
  • 3
0

Try the below script into your user-data

#!/bin/bash
EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed 's/[a-z]$//'`"
EC2_INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
DATA_VOLUME_ID="`aws ec2 describe-volumes  --filters Name=attachment.device,Values=/dev/sda1 Name=attachment.instance-id,Values=$EC2_INSTANCE_ID --query 'Volumes[*].{ID:VolumeId}' --region $EC2_REGION --output text`"
aws ec2 create-tags --resources $DATA_VOLUME_ID --tags Key=KeyName,Value=ValueName Key=KeyName,Value=ValueName --region ${EC2_REGION}
Ayush Shah
  • 29
  • 4