Aditya
Aditya

Reputation: 1261

Pass tag Key and Value for resource name in AWS Cloudformation

I have my cloudformation json template which has the resource(VPC to be specific) Tags mapped something like this:

"Tags" : [ {"Key" : "Name", "Value" : "nameofresource"} ]
Now I am running this from a shell script with the command

aws cloudformation create-stack --stack-name stackname --template-body file://template.json

I want to pass the name from the parameter. I tried it using

--parameters ParameterKey=Name,ParameterValue=somevalue

after my template. But it throws an error. Also I tried replacing the "Value" in the JSON with "Ref" : "paramkeyvalue" and passed it as required from the cli. How do I pass the name from parameters?

Upvotes: 1

Views: 3103

Answers (2)

Sudharsan Sivasankaran
Sudharsan Sivasankaran

Reputation: 5887

Command:

  aws cloudformation create-stack --stack-name "MyVPC" --template-body file://test.yaml --parameters ParameterKey=Name,ParameterValue="MyVPC"

test.yaml

{
    "Parameters": {
        "Name": {
            "Type": "String",
            "Default": "MyName"
        }
    },
    "Resources": {
        "myVPC": {
            "Type": "AWS::EC2::VPC",
            "Properties": {
                "CidrBlock": "10.0.0.0/16",
                "EnableDnsSupport": "false",
                "EnableDnsHostnames": "false",
                "InstanceTenancy": "default",
                "Tags": [{
                    "Key": "Name",
                    "Value": {
                        "Ref": "Name"
                    }
                }]
            }
        }
    }
}

Upvotes: 3

Mohan Shanmugam
Mohan Shanmugam

Reputation: 690

Replace the parameters Key and value name as ParameterKey and ParameterValue

--parameters ParameterKey=Name,ParameterValue=somevalue

"Tags" : [ {"ParameterKey" : "Name", "ParameterValue" : "somevalue"} ]

Upvotes: 1

Related Questions