Aws-sam-cli: sam deploy should accept parameter file just like aws cloudformation *-stack

Created on 18 Jun 2020  路  8Comments  路  Source: aws/aws-sam-cli

Description:

I would like to store configuration is version control and make deployment steps immutable. Instead of changing parameter overrides on the command line I would like to provide template parameters from a file.

There is an idiom for this in aws cloudformation create-stack --parameters file://.

Steps to reproduce the issue:

  1. sam deploy --parameter-overrides file://params.json

Observed result:

Error: Invalid value for '--parameter-overrides':  file://params.json is not in valid format. It must look something like 'ParameterKey=KeyPairName,ParameterValue=MyKey ParameterKey=InstanceType,ParameterValue=t1.micro' or 'KeyPairName=MyKey InstanceType=t1.micro'

Expected result:

    Deploying with following values
    ===============================
    Stack name                 : ...
    Region                     : ...
    Confirm changeset          : ...
    Deployment s3 bucket       : ...
    Capabilities               : ...
    Parameter overrides        : {'MyParamOverride': 'MyParamValue'}
typfeature

Most helpful comment

We use a similar pattern to @rojomisin but use a standard environment variable file format.

.sam-params

DBUsername=admin
DBPassword=password123
Subnets=subnet-12345
VpcId=vpc-1234

Then in our Makefile, we cat that file and pass them in as --parameter-overrides

sam deploy --parameter-overrides $(shell cat .sam-params)

All 8 comments

Why not use the samconfig.toml file? https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html

I am trying to set a parameter that is specified in the template. There is no hint in the documentation that this should be possible but following your suggestion it tried the following:

Parameter defined in the template:

Parameters:
  AllowedSrcIp:
    Type: "String"
    Description: "IP address of the client used for testing. If in doubt, deploy the code and the output will guide you"
    Default: "192.168.0.1"

Updated samconfig.toml with the parameters directly:

version = 0.1
[default]
[default.deploy]
[default.deploy.parameters]
stack_name = "api-unsecured-sam"
s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-10ziytzq60hp8"
s3_prefix = "api-unsecured-sam"
region = "us-west-2"
confirm_changeset = false
capabilities = "CAPABILITY_IAM"
AllowedSrcIp = "205.251.233.178"

The deploy works but doesn't honor the new parameter. Looking at the structure of the config I also tried this line:

parameter_overrides = ParameterKey=AllowedSrcIp,ParameterValue=205.251.233.178

Which breaks the deployment entirely:

sam deploy
Usage: sam deploy [OPTIONS]
Try 'sam deploy --help' for help.

Error: Missing option '--stack-name', 'sam deploy --guided' can be used to provide and save needed parameters for future deploys.

And this:

parameter_overrides = "ParameterKey=AllowedSrcIp,ParameterValue=205.251.233.178"

And this:

parameter_overrides = "AllowedSrcIp=205.251.233.178"

both of which do work but are really hard to maintain if you have more than a couple parameters or if your parameters need complex quoting. In fact it turns out that if you were to enter the data interactively as a result of using sam deploy --guided it will create something like this which highlights the issue:

parameter_overrides = "VpcIdParameter=\"vpc-0000111122223333\" VpcAllowedSecurityGroupIdParameter=\"sg-0000111122223333\" VpcEndpointSubnetIdsParameter=\"subnet-0000111122223333 \""

As @rudpot mentioned, the following parameter_overrides value is rather tedious to maintain with more than a couple entries:

parameter_overrides = "VpcIdParameter=\"vpc-0000111122223333\" VpcAllowedSecurityGroupIdParameter=\"sg-0000111122223333\" VpcEndpointSubnetIdsParameter=\"subnet-0000111122223333 \""

+1 for a simpler approach.

work around until a file can be used is to wrap w/ shell or make

...
        --parameter-overrides \
            DBUsername=$(DBUSER) \
            DBPassword=\"$(shell get-db-pass)\" \
            Subnets=$(SUBNETS) \
            VpcId=$(VPC_ID)

load up a config file with k/v pairs and use make's include

ifndef CONFIG
include config/$(CONFIG)
endif

make sam-deploy CONFIG=dev

We use a similar pattern to @rojomisin but use a standard environment variable file format.

.sam-params

DBUsername=admin
DBPassword=password123
Subnets=subnet-12345
VpcId=vpc-1234

Then in our Makefile, we cat that file and pass them in as --parameter-overrides

sam deploy --parameter-overrides $(shell cat .sam-params)

I've used the above workaround for now, but it would be great to get some consistency with cloudformation in this regard.

2253 proposes parameter overrides should get their own section in samconfig.toml (and should be individually overridden by --parameter-overrides)

The trick by @jveldboom works just as well in a shell-script, except with --parameter-overrides $(cat path-to-file).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kaukas picture kaukas  路  24Comments

TaylorHG picture TaylorHG  路  27Comments

burck1 picture burck1  路  45Comments

matheusmaximo picture matheusmaximo  路  30Comments

oldnerd picture oldnerd  路  25Comments