Intermediate
CI/CD Pipeline
Automate your application deployments with CodePipeline, CodeBuild, and CodeDeploy
Project Overview
Create an automated deployment pipeline that builds, tests, and deploys your application whenever you push code. Essential for modern DevOps practices.
Prerequisites
- GitHub or CodeCommit repository with application code
- EC2 instances or ECS cluster for deployment
- Basic understanding of build processes
- Familiarity with YAML configuration
Architecture
GitHub
Source
CodePipeline
Orchestration
CodeBuild
Build & Test
CodeDeploy
Deploy
EC2/ECS
Target
ECR stores container images, S3 stores build artifacts
Step-by-Step Instructions
1
Prepare Your Repository
- Store your code in GitHub or AWS CodeCommit
- Create a buildspec.yml file in the root directory
- Define build phases: install, pre_build, build, post_build
- Specify artifacts to pass to deployment stage
2
Create CodeBuild Project
- Go to CodeBuild and create a new project
- Connect to your source repository
- Select a managed image (e.g., Amazon Linux 2, Node.js)
- Configure environment variables for sensitive values
- Enable build caching to speed up subsequent builds
3
Create AppSpec File for CodeDeploy
- Create appspec.yml in your repository root
- Define file mappings (source to destination)
- Add lifecycle hooks: BeforeInstall, AfterInstall, ApplicationStart
- Create deployment scripts in a scripts/ folder
4
Set Up CodeDeploy
- Install CodeDeploy agent on target EC2 instances
- Create a CodeDeploy application
- Create a deployment group targeting your instances
- Choose deployment type (in-place or blue/green)
- Configure rollback settings
5
Create CodePipeline
- Create a new pipeline in CodePipeline
- Add Source stage (GitHub or CodeCommit)
- Add Build stage (CodeBuild project)
- Add Deploy stage (CodeDeploy application)
- Optionally add manual approval before production
6
Test the Pipeline
- Push a code change to trigger the pipeline
- Monitor each stage in the CodePipeline console
- Review build logs in CodeBuild
- Verify deployment succeeded on target instances
- Test rollback by intentionally failing a deployment
Tips
- Use Parameter Store for secrets - Store database passwords, API keys securely and reference in buildspec
- Enable build caching - Cache dependencies to significantly speed up builds
- Add notifications - Use SNS to get notified on pipeline failures
- Use blue/green deployments - Safer deployments with instant rollback capability
Code Examples
CodeBuild buildspec.yml
buildspec.yml
YAML
version: 0.2
phases:
install:
runtime-versions:
nodejs: 18
commands:
- npm install
pre_build:
commands:
- echo "Running tests..."
- npm test
build:
commands:
- echo "Building application..."
- npm run build
post_build:
commands:
- echo "Build completed on $(date)"
artifacts:
files:
- '**/*'
base-directory: dist
cache:
paths:
- node_modules/**/*
CodeDeploy appspec.yml
appspec.yml
YAML
version: 0.0
os: linux
files:
- source: /
destination: /var/www/myapp
permissions:
- object: /var/www/myapp
owner: www-data
group: www-data
hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/after_install.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/start_server.sh
timeout: 300
runas: root
ValidateService:
- location: scripts/validate.sh
timeout: 300
Deployment Scripts
scripts/after_install.sh
BASH
#!/bin/bash
set -e
# Navigate to app directory
cd /var/www/myapp
# Install dependencies
npm install --production
# Set correct permissions
chown -R www-data:www-data /var/www/myapp
echo "After install completed"
Pipeline Creation (AWS CLI)
Terminal Commands
BASH
# Create CodeBuild project
aws codebuild create-project \
--name my-build-project \
--source type=GITHUB,location=https://github.com/user/repo \
--artifacts type=S3,location=my-artifact-bucket \
--environment type=LINUX_CONTAINER,image=aws/codebuild/amazonlinux2-x86_64-standard:4.0,computeType=BUILD_GENERAL1_SMALL \
--service-role arn:aws:iam::123456789:role/CodeBuildRole
# Create CodeDeploy application
aws deploy create-application \
--application-name my-app \
--compute-platform Server
# Create deployment group
aws deploy create-deployment-group \
--application-name my-app \
--deployment-group-name production \
--deployment-config-name CodeDeployDefault.OneAtATime \
--ec2-tag-filters Key=Environment,Value=Production,Type=KEY_AND_VALUE \
--service-role-arn arn:aws:iam::123456789:role/CodeDeployRole
What You'll Learn
- Source control integration with AWS services
- Build automation with CodeBuild and buildspec.yml
- Deployment strategies (rolling, blue/green)
- Container image management with ECR
- Pipeline orchestration and stage management