Cloudformation CLI Parameters Using Deploy Command - amazon-web-services

I'm having an issue getting the hang of using cli parameters with cloudformation deploy. I'm trying to pass in the name for the S3 bucket that I want to create, and the cli is complaining when I use --parameters to do this:
aws cloudformation deploy --template-file ../infrastructure.yml --stack-name stripe-python --parameters ParameterKey=S3BucketNameParameter,ParameterValue=lambda-artifacts-948d01bc80800b36
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: argument subcommand: Invalid choice, valid choices are:
push | register
deregister | install
Obviously, omitting the parameter doesn't work either:
aws cloudformation deploy --template-file ../infrastructure.yml --stack-name stripe-python
An error occurred (ValidationError) when calling the CreateChangeSet operation: Parameters: [S3BucketNameParameter] must have values
When I look at the documentation for cloudformation deploy, it seems to not support --parameters but instead --parameter-overrides, which I've also tried with no success:
aws cloudformation deploy --template-file ../infrastructure.yml --stack-name stripe-python --parameter-overrides S3BucketNameParameter=lambda-artifacts-948d01bc80800b36
An error occurred (ValidationError) when calling the CreateChangeSet operation: Parameters: [S3BucketNameParameter] must have values
So, I'm kind of stumped here. Here's the template file's contents:
cat ../infrastructure.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Lambda application that calls the Stripe API to tokenize and charge credit cards
Parameters:
S3BucketNameParameter:
Type: String
Description: Bucket name for deployment artifacts
Resources:
S3Bucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
BucketName: !Ref S3BucketNameParameter
Any suggestions on the correct approach here?

This works for me:
aws cloudformation deploy --template-file infrastructure.yml --stack-name stripe-python --parameter-overrides S3BucketNameParameter=lambda-artifacts-948d01bc80800b36
It may come down to awscli version (ie check the version you are running and the doc for that)
aws --version
aws-cli/2.0.44 Python/3.8.5 Darwin/18.7.0 source/x86_64

Related

Lambda code does not get zipped during `package` command when using substacks

I am using CloudFormation for creating lambda functions. The lambda functions are stored in a separate file and then recreated using aws cloudformation package command. This works fine and the stack gets deployed successfully:
# Filename: auth/auth.yml
# Lambda JS file: auth/lambda-pre-signup.js
Resources:
## Other resources here
MyPreSignupLambda:
Type: AWS::Lambda::Function
Properties:
Architectures:
- arm64
Code: 'lambda-pre-signup.js'
Handler: 'lambda-pre-signup.handler'
Runtime: nodejs16.x
PackageType: Zip
Role: !GetAtt MyRole.Arn
Command:
aws cloudformation package --template-file auth.yml --s3-bucket my-bucket --output-template-file generated-auth.yml
aws cloudformation deploy --template-file generated-auth.yml --stack-name test-stack --capabilities CAPABILITY_IAM
However, when I create a root stack template and reference lambda, I get an error:
Resource handler returned message: "Could not unzip uploaded file. Please check your file, then try to upload again. (Service: Lambda, Status Code: 400, Request ID: xxxxx)"
When I check the S3 bucket for the uploaded file, the source code is there but it is not zipped (I can download and directly view the code without needing to unzip it).
Here is my current CF template for root stack:
# Filename: root.yml
Resources:
MyAuth:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: ./auth/auth.yml
Command:
aws cloudformation package --template-file root.yml --s3-bucket my-bucket --output-template-file generated-root.yml
aws cloudformation deploy --template-file generated-root.yml --stack-name test-root-stack --capabilities CAPABILITY_IAM
Is there some option in the package command to make sure that the uploaded lambda code is zipped?
EDIT: Wrote a wrong argument

Output AWS CLI YAML output to console

I am using the AWS CLI and CloudFormation to create a new S3 bucket.
Here is my yaml file:
AWSTemplateFormatVersion: '2010-09-09'
Description: Creates an S3 bucket
Parameters:
BucketName:
Description: Name of the Bucket
Type: String
Resources:
ArtifactBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Outputs:
ArtifactBucket:
Value: !Sub ${BucketName}
BucketArn:
Value: !GetAtt ArtifactBucket.Arn
Description: Arn of the new bucket
I run it with the following cli command in a terminal window:
aws cloudformation deploy --stack-name brendan-s3 \
--template-file ComposeEveryApp/create-s3-bucket.yaml \
--profile compose-staging \
--parameter-overrides BucketName=brendan
Everything works fine. Here is the new bucket displayed in the AWS console:
I'd like to display the Arn of the new bucket (as shown above) in the terminal window. How do I do that?
The command aws cloudformation deploy is only instructing the CloudFormation service to start the deployment and not actually waiting for the deployment to finish. Hence there is no link between the Outputs section and the return value of the command you're executing on the CLI.
If you want the Outputs of a cloudformation stack, you'll have to use the describe-stacks command, and you'll need to combine it with a client side filter using --query if you want to only output that specific value.
You can find more info on this SO question.
You can use describe-stacks command. One of its return values will be outputs of your stack.

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
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
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
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!
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.
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
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
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

AWS SAM: An error occurred (ValidationError) when calling the CreateChangeSet operation: Parameters: [IdentityNameParameter] must have values

I want to get started with AWS SAM and I encounter this issue when trying to deploy to the AWS.
I am trying to deploy a 'Hello World!'-application that can be found here.
This is the error I encounter:
$ sam package --s3-bucket dolphin-code --s3-prefix prod --output-template-file packaged.yaml --region eu-central-1
Uploading to prod/de65208b144ad296cfdc39666a47ad1c 34671 / 34671.0 (100.00%)
Successfully packaged artifacts and wrote output template to file packaged.yaml.
Execute the following command to deploy the packaged template
sam deploy --template-file /builds/gitlab/dophin/apis/hello-world/packaged.yaml --stack-name
$ sam deploy --template-file ./packaged.yaml --stack-name prod --capabilities CAPABILITY_IAM --region eu-central-1
Deploying with following values
===============================
Stack name : prod
Region : eu-central-1
Confirm changeset : False
Deployment s3 bucket : None
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {}
Initiating deployment
=====================
Error: Failed to create changeset for the stack: prod, An error occurred (ValidationError) when
calling the CreateChangeSet operation: Parameters: [IdentityNameParameter] must have values
ERROR: Job failed: exit code 1
For me, that seems to be an error in the AWS CLI and not in SAM directly, right?
Can anyone help me? Thanks in advance!
It seems that you are using a parameter in your sam template called "IdentityNameParameter", and it doesn't have a default value, thus Sam expects you to provide a value for it.
Either you set the value when you call your sam deploy using the flag --parameters-overrides
$ sam deploy --template-file ./packaged.yaml --stack-name prod --capabilities CAPABILITY_IAM --region eu-central-1 --parameter-overrides IdentityNameParameter=xyz
or give it a default value in your SAM template
Parameters:
IdentityNameParameter:
Type: String
Default:"xyz"
You can read more about the sam deploy command here https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-deploy.html

AWS sam deploy return error invalid choice

I'm following the instruction here to use AWS CodeDeploy to push code from GitHub to AWS.
I run into this error:
$ sam deploy -template-file packaged.yaml –stack-name mySafeDeployStack –capabilities CAPABILITY_IAM
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: argument subcommand: Invalid choice, valid choices are:
push | register
deregister | install
uninstall
I have previously run this command successfully:
$ sam package --template-file template.yaml --s3-bucket my-bucket --output-template-file packaged.yaml
Uploading to ... (100.00%)
Successfully packaged artifacts and wrote output template to file packaged.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file .../packaged.yaml --stack-name <YOUR STACK NAME>
$ sam --version
SAM CLI, version 0.6.0
I've tried the recommended command:
aws cloudformation deploy ...
but it returns the same error.
It looks like you're using single dashes for the flags when they require two. The sam package command succeeded since you used two dashes for it.
This should work:
sam deploy --template-file packaged.yaml --stack-name mySafeDeployStack --capabilities CAPABILITY_IAM