Mastering Artifact Preparation for AWS Deployment
Prepare application artifacts to be deployed to AWS
Mastering Artifact Preparation for AWS Deployment
Preparing application artifacts is the critical bridge between writing source code and running a functional application in the cloud. In the AWS Certified Developer - Associate (DVA-C02) exam, this topic covers how you package code, manage dependencies, and configure environment-specific settings.
Learning Objectives
After studying this guide, you should be able to:
- Manage dependencies using environment variables, configuration files, and container images.
- Organize file and directory structures optimized for AWS deployment services.
- Define build processes using
buildspec.ymlfor AWS CodeBuild. - Allocate resource requirements (CPU, memory) based on application needs.
- Configure environment-specific parameters using AWS AppConfig and Systems Manager.
Key Terms & Glossary
- Artifact: A compiled or packaged version of your application code (e.g., a .zip file, a JAR, or a Docker image) ready for deployment.
- Buildspec: A collection of build commands and related settings, in YAML format, that AWS CodeBuild uses to run a build.
- Environment Variables: Dynamic-named values that can affect the way running processes will behave on a computer, used to keep secrets and configurations out of code.
- Lambda Layer: A distribution mechanism for libraries, custom runtimes, and other function dependencies to keep deployment packages small.
- AWS AppConfig: A service used to create, manage, and quickly deploy application configurations separately from code.
The "Big Idea"
[!IMPORTANT] The goal of artifact preparation is to create an immutable deployment unit. Once an artifact is built (e.g., in a "Build" stage), it should remain unchanged as it moves through Staging and into Production. Only the environment-specific configurations (provided by services like AppConfig or Parameter Store) should change.
Formula / Concept Box
| Concept | Tool / Method | Purpose |
|---|---|---|
| Static Config | config.json / settings.yaml | Hardcoded parameters that don't change between environments. |
| Dynamic Config | AWS AppConfig / Parameter Store | Values that change (e.g., DB endpoints) without rebuilding code. |
| Secrets | AWS Secrets Manager | Highly sensitive data (API keys, DB passwords) with rotation capabilities. |
| Dependencies | Lambda Layers / Docker Layers | Decoupling heavy libraries from the core application logic. |
Hierarchical Outline
- Dependency Management
- External Libraries: Managed via
package.json(Node),requirements.txt(Python), orpom.xml(Java). - Lambda Layers: Shared dependencies across multiple functions.
- Container Images: Bundling the OS, runtime, and code into a single OCI-compliant image.
- External Libraries: Managed via
- Build Lifecycle (The
buildspec.yml)- Install Phase: Installing runtimes or packages (e.g.,
npm install). - Pre-build: Logging into ECR or running unit tests.
- Build: Compiling code or building Docker images.
- Post-build: Packaging artifacts and uploading to S3.
- Install Phase: Installing runtimes or packages (e.g.,
- Environment Preparation
- AWS AppConfig: Feature flags and runtime configuration updates.
- Resource Sizing: Mapping memory (128MB - 10GB for Lambda) to proportional CPU power.
Visual Anchors
The Buildspec Lifecycle
Resource Allocation Concept
\begin{tikzpicture}[scale=0.8] \draw[thick, ->] (0,0) -- (6,0) node[right] {Memory (MB)}; \draw[thick, ->] (0,0) -- (0,4) node[above] {CPU Power}; \draw[blue, ultra thick] (0.5,0.5) -- (5,3.5); \node at (3,2) [rotate=32, anchor=south] {Linear Scaling}; \filldraw[red] (1,0.8) circle (2pt) node[anchor=north west] {128MB / low CPU}; \filldraw[green!60!black] (4.5,3.1) circle (2pt) node[anchor=south east] {10GB / 6 vCPUs}; \end{tikzpicture}
Definition-Example Pairs
- Definition: Environment Variables are key-value pairs stored in the execution environment rather than hardcoded.
- Real-World Example: Storing
DB_ENDPOINTas an environment variable so the same code connects todev-db.aws.comin development andprod-db.aws.comin production.
- Real-World Example: Storing
- Definition: Artifact Mapping is the process of defining which files from a build should be kept for deployment.
- Real-World Example: In a React project, the
buildspec.ymlspecifies the/builddirectory as the artifact, ignoring the heavynode_modulesfolder.
- Real-World Example: In a React project, the
Worked Examples
Creating a buildspec.yml for a Node.js Lambda
To prepare a Lambda artifact, you must define the phases in a buildspec.yml file located in the root of your source repository.
version: 0.2
phases:
install:
runtime-versions:
nodejs: 18
commands:
- npm install
pre_build:
commands:
- npm test
build:
commands:
- echo "Building the package..."
- zip -r deployment_package.zip .
artifacts:
files:
- deployment_package.zip
discard-paths: yes[!TIP] Use
discard-paths: yesif you want the artifact to be placed in the root of the S3 destination without replicating the source folder structure.
Checkpoint Questions
- Which file is required by AWS CodeBuild to define the build commands and artifact locations?
- If your Lambda function requires a 50MB shared library used by ten other functions, what is the best way to package this library?
- In which phase of the
buildspec.ymlwould you typically run unit tests? - True or False: Increasing the memory allocation for an AWS Lambda function also increases its available CPU capacity.
- Which service would you use to manage "Feature Flags" that can be toggled without redeploying code?
▶Click to see answers
buildspec.yml- As a Lambda Layer.
- The
pre_buildphase. - True. CPU scales linearly with memory in Lambda.
- AWS AppConfig.