CodeArtifact and Artifact Management
Overview
A pipeline produces and consumes many artifacts: libraries your code depends on, compiled packages, container images and deployment bundles. AWS CodeArtifact is a managed package repository compatible with npm, PyPI, Maven, NuGet, Swift, Ruby and generic formats, so teams stop depending directly on public registries during builds.
Think of a corporate pantry. Instead of every employee shopping at different supermarkets, the pantry stocks vetted items, caches anything first fetched from outside, and keeps a record of what is on the shelf. CodeArtifact is the pantry for packages, and ECR is the pantry for container images.
The exam focuses on repository upstreams, dependency-confusion protection, cross-account sharing, artifact promotion between environments, and when to use S3, ECR or CodeArtifact.
Key Concepts
- CodeArtifact organises packages into a domain (shared encryption and dedup) containing repositories.
- An upstream repository chain lets one repository pull from another and from external connections (npmjs, PyPI, Maven Central); fetched packages are cached.
- Package origin controls (publish and upstream allow/block) defend against dependency confusion attacks.
- Authentication uses a 12-hour authorization token from
aws codeartifact get-authorization-token. - Domain permissions policy and repository policies allow cross-account and Organization-wide sharing.
- ECR stores images with lifecycle policies, scan on push, immutable tags and cross-Region/cross-account replication.
- Promote the same immutable artifact through environments; rebuild only on source change.
- CodePipeline passes artifacts between stages through an S3 artifact store encrypted with KMS (a customer managed key is required for cross-account).
Detailed Notes
Domains, repositories and upstreams
| Concept | Purpose |
|---|---|
| Domain | Container for repositories; one KMS key, one billing unit, deduplicated storage |
| Repository | Holds packages by format; has zero or more upstreams |
| Upstream | Another CodeArtifact repository queried when a package is missing |
| External connection | Link to a public registry (one per repository) |
A typical layout: a company-shared repository connected externally to npmjs, with team repositories listing it as upstream. Developers only talk to the team repository.
aws codeartifact create-domain --domain corp
aws codeartifact create-repository --domain corp --repository shared-npm
aws codeartifact associate-external-connection --domain corp \
--repository shared-npm --external-connection public:npmjs
aws codeartifact login --tool npm --domain corp --repository team-npm
Using it in CodeBuild
phases:
pre_build:
commands:
- aws codeartifact login --tool pip --domain corp --repository team-pypi
- pip install -r requirements.txt
The build role needs codeartifact:GetAuthorizationToken, codeartifact:GetRepositoryEndpoint, codeartifact:ReadFromRepository and sts:GetServiceBearerToken.
Dependency confusion and origin controls
An attacker publishes a public package with the same name as your internal one and a higher version. If your tool resolves from both, it may install the malicious copy. Package origin controls let you set, per package, whether versions may be published directly or ingested from upstream, e.g. internal packages are publish allowed, upstream blocked.
Choosing an artifact store
| Artifact | Store | Notes |
|---|---|---|
| Libraries (npm, pip, Maven) | CodeArtifact | Upstream caching, versioning, access control |
| Container images | ECR | Scan on push, lifecycle, replication, immutable tags |
| Deployment bundles, zips, templates | S3 (versioned) | Cheap; bucket policy and KMS control access |
| Lambda layers | Lambda | Versioned, shareable |
ECR practices
- Immutable tags prevent overwrites; tag with the Git commit SHA and optionally a moving tag like
prod. - Lifecycle policies expire untagged or old images.
- Enhanced scanning with Amazon Inspector continuously scans images for CVEs and can emit EventBridge findings.
- Replication copies images across Regions and accounts for DR and locality.
Pipeline artifact store
For cross-account pipelines the S3 bucket must be encrypted with a customer managed KMS key whose key policy allows the target account roles; the default AWS managed aws/s3 key cannot be shared across accounts. The bucket policy must allow those roles to s3:GetObject.
Artifact promotion
Build once, store with a unique version, deploy that exact version to every environment. Record metadata (commit, build ID, scan results) in tags or a manifest so that audits can trace what runs in production.
Cost and retention
Set S3 lifecycle rules on the artifact bucket (for example 30 days), ECR lifecycle policies on old tags, and CodeArtifact package version retention for unneeded snapshots.
Repository permissions and artifact security
- CodeArtifact uses a domain policy (cross-account access to the domain) and repository policies; clients obtain a short-lived token with
aws codeartifact get-authorization-token(valid up to 12 hours) and needcodeartifact:GetAuthorizationTokenplussts:GetServiceBearerToken. - ECR repositories use repository policies and lifecycle policies; enable tag immutability, scan on push (basic or enhanced with Inspector), and use pull-through cache rules for public registries.
- S3 artifact buckets should use SSE-KMS, versioning, a bucket policy limited to the pipeline roles, and lifecycle rules; use Object Lock when artifacts need WORM retention.
- Package origin controls (allow or block publish and upstream per package) stop dependency-confusion attacks where a public package shadows an internal name.
- EC2 Image Builder automates golden AMI and container image creation with build and test components, distribution settings across Regions and accounts, and EventBridge notifications.
Artifacts have a lifecycle: create (build), store (immutable, versioned), promote (same artifact through stages), and expire (retention rules). Promote artifacts, not rebuilds, so what was tested is what ships.
Real World Example
Scenario: a bank worries about supply-chain attacks. Developers previously ran npm install against the public registry from laptops and build servers. The security team creates one CodeArtifact domain with a vetted shared repository (external connection to npmjs) and per-team repositories. Internal package names are protected with origin controls that block upstream ingestion.
Builds authenticate with codeartifact login, container images are pushed to ECR with scan on push and immutable SHA tags, and EventBridge forwards critical findings to the pipeline, which blocks promotion. Dependency risk becomes visible and auditable.
Architecture Diagram
Builds consume vetted packages and publish immutable artifacts.
- 1A package is requested from the team repository.
- 2On a miss, CodeArtifact fetches it from the upstream chain and caches it.
- 3CodeBuild authenticates with a short-lived token and resolves dependencies.
- 4The build publishes an image to ECR and a bundle to the S3 artifact store.
- 5Scan findings and metadata decide whether the artifact is promoted.
Best Practices
- ✓Route all dependency resolution through CodeArtifact and block direct internet registries in builds.
- ✓Apply package origin controls to internal package names.
- ✓Use immutable, SHA-based tags and promote the same image across environments.
- ✓Encrypt cross-account pipeline artifacts with a customer managed KMS key.
- ✓Set lifecycle policies on S3 and ECR to control storage cost.
Common Interview Questions
Exam Tips
DOP-C02 focus
- "Cache public packages / central proxy for npm, Maven, pip" = CodeArtifact with an external connection.
- "Cross-account pipeline artifact decrypt fails" = CMK shared in key policy, not aws/s3.
- "Scan images for vulnerabilities on push" = ECR scanning (Inspector enhanced scanning for continuous).
- CodeArtifact auth tokens last 12 hours by default (up to 12 hours configurable down).
Finished this lesson?
0 of 44 lessons done · 0% of the course

