简体   繁体   中英

tag ec2 instance with stackname

I want to tag the ec2 instances with key- somekey and otherkey- with the value as stackname. is this going to do the trick with this python code??

import os, sys, pprint #standard library imports
import yaml, boto3 #pip library imports
import lib.aws as aws
import config.hooks as hooks


def generate(source_data):
    return yaml.dump(generate_map(source_data), default_flow_style=False)

def generate_resource(ami, source_data):
    resource = {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "ImageId": ami["ImageId"],
            "InstanceType": ami["InstanceType"],
            "PrivateIpAddress": ami["PrivateIpAddress"],
            "KeyName": ami["KeyName"],
            "SubnetId": { "Ref": "SubnetId" },
            "SecurityGroupIds": { "Ref":  "SecurityGroupId" }, 
            "Tags": [
                { "Key": "Name", "Value": ami["Name"] },
                { "Key": "BootUpDependsOn", "Value": ami["BootUpDependsOn"]},
                { "Key": "somekey", "Value": "Fn::Sub": "${AWS::StackName}},
                { "Key": "otherkey", "Value": "Fn::Sub": "${AWS::StackName}},
                { "Key": "WaitTimeAfterBootUp", "Value": ami["WaitTimeAfterBootUp"]}
            ]
        }
    }

CloudFormation automatically tags resources with the following tags:

  • aws:cloudformation:logical-id
  • aws:cloudformation:stack-id
  • aws:cloudformation:stack-name

Thus maybe instead of duplicating the tag with AWS::StackName you could use those automatically provided.

Update

There is quotation mark missing in:

   { "Key": "otherkey", "Value": "Fn::Sub": "${AWS::StackName}},

it should be:

   { "Key": "otherkey", "Value": "Fn::Sub": "${AWS::StackName}"},

it maybe also should be:

   { "Key": "otherkey", "Value": {"Fn::Sub": "${AWS::StackName}"}},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM