CodeBuild CDK Deployment Template¶
This CloudFormation template automates the deployment of AWS CDK projects for workshop environments. It provides a simplified, robust solution for bootstrapping AWS accounts, deploying CDK applications, and monitoring pipeline executions with intelligent retry handling.
Overview¶
The template creates:
- S3 bucket for configuration storage and CodePipeline source
- CodeBuild project for CDK deployment orchestration with local caching
- Lambda functions for deployment initiation and resource cleanup
- IAM roles with appropriate permissions
- Wait conditions for CloudFormation synchronization
- Intelligent pipeline monitoring with retry support
Deployment Flow¶
flowchart TD
A[CloudFormation Stack Creation] --> B[Create S3 Bucket & IAM Roles]
B --> C[Start Deployment Lambda]
C --> D[CodeBuild Project Starts]
D --> E[Clone GitHub Repository]
E --> F[Download Configuration]
F --> G[Setup S3 Remote]
G --> H{Bootstrap Required?}
H -->|Yes| I[Bootstrap CDK]
H -->|No| J[Deploy CDK Application]
I --> J
J --> K[Extract Pipeline ARN]
K --> L[Monitor Pipeline]
L --> M{Status?}
M -->|InProgress| N[Wait 30s]
N --> L
M -->|Succeeded| O[Signal SUCCESS]
M -->|Failed| P{Retries Left?}
P -->|Yes| Q[Wait & Retry]
Q --> L
P -->|No| R[Signal FAILURE] Parameters¶
| Parameter | Description | Default |
|---|---|---|
pConfigFileUrl | URL to initial configuration file | https://raw.githubusercontent.com/.../default.env |
pOrganizationName | GitHub organization name | aws-samples |
pRepositoryName | Repository containing CDK code | one-observability-demo |
pBranchName | Branch to deploy from | main |
pCodeConnectionArn | Optional CodeConnection ARN for GitHub | (empty) |
pWorkingFolder | Working folder for deployment | src/cdk |
pApplicationName | Application name for tagging | One Observability Workshop |
pDisableCleanup | Disable cleanup on failure | false |
pCDKStackName | CDK stack name for outputs | Microservices-Microservice |
pParameterStoreBasePath | Base path in Parameter Store | /petstore |
pWaitForDeployment | Wait for pipeline completion | true |
pVpcCidr | CIDR block for VPC | 10.0.0.0/16 |
pUserDefinedTagKey1-5 | Custom tag keys | (various) |
pUserDefinedTagValue1-5 | Custom tag values | (various) |
Retry Handling¶
The system automatically detects and handles retries:
- Manual Retries — User manually retries failed pipeline execution
- Automatic Retries — Pipeline configured with automatic retry policies
- Superseded Executions — New execution starts while previous is running
- Partial Failures — Individual stage failures with stage-level retries
Configuration:
- Maximum retries: 3 attempts
- Retry wait time: 60 seconds
- Overall timeout: 2 hours (7200 seconds)
- Polling interval: 30 seconds
Deployment Timeline¶
The deployment is coordinated by two independent timers that must stay in the right order, or a healthy-but-slow deploy is torn down as if it had failed:
| Timer | Where | Value | Role |
|---|---|---|---|
| Pipeline signaller | src/cdk/scripts/wait-for-pipeline.sh (TIMEOUT) | 5100s (85 min) | Polls the inner CDK pipeline and signals SUCCESS/FAILURE to the CloudFormation wait condition. |
| Wait condition | rCDKDeploymentWaitCondition.Timeout in the template | 7200s (120 min) | How long CloudFormation waits for that signal before declaring failure. |
Invariant: the wait condition timeout MUST be greater than the signaller timeout. If the wait condition expires first, CloudFormation rolls the stack back (and, on the rollback path, triggers the cleanup state machine that deletes the environment) even though the deployment was still progressing normally.
0 min ───────────────────────── 85 min ──────────── 120 min
| deploy running | signaller gives | wait condition
| (build + pipeline stages) | up if not done | gives up
+------------------------------+-------------------+
signaller (5100s) < wait condition (7200s)
A typical full deployment (CloudFormation to CodeBuild to the CDK pipeline stages: Core, Backend, Microservices) takes ~40-50 min; the headroom above absorbs slow container builds, ECR pulls, and EKS/ECS convergence.
Keep the two copies of the template in sync
There are two copies of this template: the GitHub source (src/templates/codebuild-deployment-template.yaml) and the Workshop Studio copy in the workshop content repository (Workshop Studio requires the template to live there). They should differ only in parameter default values. A past incident traced to the wait-condition timeout drifting between the two copies (3600 vs 4500), both below the signaller.
Build Phases¶
1. Install¶
2. Pre-Build¶
- Clone repository from GitHub
- Download and merge configuration
- Setup S3 remote for CodePipeline source
- Bootstrap CDK (with intelligent status checking)
3. Build¶
4. Pipeline Monitoring¶
- Execution ID tracking for retry detection
- Status-specific handling for all pipeline states
- Progress reporting every 5 minutes
- Detailed error information on failures
Outputs¶
| Output | Description |
|---|---|
oConfigBucketName | S3 bucket name for configuration storage |
oCodeBuildProjectName | CodeBuild project name |
oDeploymentStatus | Deployment status information |
oRepositoryInfo | Repository configuration details |
Troubleshooting¶
Manual Pipeline Fixes
If you need to manually fix pipeline issues and want the CloudFormation stack to complete: