mahendra rathod
mahendra rathod

Reputation: 1638

How to pass parameter as a file in AWS CloudFormation deploy?

I was trying to update the existing CloudFormation stack with the below command.

aws cloudformation deploy

there is no option to pass parameter file with deploy option. we tried to pass parameter file with --parameter-overrides but it's giving the below error.

value passed to --parameter-overrides must be of format Key=Value

the command we try to execute is

aws cloudformation deploy --template-file sg.yml --stack-name Common-SG --parameter-overrides ip.json --no-execute-changeset

is there any way to pass the parameters in file with aws cloudformation deploy

Upvotes: 18

Views: 31027

Answers (7)

MederD
MederD

Reputation: 75

This worked for me in buildspec file:
Structure of parameters.json:

[
  {
    "ParameterKey": "Key1",
    "ParameterValue": "Value1"
  }
]

and then:

post_build:
        commands:
            - echo "Start build..."
            - aws cloudformation deploy --template-file ./template.yaml --parameter-overrides $(jq -r '.[] | [.ParameterKey, .ParameterValue] | "\(.[0])=\(.[1])"' ./parameters/parameters.json) --stack-name ${stackName} --capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND

Upvotes: 0

Christophe Grall
Christophe Grall

Reputation: 317

Passing a parameters stored as JSON in a local file looks like:

aws cloudformation deploy \
    --stack-name demo \
    --template-file test.yml --parameter-overrides file://test.json  \

and the test.json like this.

{
    "Parameters": {
      "BucketName": "myawesometestdemo"
    }
}

test.yml

---
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple S3 Bucket test
Parameters:
  BucketName:
    Type: String
    Description: The name of the S3 Bucket to create

Metadata:

  AWS::CloudFormation::Interface:
    ParameterLabels:
      BucketName:
        default: S3 Bucket Name

Resources:

  S3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName

aws cli version :

aws --version
aws-cli/2.2.35 Python/3. XXX

Upvotes: 18

DataGuru
DataGuru

Reputation: 837

I had the same issue with the files

Initially I had used

[
  {
    "ParameterKey": "EnvironmentStage",
    "ParameterValue": "sandbox"
  }
]

This did not work I got the error that the elements should be of Class 'Str' and not an ordered.Dict

2nd iteration I changed it to as mentioned in the earlier responses that did not work either

finally I have it as

[
  "EnvironmentStage=sandbox"
]

and it works well

Upvotes: 1

Emeruche
Emeruche

Reputation: 51

You can actually pass a file path to Cloudformation deploy --parameter-overrides. The below syntax worked for me:

aws cloudformation deploy \
  --template-file template.yml \
  --stack-name my-stack \
  --parameter-overrides file://path/to_parameter_file.json

where file://path/to_parameter_file.json represents the path to the parameter you want to pass.

Upvotes: 3

sendon1982
sendon1982

Reputation: 11314

You can do like this based on aws doc:

https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/index.html

aws cloudformation deploy --template-file /path_to_template/template.json --stack-name my-new-stack --parameter-overrides Key1=Value1 Key2=Value2

Upvotes: 0

Dean
Dean

Reputation: 374

this might be too late already, but for the sake of future similar issue I found this answer on (https://github.com/aws/serverless-application-model/issues/111)

The command should look like:

aws cloudformation deploy --template-file sg.yml --stack-name Common-SG --parameter-overrides $(cat params.properties) --no-execute-changeset

Now this is not going to be a json file, since "parameter-overrieds" expects a Key=Value pairs!

Upvotes: 2

mahendra rathod
mahendra rathod

Reputation: 1638

workaround for this issue is pass parameters with jq command.

yum install jq

Below is the syntax for the same.

aws cloudformation deploy --template-file sg.yml --stack-name Common-SG --parameter-overrides $(jq -r '.[] | [.ParameterKey, .ParameterValue] | "\(.[0])=\(.[1])"' ip.json) --no-execute-changeset

Upvotes: 3

Related Questions