Skip to content

Cicd

Chapter 31: AWS CodeCommit, CodeBuild & CodeDeploy

Section titled “Chapter 31: AWS CodeCommit, CodeBuild & CodeDeploy”

AWS provides a suite of native CI/CD services that enable you to build, test, and deploy applications automatically.

AWS CI/CD Services Overview
+------------------------------------------------------------------+
| |
| +------------------------+ |
| | AWS CI/CD Suite | |
| +------------------------+ |
| | |
| +---------------------+---------------------+ |
| | | | |
| v v v |
| +----------+ +----------+ +----------+ |
| | CodeCommit| | CodeBuild| | CodeDeploy| |
| | | | | | | |
| | - Source | | - Build | | - Deploy | |
| | Control| | - Test | | - Release| |
| | - Repos | | - Package| | - Rollback |
| +----------+ +----------+ +----------+ |
| |
+------------------------------------------------------------------+
FeatureCodeCommitCodeBuildCodeDeploy
Primary UseSource controlBuild & testDeployment
AnalogyGitHub/GitLabJenkinsSpinnaker
PricingFree tier + storagePer build minutePer deployment
IntegrationGit-basedDocker-basedAgent-based

AWS CodeCommit Architecture
+------------------------------------------------------------------+
| |
| +------------------------+ |
| | AWS CodeCommit | |
| +------------------------+ |
| | |
| +---------------------+---------------------+ |
| | | | |
| v v v |
| +----------+ +----------+ +----------+ |
| | Git | | Repositories| | Branches | |
| | Protocols| | | | | |
| | | | | | | |
| | - HTTPS | | - Create | | - Create | |
| | - SSH | | - Clone | | - Merge | |
| | - GRC | | - Push | | - Delete | |
| +----------+ +----------+ +----------+ |
| |
+------------------------------------------------------------------+
CodeCommit Repository Features
+------------------------------------------------------------------+
| |
| Core Features |
| +------------------------------------------------------------+ |
| | - Git-based version control | |
| | - Unlimited private repositories | |
| | - Branching and merging | |
| | - Pull requests | |
| | - Code reviews | |
| | - Notifications | |
| +------------------------------------------------------------+ |
| |
| Security Features |
| +------------------------------------------------------------+ |
| | - Encryption at rest (KMS) | |
| | - Encryption in transit (TLS) | |
| | - IAM-based access control | |
| | - Cross-account access | |
| | - Git credential helper | |
| +------------------------------------------------------------+ |
| |
| Integration Features |
| +------------------------------------------------------------+ |
| | - AWS CodePipeline integration | |
| | - AWS Lambda triggers | |
| | - Amazon SNS notifications | |
| | - AWS CodeGuru Reviewer | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# Create repository
aws codecommit create-repository \
--repository-name my-repo \
--repository-description "My application repository"
# List repositories
aws codecommit list-repositories
# Get repository
aws codecommit get-repository \
--repository-name my-repo
# Clone repository (HTTPS)
git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/my-repo
# Clone repository (SSH)
git clone ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/my-repo
# Create branch
aws codecommit create-branch \
--repository-name my-repo \
--branch-name feature-branch \
--commit-id abc123
# List branches
aws codecommit list-branches \
--repository-name my-repo
# Create pull request
aws codecommit create-pull-request \
--title "Feature implementation" \
--description "Adding new feature" \
--targets repositoryName=my-repo,sourceReference=feature-branch,destinationReference=main
# Merge pull request
aws codecommit merge-pull-request-by-fast-forward \
--pull-request-id 1 \
--repository-name my-repo
# Get file
aws codecommit get-file \
--repository-name my-repo \
--file-path src/app.js
# Put file
aws codecommit put-file \
--repository-name my-repo \
--branch-name main \
--file-path src/new-file.js \
--file-content fileb://new-file.js \
--commit-message "Add new file"
# Delete repository
aws codecommit delete-repository \
--repository-name my-repo

AWS CodeBuild Architecture
+------------------------------------------------------------------+
| |
| +------------------------+ |
| | AWS CodeBuild | |
| +------------------------+ |
| | |
| +---------------------+---------------------+ |
| | | | |
| v v v |
| +----------+ +----------+ +----------+ |
| | Build | | Build | | Build | |
| | Projects | | Process | | Artifacts| |
| | | | | | | |
| | - Config | | - Source | | - Output | |
| | - Env | | - Build | | - Logs | |
| | - IAM | | - Post | | - Cache | |
| +----------+ +----------+ +----------+ |
| |
+------------------------------------------------------------------+
CodeBuild Project Configuration
+------------------------------------------------------------------+
| |
| Source Configuration |
| +------------------------------------------------------------+ |
| | | |
| | Supported Sources: | |
| | +------------------------------------------------------+ | |
| | | - AWS CodeCommit | | |
| | | - GitHub / GitHub Enterprise | | |
| | | - Bitbucket | | |
| | | - Amazon S3 | | |
| | | - AWS CodePipeline | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
| Environment Configuration |
| +------------------------------------------------------------+ |
| | | |
| | Compute Types: | |
| | +------------------------------------------------------+ | |
| | | - BUILD_GENERAL1_SMALL (3 GB, 2 vCPU) | | |
| | | - BUILD_GENERAL1_MEDIUM (7 GB, 4 vCPU) | | |
| | | - BUILD_GENERAL1_LARGE (15 GB, 8 vCPU) | | |
| | | - BUILD_GENERAL1_2XLARGE (145 GB, 72 vCPU) | | |
| | +------------------------------------------------------+ | |
| | | |
| | Environment Types: | |
| | +------------------------------------------------------+ | |
| | | - Managed images (AWS provided) | | |
| | | - Custom images (ECR) | | |
| | | - Windows images | | |
| | | - Linux images | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
| Build Specification (buildspec.yml) |
| +------------------------------------------------------------+ |
| | | |
| | version: 0.2 | |
| | | |
| | phases: | |
| | install: | |
| | runtime-versions: | |
| | nodejs: 18 | |
| | commands: | |
| | - npm install | |
| | pre_build: | |
| | commands: | |
| | - npm test | |
| | build: | |
| | commands: | |
| | - npm run build | |
| | post_build: | |
| | commands: | |
| | - echo Build completed | |
| | | |
| | artifacts: | |
| | files: | |
| | - dist/**/* | |
| | base-directory: dist | |
| | | |
| | cache: | |
| | paths: | |
| | - node_modules/**/* | |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
CodeBuild Phases
+------------------------------------------------------------------+
| |
| Phase Flow |
| +------------------------------------------------------------+ |
| | | |
| | +----------+ +----------+ +----------+ | |
| | | INSTALL | --> | PRE_BUILD| --> | BUILD | | |
| | +----------+ +----------+ +----------+ | |
| | | | |
| | v | |
| | +----------+ | |
| | |POST_BUILD| | |
| | +----------+ | |
| | | | |
| | v | |
| | +----------+ | |
| | |ARTIFACTS | | |
| | +----------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
| Phase Descriptions |
| +------------------------------------------------------------+ |
| | | |
| | INSTALL: Install dependencies and tools | |
| | PRE_BUILD: Pre-build tasks (tests, linting) | |
| | BUILD: Main build commands | |
| | POST_BUILD: Cleanup, notifications, packaging | |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# Create build project
aws codebuild create-project \
--name my-build-project \
--source type=CODECOMMIT,location=arn:aws:codecommit:us-east-1:123456789012:my-repo \
--artifacts type=S3,location=my-bucket,name=artifacts \
--environment type=LINUX_CONTAINER,image=aws/codebuild/standard:7.0,computeType=BUILD_GENERAL1_SMALL \
--service-role arn:aws:iam::123456789012:role/CodeBuildServiceRole
# List build projects
aws codebuild list-projects
# Get project details
aws codebuild batch-get-projects \
--names my-build-project
# Start build
aws codebuild start-build \
--project-name my-build-project
# Start build with override
aws codebuild start-build \
--project-name my-build-project \
--environment-variables-override name=ENV,value=production
# List builds
aws codebuild list-builds \
--sort-order DESCENDING
# Get build details
aws codebuild batch-get-builds \
--ids build-id-1 build-id-2
# Delete project
aws codebuild delete-project \
--name my-build-project
# Create webhook (for automatic builds)
aws codebuild create-webhook \
--project-name my-build-project \
--branch-filter main

AWS CodeDeploy Architecture
+------------------------------------------------------------------+
| |
| +------------------------+ |
| | AWS CodeDeploy | |
| +------------------------+ |
| | |
| +---------------------+---------------------+ |
| | | | |
| v v v |
| +----------+ +----------+ +----------+ |
| | App | | Deploy | | Compute | |
| | Name | | Config | | Platforms| |
| | | | | | | |
| | - Groups | | - In-Place| | - EC2 | |
| | - Revisions| | - Blue/Green| | - Lambda | |
| | | | | | - ECS | |
| +----------+ +----------+ +----------+ |
| |
+------------------------------------------------------------------+
CodeDeploy Deployment Types
+------------------------------------------------------------------+
| |
| In-Place Deployment |
| +------------------------------------------------------------+ |
| | | |
| | Before: After: | |
| | +--------+ +--------+ | |
| | | App v1 | Deploy | App v2 | | |
| | | | -----> | | | |
| | +--------+ +--------+ | |
| | | |
| | - Updates instances in place | |
| | - Rolling update (one instance at a time) | |
| | - Suitable for EC2/On-premises | |
| | - Downtime possible during deployment | |
| | | |
| +------------------------------------------------------------+ |
| |
| Blue/Green Deployment |
| +------------------------------------------------------------+ |
| | | |
| | Before: After: | |
| | +--------+ +--------+ | |
| | | Blue | | Green | | |
| | | App v1 | Deploy | App v2 | | |
| | +--------+ -----> +--------+ | |
| | | | | |
| | v v | |
| | +--------+ +--------+ | |
| | | LB --> | | LB --> | | |
| | +--------+ +--------+ | |
| | | |
| | - Creates new environment | |
| | - Traffic shift to new environment | |
| | - Zero downtime | |
| | - Easy rollback (switch back to blue) | |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
CodeDeploy AppSpec File
+------------------------------------------------------------------+
| |
| For EC2/On-Premises (appspec.yml) |
| +------------------------------------------------------------+ |
| | | |
| | version: 0.0 | |
| | os: linux | |
| | | |
| | files: | |
| | - source: / | |
| | destination: /var/www/html | |
| | | |
| | hooks: | |
| | ApplicationStop: | |
| | - location: scripts/stop_server.sh | |
| | timeout: 300 | |
| | BeforeInstall: | |
| | - location: scripts/install_dependencies.sh | |
| | timeout: 300 | |
| | AfterInstall: | |
| | - location: scripts/change_permissions.sh | |
| | timeout: 300 | |
| | ApplicationStart: | |
| | - location: scripts/start_server.sh | |
| | timeout: 300 | |
| | ValidateService: | |
| | - location: scripts/validate_service.sh | |
| | timeout: 300 | |
| | | |
| +------------------------------------------------------------+ |
| |
| For Lambda (appspec.yml) |
| +------------------------------------------------------------+ |
| | | |
| | version: 0.0 | |
| | | |
| | hooks: | |
| | BeforeAllowTraffic: | |
| | - location: scripts/pre_traffic.sh | |
| | AfterAllowTraffic: | |
| | - location: scripts/post_traffic.sh | |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
CodeDeploy Lifecycle Hooks
+------------------------------------------------------------------+
| |
| EC2/On-Premises Deployment Order |
| +------------------------------------------------------------+ |
| | | |
| | 1. ApplicationStop | |
| | - Stop application gracefully | |
| | | |
| | 2. DownloadBundle | |
| | - Download revision from S3 | |
| | | |
| | 3. BeforeInstall | |
| | - Pre-installation tasks | |
| | | |
| | 4. Install | |
| | - Copy files to destination | |
| | | |
| | 5. AfterInstall | |
| | - Post-installation tasks | |
| | | |
| | 6. ApplicationStart | |
| | - Start application | |
| | | |
| | 7. ValidateService | |
| | - Verify application is running | |
| | | |
| +------------------------------------------------------------+ |
| |
| Lambda Deployment Order |
| +------------------------------------------------------------+ |
| | | |
| | 1. BeforeAllowTraffic | |
| | - Pre-traffic validation | |
| | | |
| | 2. AllowTraffic | |
| | - Shift traffic to new version | |
| | | |
| | 3. AfterAllowTraffic | |
| | - Post-traffic validation | |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# Create application
aws deploy create-application \
--application-name my-app \
--compute-platform Server
# List applications
aws deploy list-applications
# Create deployment group
aws deploy create-deployment-group \
--application-name my-app \
--deployment-group-name my-deployment-group \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--ec2-tag-filters Key=Name,Value=WebServer,Type=KEY_AND_VALUE \
--service-role-arn arn:aws:iam::123456789012:role/CodeDeployServiceRole
# List deployment groups
aws deploy list-deployment-groups \
--application-name my-app
# Create deployment
aws deploy create-deployment \
--application-name my-app \
--deployment-group-name my-deployment-group \
--revision revisionType=S3,s3Location='{bucket=my-bucket,key=app.zip,bundleType=zip}' \
--description "Production deployment"
# Get deployment
aws deploy get-deployment \
--deployment-id d-ABCDEF123
# List deployments
aws deploy list-deployments \
--application-name my-app \
--deployment-group-name my-deployment-group
# Stop deployment
aws deploy stop-deployment \
--deployment-id d-ABCDEF123
# Create deployment config
aws deploy create-deployment-config \
--deployment-config-name my-config \
--minimum-healthy-hosts type=FLEET_PERCENT,value=75
# Get deployment target
aws deploy get-deployment-target \
--deployment-id d-ABCDEF123 \
--target-id i-1234567890abcdef0

Complete CI/CD Pipeline
+------------------------------------------------------------------+
| |
| Developer |
| | |
| v |
| +----------+ |
| | Local | |
| | Dev | |
| +----------+ |
| | |
| | git push |
| v |
| +----------+ +----------+ +----------+ +----------+ |
| | CodeCommit| --> | CodeBuild| --> | CodeDeploy| --> | Production| |
| | | | | | | | | |
| | - Source | | - Build | | - Deploy | | - Running| |
| | - Review | | - Test | | - Release| | App | |
| +----------+ +----------+ +----------+ +----------+ |
| | | | |
| v v v |
| +----------+ +----------+ +----------+ |
| | PR Review| | Artifacts| | Rollback | |
| +----------+ +----------+ +----------+ |
| |
+------------------------------------------------------------------+

CodeCommit Best Practices
+------------------------------------------------------------------+
| |
| 1. Use branch protection rules |
| +------------------------------------------------------------+ |
| | - Require pull request reviews | |
| | - Require approved reviews before merge | |
| +------------------------------------------------------------+ |
| |
| 2. Implement approval rules |
| +------------------------------------------------------------+ |
| | - Require specific approvers | |
| | - Set minimum number of approvals | |
| +------------------------------------------------------------+ |
| |
| 3. Use cross-account access |
| +------------------------------------------------------------+ |
| | - Centralize repositories in shared account | |
| | - Grant access via IAM roles | |
| +------------------------------------------------------------+ |
| |
| 4. Enable notifications |
| +------------------------------------------------------------+ |
| | - Notify on pull requests | |
| | - Notify on branch changes | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
CodeBuild Best Practices
+------------------------------------------------------------------+
| |
| 1. Use buildspec.yml in repository |
| +------------------------------------------------------------+ |
| | - Version control build configuration | |
| | - Enable per-branch build customization | |
| +------------------------------------------------------------+ |
| |
| 2. Enable caching |
| +------------------------------------------------------------+ |
| | - Cache dependencies (node_modules, Maven, etc.) | |
| | - Use S3 or local cache | |
| +------------------------------------------------------------+ |
| |
| 3. Use environment variables |
| +------------------------------------------------------------+ |
| | - Store secrets in Parameter Store/Secrets Manager | |
| | - Use environment variables for configuration | |
| +------------------------------------------------------------+ |
| |
| 4. Optimize build time |
| +------------------------------------------------------------+ |
| | - Use appropriate compute type | |
| | - Parallelize tests | |
| | - Use custom images with pre-installed tools | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
CodeDeploy Best Practices
+------------------------------------------------------------------+
| |
| 1. Use Blue/Green deployments for production |
| +------------------------------------------------------------+ |
| | - Zero downtime deployments | |
| | - Easy rollback | |
| +------------------------------------------------------------+ |
| |
| 2. Configure health checks |
| +------------------------------------------------------------+ |
| | - ValidateService hook for verification | |
| | - ELB health checks | |
| +------------------------------------------------------------+ |
| |
| 3. Use deployment configurations |
| +------------------------------------------------------------+ |
| | - AllAtOnce: Fast, risky | |
| | - HalfAtATime: Balanced | |
| | - OneAtATime: Slow, safe | |
| +------------------------------------------------------------+ |
| |
| 4. Configure rollback |
| +------------------------------------------------------------+ |
| | - Automatic rollback on failure | |
| | - Rollback on alarm threshold | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Common CI/CD Issues
+------------------------------------------------------------------+
| |
| Issue 1: CodeCommit authentication failed |
| +------------------------------------------------------------+ |
| | Cause: Missing IAM permissions or credentials | |
| | Solution: Configure git credential helper or SSH keys | |
| +------------------------------------------------------------+ |
| |
| Issue 2: CodeBuild timeout |
| +------------------------------------------------------------+ |
| | Cause: Build taking longer than default timeout | |
| | Solution: Increase timeout or optimize build process | |
| +------------------------------------------------------------+ |
| |
| Issue 3: CodeDeploy agent not running |
| +------------------------------------------------------------+ |
| | Cause: CodeDeploy agent stopped or not installed | |
| | Solution: Install/start codedeploy-agent service | |
| +------------------------------------------------------------+ |
| |
| Issue 4: Deployment failed with no error message |
| +------------------------------------------------------------+ |
| | Cause: Script errors or missing files | |
| | Solution: Check /opt/codedeploy-agent/deployment-root | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Exam Tip

Key Exam Points
+------------------------------------------------------------------+
| |
| 1. CodeCommit = Git-based source control (like GitHub) |
| |
| 2. CodeBuild = Managed build service (no servers to manage) |
| |
| 3. CodeDeploy = Automated deployments to EC2, Lambda, ECS |
| |
| 4. CodeBuild uses buildspec.yml for configuration |
| |
| 5. CodeDeploy uses appspec.yml for deployment hooks |
| |
| 6. Blue/Green deployment = zero downtime, easy rollback |
| |
| 7. In-Place deployment = updates existing instances |
| |
| 8. CodeDeploy agent required on EC2 instances |
| |
| 9. CodeBuild supports Docker, Lambda, custom images |
| |
| 10. CodeCommit supports HTTPS, SSH, GRC protocols |
| |
+------------------------------------------------------------------+

Chapter 31 Summary
+------------------------------------------------------------------+
| |
| AWS CodeCommit |
| +------------------------------------------------------------+ |
| | - Git-based source control | |
| | - Private repositories | |
| | - Pull requests and code reviews | |
| | - IAM-based access control | |
| +------------------------------------------------------------+ |
| |
| AWS CodeBuild |
| +------------------------------------------------------------+ |
| | - Fully managed build service | |
| | - buildspec.yml configuration | |
| | - Multiple environment types | |
| | - Pay per build minute | |
| +------------------------------------------------------------+ |
| |
| AWS CodeDeploy |
| +------------------------------------------------------------+ |
| | - Automated deployments | |
| | - In-Place and Blue/Green deployments | |
| | - appspec.yml configuration | |
| | - Lifecycle hooks for custom scripts | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Next Chapter: Chapter 32: AWS CodePipeline & CI/CD Best Practices