|
| 1 | +Container Image Builds for ECS and AgentCore |
| 2 | +============================================= |
| 3 | + |
| 4 | +This is the design for extending `sam build`, `sam package`, `sam deploy`, and `sam sync` |
| 5 | +to support building and deploying container images for non-Lambda resources: |
| 6 | +`AWS::ECS::TaskDefinition` and `AWS::BedrockAgentCore::Runtime`. |
| 7 | + |
| 8 | +What is the problem? |
| 9 | +-------------------- |
| 10 | + |
| 11 | +SAM CLI provides an excellent developer experience for Lambda Image functions: a single |
| 12 | +`sam build && sam deploy` builds the Docker image, pushes to ECR, and deploys via |
| 13 | +CloudFormation. However, users deploying containerized workloads to ECS (Fargate) or |
| 14 | +Bedrock AgentCore must manage their Docker build/push/deploy pipeline separately, even |
| 15 | +when these resources live in the same CloudFormation template alongside Lambda functions. |
| 16 | + |
| 17 | +This creates a fragmented workflow where developers need external tooling (shell scripts, |
| 18 | +Makefiles, or CI/CD steps) for the identical operation: build image → push to ECR → |
| 19 | +update template with ECR URI → deploy. |
| 20 | + |
| 21 | +What will be changed? |
| 22 | +--------------------- |
| 23 | + |
| 24 | +We extend the existing Lambda Image build pipeline to recognize `AWS::ECS::TaskDefinition` |
| 25 | +and `AWS::BedrockAgentCore::Runtime` resources that have a `Metadata` block with |
| 26 | +`Dockerfile` and `DockerContext`. No new commands are introduced — the existing |
| 27 | +`sam build`, `sam package`, `sam deploy`, and `sam sync` gain awareness of these |
| 28 | +resource types. |
| 29 | + |
| 30 | +### Design Principles |
| 31 | + |
| 32 | +1. **Same convention** — Uses the identical Metadata block as Lambda Image functions |
| 33 | + (Dockerfile, DockerContext, DockerTag, DockerBuildArgs, DockerBuildTarget) |
| 34 | +2. **No Transform changes** — Works with native CloudFormation resource types |
| 35 | +3. **Opt-in** — Only resources with the Metadata block are affected; existing templates |
| 36 | + work unchanged |
| 37 | +4. **Reuse** — Delegates to the same `_build_lambda_image()` Docker build logic |
| 38 | + |
| 39 | +Success criteria for the change |
| 40 | +------------------------------- |
| 41 | + |
| 42 | +1. `sam build` discovers and builds container images for ECS TaskDefinitions and |
| 43 | + AgentCore Runtimes that have Dockerfile metadata |
| 44 | +2. `sam deploy --resolve-image-repos` auto-creates ECR repos for these resources |
| 45 | +3. `sam package` / `sam deploy` pushes images to ECR and rewrites the template with |
| 46 | + the ECR URI at the correct property path |
| 47 | +4. `sam sync` builds, pushes, and triggers redeployment for these resources |
| 48 | +5. Multi-container ECS TaskDefinitions can target a specific container via `ContainerName` |
| 49 | +6. Architecture can be specified via `Architecture` metadata (e.g., `arm64`) |
| 50 | +7. Buildkit support works automatically (shared with Lambda Image builds) |
| 51 | +8. No regressions for existing Lambda, Layer, or API builds |
| 52 | + |
| 53 | +User Experience |
| 54 | +--------------- |
| 55 | + |
| 56 | +### Template Format |
| 57 | + |
| 58 | +```yaml |
| 59 | +Resources: |
| 60 | + # AgentCore Runtime |
| 61 | + MyAgent: |
| 62 | + Type: AWS::BedrockAgentCore::Runtime |
| 63 | + Metadata: |
| 64 | + Dockerfile: Dockerfile |
| 65 | + DockerContext: ./agent |
| 66 | + DockerTag: latest |
| 67 | + Architecture: arm64 |
| 68 | + Properties: |
| 69 | + AgentRuntimeName: my_agent |
| 70 | + AgentRuntimeArtifact: |
| 71 | + ContainerConfiguration: |
| 72 | + ContainerUri: placeholder |
| 73 | + NetworkConfiguration: |
| 74 | + NetworkMode: PUBLIC |
| 75 | + RoleArn: !GetAtt AgentRole.Arn |
| 76 | + |
| 77 | + # ECS TaskDefinition (multi-container) |
| 78 | + MyTask: |
| 79 | + Type: AWS::ECS::TaskDefinition |
| 80 | + Metadata: |
| 81 | + Dockerfile: Dockerfile |
| 82 | + DockerContext: ./app |
| 83 | + DockerTag: latest |
| 84 | + ContainerName: web |
| 85 | + Properties: |
| 86 | + Family: my-app |
| 87 | + ContainerDefinitions: |
| 88 | + - Name: envoy |
| 89 | + Image: public.ecr.aws/envoy:latest |
| 90 | + - Name: web |
| 91 | + Image: placeholder |
| 92 | +``` |
| 93 | +
|
| 94 | +### CLI Usage |
| 95 | +
|
| 96 | +```bash |
| 97 | +# Build container images |
| 98 | +sam build |
| 99 | + |
| 100 | +# Deploy with auto ECR repo creation |
| 101 | +sam deploy --resolve-image-repos |
| 102 | + |
| 103 | +# Or with explicit repo |
| 104 | +sam deploy --image-repositories SimpleAgent=123456789012.dkr.ecr.us-east-1.amazonaws.com/repo |
| 105 | + |
| 106 | +# Live sync |
| 107 | +sam sync --stack-name my-stack --watch --resolve-image-repos |
| 108 | +``` |
| 109 | + |
| 110 | +### Metadata Fields |
| 111 | + |
| 112 | +| Field | Required | Description | |
| 113 | +|-------|----------|-------------| |
| 114 | +| `Dockerfile` | Yes | Path to Dockerfile relative to DockerContext | |
| 115 | +| `DockerContext` | Yes | Build context directory relative to template | |
| 116 | +| `DockerTag` | No | Image tag (default: `latest`) | |
| 117 | +| `DockerBuildArgs` | No | Dict of build args | |
| 118 | +| `DockerBuildTarget` | No | Multi-stage build target | |
| 119 | +| `DockerBuildExtraParams` | No | List of extra docker build params | |
| 120 | +| `Architecture` | No | Target platform: `x86_64` (default) or `arm64` | |
| 121 | +| `ContainerName` | No | ECS only: target container in multi-container TaskDefinition | |
| 122 | + |
| 123 | +Implementation |
| 124 | +-------------- |
| 125 | + |
| 126 | +### Architecture |
| 127 | + |
| 128 | +``` |
| 129 | +┌─────────────────────────────────────────────────────────────────┐ |
| 130 | +│ sam build │ |
| 131 | +├─────────────────────────────────────────────────────────────────┤ |
| 132 | +│ BuildContext.run() │ |
| 133 | +│ ├── builder.build() → Lambda functions + layers │ |
| 134 | +│ └── _build_container_images() → ECS + AgentCore containers │ |
| 135 | +│ ├── SamContainerServiceProvider (discovery) │ |
| 136 | +│ ├── ContainerBuildDefinition (build graph) │ |
| 137 | +│ └── ApplicationBuilder.build_container_images() │ |
| 138 | +│ └── _build_lambda_image() (shared Docker logic) │ |
| 139 | +├─────────────────────────────────────────────────────────────────┤ |
| 140 | +│ sam package / sam deploy │ |
| 141 | +│ ├── sync_ecr_stack() → auto-creates ECR repos (companion) │ |
| 142 | +│ ├── ECSTaskDefinitionImageResource.export() → push + rewrite │ |
| 143 | +│ └── AgentCoreRuntimeImageResource.export() → push + rewrite │ |
| 144 | +├─────────────────────────────────────────────────────────────────┤ |
| 145 | +│ sam sync │ |
| 146 | +│ └── ECSContainerSyncFlow │ |
| 147 | +│ ├── gather_resources() → build image │ |
| 148 | +│ ├── sync() → push to ECR + force ECS deployment │ |
| 149 | +│ └── SyncFlowFactory (registered for both types) │ |
| 150 | +└─────────────────────────────────────────────────────────────────┘ |
| 151 | +``` |
| 152 | + |
| 153 | +### Key Components |
| 154 | + |
| 155 | +**`samcli/lib/providers/sam_container_provider.py`** (new) |
| 156 | +- `SamContainerServiceProvider`: Scans stacks for ECS/AgentCore resources with |
| 157 | + Dockerfile+DockerContext metadata. Returns `ContainerService` NamedTuples. |
| 158 | + |
| 159 | +**`samcli/lib/build/build_graph.py`** (modified) |
| 160 | +- `ContainerBuildDefinition`: Parallel to `FunctionBuildDefinition`. Holds resource |
| 161 | + identifier, type, metadata, and architecture. Reads `Architecture` from metadata. |
| 162 | + |
| 163 | +**`samcli/lib/build/app_builder.py`** (modified) |
| 164 | +- `build_container_images()`: Iterates container definitions and builds each. |
| 165 | +- `_build_container_image()`: Delegates to `_build_lambda_image()` — same Docker logic. |
| 166 | +- `_update_built_resource()`: Extended for ECS (`ContainerDefinitions[N].Image`) and |
| 167 | + AgentCore (`AgentRuntimeArtifact.ContainerConfiguration.ContainerUri`). Accepts |
| 168 | + optional `metadata` param for `ContainerName` targeting. |
| 169 | + |
| 170 | +**`samcli/lib/package/packageable_resources.py`** (modified) |
| 171 | +- `ECSTaskDefinitionImageResource`: Custom export for nested `ContainerDefinitions[0].Image`. |
| 172 | +- `AgentCoreRuntimeImageResource`: Export using jmespath for deeply nested property path. |
| 173 | +- Both use `ARTIFACT_TYPE = ZIP` to pass the `PackageType` filter (these resources |
| 174 | + don't have a `PackageType` property). |
| 175 | + |
| 176 | +**`samcli/lib/sync/flows/ecs_container_sync_flow.py`** (new) |
| 177 | +- `ECSContainerSyncFlow`: Builds image, pushes to ECR, forces ECS service redeployment |
| 178 | + by finding services using the task definition family. |
| 179 | + |
| 180 | +**`samcli/lib/bootstrap/companion_stack/companion_stack_manager.py`** (modified) |
| 181 | +- `sync_ecr_stack()`: Extended to include container service resources when creating |
| 182 | + ECR repos via the companion stack. |
| 183 | + |
| 184 | +### Property Path Mapping |
| 185 | + |
| 186 | +| Resource Type | Property Path for Image URI | |
| 187 | +|---------------|----------------------------| |
| 188 | +| `AWS::Serverless::Function` | `ImageUri` | |
| 189 | +| `AWS::Lambda::Function` | `Code.ImageUri` | |
| 190 | +| `AWS::ECS::TaskDefinition` | `ContainerDefinitions[N].Image` | |
| 191 | +| `AWS::BedrockAgentCore::Runtime` | `AgentRuntimeArtifact.ContainerConfiguration.ContainerUri` | |
| 192 | + |
| 193 | +Alternatives Considered |
| 194 | +----------------------- |
| 195 | + |
| 196 | +### 1. New SAM Transform resource type (e.g., `AWS::Serverless::ContainerService`) |
| 197 | + |
| 198 | +**Rejected because:** |
| 199 | +- Requires changes to the SAM Transform (separate repo, separate approval process) |
| 200 | +- Adds coupling between SAM CLI and the Transform |
| 201 | +- Users would need to wait for Transform support in all regions |
| 202 | +- Native CFN types already work and are well-understood |
| 203 | + |
| 204 | +### 2. Separate `sam container build` command |
| 205 | + |
| 206 | +**Rejected because:** |
| 207 | +- Fragments the workflow — users would need to remember different commands |
| 208 | +- Doesn't integrate with `sam deploy` and `sam sync` naturally |
| 209 | +- The existing `sam build` already handles image builds for Lambda |
| 210 | + |
| 211 | +### 3. Using `PackageType: Image` on ECS/AgentCore resources |
| 212 | + |
| 213 | +**Rejected because:** |
| 214 | +- `PackageType` is a Lambda-specific concept not present on ECS or AgentCore resources |
| 215 | +- Would require CloudFormation schema changes |
| 216 | +- The Metadata-based approach is already the established pattern |
| 217 | + |
| 218 | +Breaking Changes |
| 219 | +---------------- |
| 220 | + |
| 221 | +None. This is purely additive: |
| 222 | +- Templates without ECS/AgentCore Metadata are unaffected |
| 223 | +- The `_update_built_resource` signature change is backward compatible (optional param) |
| 224 | +- No existing CLI flags or behaviors change |
| 225 | + |
| 226 | +Future Extensions |
| 227 | +----------------- |
| 228 | + |
| 229 | +1. **Multiple Dockerfiles per ECS TaskDefinition** — Build multiple containers from |
| 230 | + one resource using a list of Metadata entries |
| 231 | +2. **`sam local start-ecs`** — Local testing of ECS containers (similar to `sam local start-api`) |
| 232 | +3. **Health check integration** — Wait for container health before marking sync complete |
| 233 | +4. **Build caching** — Layer-aware caching for container builds (currently rebuilds fully) |
| 234 | +5. **`sam init` templates** — Starter templates for ECS+SAM and AgentCore+SAM projects |
0 commit comments