Recreating CloudFormation Build Bucket - amazon-web-services

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.

Related

AWS Cloudformation nested stack deployment template path

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

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 do I destroy a aws SAM Local lambda?

Follow the read for an example it says:
# AWS SAM Hello World Example #
A simple AWS SAM template that specifies a single Lambda function.
## Usage ##
To create and deploy the SAM Hello World example, first ensure that you've met the requirements described in the [root README](../../README.md). Then
follow the steps below.
### Test your application locally ###
Use [SAM Local](https://github.com/awslabs/aws-sam-local) to run your Lambda function locally:
sam local invoke "HelloWorldFunction" -e event.json
### Package artifacts ###
Run the following command, replacing `BUCKET-NAME` with the name of your bucket:
sam package --template-file template.yaml --s3-bucket BUCKET-NAME --output-template-file packaged-template.yaml
This creates a new template file, packaged-template.yaml, that you will use to deploy your serverless application.
### Deploy to AWS CloudFormation ###
Run the following command, replacing `MY-NEW-STACK` with a name for your CloudFormation stack.
sam deploy --template-file packaged-template.yaml --stack-name MY-NEW-STACK --capabilities CAPABILITY_IAM
This uploads your template to an S3 bucket and deploys the specified resources using AWS CloudFormation.
Now what is the sam local command to delete the whole stack including s3 bucket and CF stack?
Edit for 2022:
Sam cli now has a delete command can see official docs here. Should be able run like this now:
sam delete --stack-name MY-NEW-STACK
Thanks #jesusnoseq for pointer to update 🍻.
Legacy 2018 Answer
There is currently no sam command to delete all of these resources. You would just use the relevant aws cli commands which sam-cli is just a wrapper around anyways. Example to delete you CFN stack.
aws cloudformation delete-stack --stack-name MY-NEW-STACK

Importing AWS information from CloudFormation to CodeBuild

I have a pipeline in AWS with Codestar, CodeBuild, CloudFormation, etc.
I am trying to figure out how to get information from the CloudFormation step returned to the CodeBuild step. Let me break it down:
I have a buildspec.yml for CodeBuild
# buildspec.yml
...
phases:
...
build:
commands:
- aws cloudformation package --region $REGION --template template.yml --s3-bucket $S3_BUCKET --output-template $OUTPUT_TEMPLATE
The above kicks off a CloudFormation build using our template.yml
# template.yml
...
S3BucketAssets:
Type: AWS::S3::Bucket
...
At this point, it creates a unique name for an S3 bucket. Awesome. Now, for step 2 in my buildspec.yml for CodeBuild, I want to push items to the S3 bucket just created in the CloudFormation template. BUT, I don't know how to get the dynamically created name of the S3 bucket from the CloudFormation template. I want something similar to:
# buildspec.yml
...
phases:
...
build:
commands:
# this is the same step as shown above
- aws cloudformation package --region $REGION --template template.yml --s3-bucket $S3_BUCKET --output-template $OUTPUT_TEMPLATE
# this is the new step
- aws s3 sync dist_files/ s3://{NAME_OF_THE_NEW_S3_BUCKET}
How can I accomplish getting the dynamically named S3 bucket so that I can push to it?
I am aware that within a CloudFormation template, you can reference the S3 bucket name with something like !GetAtt [ClientWebAssets, WebsiteURL]. But, I do not know how to get that information out of the cloudformation template and back into the codebuild template.
You could move to using CodePipeline. Stage one would be deploying the application via cloudformation with an output artifact being the stack creation output
http://docs.aws.amazon.com/codepipeline/latest/userguide/reference-pipeline-structure.html#action-requirements