Upload local artifacts to an S3 bucket with the AWS CLI
For some resource properties that require an Amazon S3 location (a bucket name and file name), you can specify local references instead. For example, you might specify the S3 location of your AWS Lambda function's source code or an Amazon API Gateway REST API's OpenAPI (formerly Swagger) file. Instead of manually uploading the files to an S3 bucket and then adding the location to your template, you can specify local references, called local artifacts, in your template and then use the package CLI command to quickly upload them. A local artifact is a path to a file or folder that the package command uploads to Amazon S3. For example, an artifact can be a local path to your AWS Lambda function's source code or an Amazon API Gateway REST API's OpenAPI file.
If you specify a file, the command directly uploads it to the S3 bucket. After uploading the artifacts, the command returns a copy of your template, replacing references to local artifacts with the S3 location where the command uploaded the artifacts. Then, you can use the returned template to create or update a stack.
If you specify a folder, the command creates a .zip
file for the
folder, and then uploads the .zip
file. If you don't specify a path, the
command creates a .zip
file for the working directory, and uploads it. You
can specify an absolute or relative path, where the relative path is relative to your template's
location.
You can use local artifacts only for resource properties that the package command supports. For more information about this command and a list of the supported resource properties, see the package documentation in the AWS CLI Command Reference.
The following template specifies the local artifact for a Lambda function's source code. The
source code is stored in the user's /home/user/code/lambdafunction
folder.
Original template
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Resources": {
"MyFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "index.handler",
"Runtime": "nodejs18.x",
"CodeUri": "/home/user/code/lambdafunction"
}
}
}
}
The following package command creates a .zip
file
containing the function's source code folder, and then uploads the .zip
file to the root folder of the
bucket.amzn-s3-demo-bucket
package
command
aws cloudformation package --s3-bucket
amzn-s3-demo-bucket
\ --template/path_to_template/template.json
\ --output-template-filepackaged-template.json
\ --output json
The command saves the template that it generates to the path specified by the
--output-template-file
option. The command replaces the artifact with the Amazon S3
location, as shown in the following example:
Resulting template
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Resources": {
"MyFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "index.handler",
"Runtime": "nodejs18.x",
"CodeUri": "s3://amzn-s3-demo-bucket
/<md5 checksum>
"
}
}
}
}