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.