AWS Cloudformation nested stack deployment template path - amazon-web-services

I'm trying to deploy nested stack using command
aws cloudformation deploy --stack-name "${STACK_NAME}" --template-file "${S3_ROOT_TEMPLATE}" --parameter-overrides ${PARAMS[#]} --region ${REGION}
But despite the S3_ROOT_TEMPLATE having proper url, I get the error
Invalid template path
https://<s3-bucket-name>.s3.us-east-2.amazonaws.com/sm-domain-templates/main_stack.yaml
Any idea what's wrong with the above?

Even though in the console you have to use S3 path, in the awscli command, it expects the local file path of the root stack file

The following command deploys a template named S3_ROOT_TEMPLATE to a stack named STACK_NAME:
STACK_NAME="cfn-demo"
S3_ROOT_TEMPLATE="cfn-demo.yaml"
REGION="us-east-1"
bucket_name="cfn-demo-bucket"
aws cloudformation deploy --stack-name $STACK_NAME --template-file $S3_ROOT_TEMPLATE --parameter-overrides $PARAMS[#] --region $REGION
If your templates are sized greater than 51,200 bytes, then the name of the S3 bucket where this command uploads your CloudFormation template.
aws cloudformation deploy --stack-name $STACK_NAME --template-file $S3_ROOT_TEMPLATE --parameter-overrides $PARAMS[#] --region $REGION --s3-bucket $bucket_name
For updating the stack, you could upload the template file to the S3 bucket by using copy and then, update the stack by using the S3 object URL as the template source.
aws s3 cp $S3_ROOT_TEMPLATE s3://$bucket_name
aws cloudformation update-stack --stack-name $STACK_NAME --template-url https://$bucket_name.s3.$REGION.amazonaws.com/$S3_ROOT_TEMPLATE

Related

How do I set an AWS Stack name (for a Lambda Layer) in a SAM Template?

This page describes how to set a stack name in some AWS console GUI: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-using-console-create-stack-parameters.html
How do I set these values in the SAM Template .yml files?
I'm specifically doing this on a Stack that is only a Lambda Layer if that matters.
I can see that there is some way to do this via CLI as described here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-cli-creating-stack.html
aws cloudformation create-stack --stack-name myteststack --template-url "ssm-doc://arn:aws:ssm:us-east-1:123456789012:document/documentName"
Is it even possible to set the name in the template?
Unfortunately, it seems like stack name is NOT part of the SAM templates. This is done via the command arguments to deploy the stack.
From the same link: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-cli-creating-stack.html
The following example creates the myteststack stack in an Amazon S3 bucket:
PROMPT> aws cloudformation create-stack \
--stack-name myteststack \
--template-body file:///home/testuser/mytemplate.json \
--parameters ParameterKey=Parm1,ParameterValue=test1 ParameterKey=Parm2,ParameterValue=test2
So when creating the stack, the --stack-name argument is how this is set.
The reason I was confused is because I didn't realize where that command was being issued.

aws cli: invalid template path, running aws cloudformation create

I have a template file in a S3 bucket. I want to create a cloudformation stack with it.
I run:
aws cloudformation create --template https://mybucket-us-east-1.s3.us-east-1.amazonaws.com/template/1.0/the-template.template --stack-name test-stack
Then I get this error:
Invalid template path https://mybucket-us-east-1.s3.us-east-1.amazonaws.com/template/1.0/the-template.template
What is the correct syntax to create/deploy a cloudformation stack from a template file found in a S3 bucket?
jprdanm was right. This command worked, however, I also needed to add --capabilities CAPABILITY_NAMED_IAM at the end of it, so:
aws cloudformation create-stack --template-url https://mybucket-us-east-1.s3.us-east-1.amazonaws.com/template/1.0/the-template.template --stack-name test-stack --capabilities CAPABILITY_NAMED_IAM

Specifying an S3 bucket when deploying a cloudformation template

I'm trying to deploy a cloudformation template using a command that looks as follows:
aws cloudformation deploy \
--stack-name stackname \
--template-file folder/file.yaml \
--s3-bucket bucketname \
--s3-prefix prefix
The error that I receive is:
An error occurred (ValidationError) when calling the CreateChangeSet operation:
S3 error: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
For more information check
http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html
I have checked the link to try and understand what is being asked of me, but it seems to relate to directly using S3, and not using S3 via CloudFormation.
I can't determine a way to do what it is asking using the available parameters of aws cloudformation
The template file that defines a stack must be in an Amazon S3 bucket that is in the same region as the AWS CloudFormation stack being created.
aws cloudformation deploy --stack-name myteststack --template-file folder/file.yaml --s3-bucket bucketname --s3-prefix prefix --region us-east-1
You may replace the parameter and try this.
You need not to pass the region for s3 bucket nor endpoint is required. Yes, if the Bucket and Cloudformation are in a different region in such cases you may face an issue. But I'm sure it will be access denied issue, not something that you have mentioned here.

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

Recreating CloudFormation Build Bucket

Created a CloudFormation stack template named foo.yaml. I validate, package, and deploy a stack from foo.yaml:
aws cloudformation package `
--template-file .\foo.yaml`
--s3-bucket abc123 `
--output-template-file .\foo.pkg.yaml
$parameters = ...
aws cloudformation deploy `
--template-file .\foo.pkg.yaml `
--stack-name foo `
--capabilities CAPABILITY_IAM `
--s3-bucket abc123 `
--parameter-overrides $parameters
I then delete the S3 bucket used in that deployment. Is there a way I can regenerate that bucket from CloudFormation?
P.S. I realize I can create the bucket and populate it manually, but I'm asking if there is anything automatic.
No, there isn't a button or option to re-populate an S3 bucket with objects created when a stack's template was packaged.
You can ONLY do this manually:
Find the CF stack instance's template
Locate the bucket name from the template
Create a new S3 bucket with the same name
Re-package your CF stack template adding the bucket name from step-3 to the aws cloudformation package command:
aws cloudformation package --s3-bucket
Bucket recreated w/artifacts!
If you're asking if the bucket can be created automatically from a CloudFormation command, then it's not really possible. You can create a S3 Bucket Resource ofcourse as a part of a Stack Resource.
I believe it is simpler to create the Bucket manually.