Generating and Storing Build Artifacts in Amazon S3
Not every artifact is a package or an image. Application bundles, CloudFormation template packages, AWS Lambda deployment packages and static website files are usually stored in Amazon S3. This lesson explains how to configure CodeBuild and AWS Lambda packaging tools to generate artifacts, how to design an S3 artifact store, and how Lambda deployment packages and layers are built and versioned.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how CodeBuild artifact settings control the name, packaging, path and encryption of S3 artifacts.
- Describe the properties of a well-designed S3 artifact bucket, including versioning, encryption and lifecycle rules.
- Explain how Lambda deployment packages, container images and layers are built and their size limits.
- Describe how aws cloudformation package and sam build and package prepare serverless artifacts.
CodeBuild Artifact Settings
When a CodeBuild project runs outside CodePipeline, its project-level artifact settings decide where the buildspec's artifact files are uploaded. The settings combine with the buildspec as follows. The artifact type is S3, CodePipeline or none. The location is the bucket. The path and namespace type decide the key prefix; a namespace type of BUILD_ID inserts the build ID into the key, so each build writes to a separate location and earlier artifacts are never overwritten. The packaging option uploads files as they are or as a single ZIP. The name can be overridden in the buildspec, for example orders-$CODEBUILD_BUILD_NUMBER.zip, to produce versioned names, which requires enabling semantic versioning of the artifact name in the project.
Artifacts are encrypted with the project's encryption key, which defaults to the AWS managed key for S3 and can be a customer managed KMS key. Artifact encryption can be disabled only for specific uses, such as publishing a static website. When CodeBuild runs inside CodePipeline, the pipeline's artifact store and key are used instead.
Designing an S3 Artifact Bucket
An artifact bucket stores the outputs that deployments use, so its configuration affects both security and the ability to roll back. The recommended properties are listed below.
- Versioning enabled, so that an object overwritten by a later build can still be retrieved by version ID. CodePipeline S3 sources and CodeDeploy revisions in S3 record version IDs or ETags.
- Default encryption with SSE-KMS and a customer managed key when the artifacts are sensitive or must be used across accounts, with S3 Bucket Keys enabled to reduce KMS request cost.
- S3 Block Public Access enabled and a bucket policy that denies requests not using TLS (
aws:SecureTransportfalse). - Lifecycle rules that expire noncurrent versions and old build outputs after a retention period, and that abort incomplete multipart uploads, to control cost.
- S3 Object Lock in compliance or governance mode when regulations require that release artifacts cannot be deleted or altered for a period.
- Cross-Region replication when deployments in a recovery Region must not depend on the primary Region.
Lambda Deployment Packages
AWS Lambda accepts code in two package types, and the build process differs for each.
ZIP archives
A ZIP package contains the function code and its dependencies. It can be uploaded directly up to 50 MB (zipped), or from S3, and the unzipped size of the function and all its layers must not exceed 250 MB. Dependencies with native extensions must be built for the Lambda runtime's operating system and architecture (x86_64 or arm64), which is why builds commonly run in a container image that matches the Lambda environment, such as a CodeBuild Amazon Linux image or the AWS SAM build container.
Container images
A container image up to 10 GB is pushed to Amazon ECR and referenced by the function. Images must implement the Lambda Runtime API, usually by starting from an AWS base image or adding the runtime interface client. Container images suit large dependencies, such as machine learning libraries, and teams that already build container images. The function must be updated to reference a new image digest for new code to take effect, because Lambda resolves the tag to a digest when the function is updated.
Layers
A layer is a ZIP archive of libraries, a custom runtime or configuration that several functions share. A function can use up to five layers, and the layer content is extracted under /opt. Layers are versioned and immutable: publishing a layer creates a new version, and functions keep using the version they reference until updated. Layers apply only to ZIP functions; container image functions include dependencies in the image.
50MB
Maximum zipped package uploaded directly
250MB
Maximum unzipped size including layers
10GB
Maximum container image size
5layers
Maximum layers per function
Lambda package limits
Packaging Serverless Applications
Templates that define Lambda functions refer to local code directories during development, but CloudFormation needs the code in S3 or ECR. Two commands bridge the gap. aws cloudformation package uploads local artifacts referenced in a template, such as a function's CodeUri or a nested stack's template, to an S3 bucket and writes a new template with the S3 locations. The AWS SAM CLI provides sam build, which resolves dependencies and builds each function, optionally in a container that matches the Lambda runtime with --use-container, and sam package or sam deploy, which upload the build output to S3 or ECR.
# In a CodeBuild build phase: build the functions and produce a deployable template
sam build --use-container
sam package --s3-bucket "$ARTIFACT_BUCKET" --s3-prefix orders --output-template-file packaged.yaml
# packaged.yaml is the output artifact that a CloudFormation deploy action usesThe packaged template becomes the pipeline output artifact. A CloudFormation action then deploys it with the CAPABILITY_AUTO_EXPAND capability, because the SAM transform is a macro.
Versioned Artifacts for Rollback
A team deploys an application bundle to EC2 instances with CodeDeploy from a CodeBuild project that runs outside a pipeline. Each build overwrites s3://acme-builds/orders/app.zip. After a faulty release, the team cannot find the previous bundle.
The project's artifact namespace type is changed to BUILD_ID so that each build writes to its own prefix, and the artifact name includes the build number. Bucket versioning is enabled as a second safeguard, and a lifecycle rule expires noncurrent versions and build prefixes older than 90 days. CodeDeploy deployments now reference a unique object, and redeploying an earlier revision restores the earlier code.
A Python Lambda function deployed from a Windows build machine fails at import time with an error about a missing native library. How should the build be changed?
Show answer
Build the dependencies in an environment that matches the Lambda runtime's operating system and architecture, for example with sam build --use-container or a CodeBuild Amazon Linux image, so native extensions are compiled for Lambda.
Exam Tips
Remember for the exam
- Use the BUILD_ID namespace type or versioned artifact names in CodeBuild so builds never overwrite earlier artifacts; enable S3 versioning on artifact buckets.
- Lambda limits: 50 MB zipped direct upload, 250 MB unzipped including layers, 10 GB container images, five layers per function.
- aws cloudformation package and sam package upload local code to S3 and output a template with S3 locations; deploying SAM templates needs CAPABILITY_AUTO_EXPAND.
- Build native dependencies for the Lambda runtime's OS and architecture (sam build --use-container).