Search code examples
amazon-web-servicesaws-cloudformation

How to output the TAGs of an EC2 instance


I have an EC2 Instance like below;

"Ec2Instance": {
    "Type": "AWS::EC2::Instance",
    "Properties": {
        "ImageId": { "Ref" : "RegionalAmi" },
        "AvailabilityZone": {
            "Ref": "Ec2AZ"
        },
        "InstanceType": { "Ref" : "AWSInstanceType" },
        "KeyName": { "Ref" : "publicKey" },
        "NetworkInterfaces": [
            {
                "NetworkInterfaceId": {
                    "Ref": "Nic0"
                },
                "DeviceIndex": "0"
            },
            {
                "NetworkInterfaceId": {
                    "Ref": "Nic1"
                },
                "DeviceIndex": "1"
            }
        ],
        "Tags" : [
            {   
                "Key": "Name",
                "Value": "Sample-TOPOLOGY"
            },
            {   
                "Key": "Resources",
                "Value": "r0"
            }
        ],
        
    },
    "DependsOn": [
        "Nic0",
        "Nic1"
    ]
},

I want in output section to print the tags used for this EC2 instance. I see that following way, we can retrieve some of the properties of EC2 instance.

"Ec2InstanceName": {
    "Description": "Public name of EC2",
    "Value" : { "Fn::GetAtt" : [ "vSRXInstance", "PublicDnsName" ] }
},

But not Tags. If I use following, i get some error like "Template error: resource Ec2Instance does not support attribute type Tags in Fn::GetAtt"

"Ec2InstanceTag": {
    "Description": "Tags of vSRX",
    "Value" : { "Fn::GetAtt" : [ "vSRXInstance", "Tags" ] }
},

Any help appreciable.


Solution

  • That's correct. Outputting tags is not supported. You would have to develop your own custom resource to get the tags and return them.

    Otherwise, you have to hardcode them in Outputs or use as Parameters which you can then reference in the Outputs.