Best practices
1 - Reference architecture
1.1 Multi-environment pipeline
1.2 Infrastructure as Code
# Séparer l'infra et l'application
project/
├── infrastructure/
│ ├── vpc/
│ │ └── template.yaml
│ ├── ecs-cluster/
│ │ └── template.yaml
│ └── pipeline/
│ └── template.yaml
├── application/
│ ├── src/
│ ├── Dockerfile
│ ├── buildspec.yml
│ └── appspec.yml
└── environments/
├── dev.yaml
├── staging.yaml
└── production.yaml
2 - Branching strategies
2.1 Simplified GitFlow
2.2 Trunk-based development
# Pipeline pour trunk-based
Stages:
- Name: Source
Actions:
- Name: GitHub
Configuration:
BranchName: main
- Name: Build
Actions:
- Name: Build
# Build à chaque commit sur main
- Name: DeployDev
Actions:
- Name: Deploy
# Déploiement automatique en dev
- Name: DeployStaging
Actions:
- Name: Deploy
# Déploiement automatique en staging
- Name: DeployProduction
Actions:
- Name: Approval
# Approbation manuelle
- Name: Deploy
# Déploiement Blue/Green
3 - Tests in the pipeline
3.1 The testing pyramid
3.2 buildspec configuration
# buildspec.yml avec tests
version: 0.2
phases:
install:
commands:
- npm ci
pre_build:
commands:
# Linting
- npm run lint
# Analyse statique
- npm run security-audit
build:
commands:
# Build
- npm run build
# Tests unitaires
- npm run test:unit -- --coverage
post_build:
commands:
# Tests d'intégration
- npm run test:integration
reports:
unit-tests:
files: ['coverage/junit.xml']
file-format: JUNITXML
coverage:
files: ['coverage/cobertura.xml']
file-format: COBERTURAXML
4 - Environment management
4.1 Per-environment variables
# environments/dev.yaml
Environment: development
ReplicaCount: 1
InstanceType: t3.small
EnableDebug: true
LogLevel: DEBUG
# environments/production.yaml
Environment: production
ReplicaCount: 3
InstanceType: t3.large
EnableDebug: false
LogLevel: INFO
4.2 SSM parameters per environment
# Structure recommandée
/app/dev/config/api-url
/app/dev/secrets/db-password
/app/staging/config/api-url
/app/staging/secrets/db-password
/app/production/config/api-url
/app/production/secrets/db-password
5 - Rollback and recovery
5.1 Rollback strategies
| Strategy | Rollback time | Complexity |
|---|---|---|
| ECS Rolling | Minutes | Low |
| Blue/Green | Seconds | Medium |
| Canary | Minutes | High |
5.2 ECS Blue/Green configuration
DeploymentGroup:
Type: AWS::CodeDeploy::DeploymentGroup
Properties:
DeploymentStyle:
DeploymentOption: WITH_TRAFFIC_CONTROL
DeploymentType: BLUE_GREEN
BlueGreenDeploymentConfiguration:
TerminateBlueInstancesOnDeploymentSuccess:
Action: TERMINATE
TerminationWaitTimeInMinutes: 60
DeploymentReadyOption:
ActionOnTimeout: CONTINUE_DEPLOYMENT
WaitTimeInMinutes: 0
AutoRollbackConfiguration:
Enabled: true
Events:
- DEPLOYMENT_FAILURE
- DEPLOYMENT_STOP_ON_ALARM
6 - Pipeline monitoring
6.1 Essential metrics
| Metric | Description | Recommended threshold |
|---|---|---|
| Lead Time | Time from commit → production | < 1 day |
| Deploy Frequency | Deployments/day | > 1/day |
| MTTR | Recovery time | < 1 hour |
| Change Failure Rate | % of failed deployments | < 15% |
6.2 CloudWatch dashboard
PipelineMetricsDashboard:
Type: AWS::CloudWatch::Dashboard
Properties:
DashboardBody: |
{
"widgets": [
{
"type": "metric",
"properties": {
"title": "Pipeline Success Rate",
"metrics": [
["AWS/CodePipeline", "SucceededPipeline", "PipelineName", "MyPipeline"],
[".", "FailedPipeline", ".", "."]
]
}
},
{
"type": "metric",
"properties": {
"title": "Build Duration",
"metrics": [
["AWS/CodeBuild", "Duration", "ProjectName", "MyProject"]
],
"stat": "Average"
}
}
]
}
7 - Cost optimization
7.1 Best practices
- Use the S3 cache for CodeBuild
- Choose the right compute type (small for lightweight tests)
- Enable ECR lifecycle policies
- Use spot instances for tests
7.2 Suitable compute type
# Tests rapides - Small
UnitTestsProject:
Type: AWS::CodeBuild::Project
Properties:
Environment:
ComputeType: BUILD_GENERAL1_SMALL
# Build Docker - Medium
BuildProject:
Type: AWS::CodeBuild::Project
Properties:
Environment:
ComputeType: BUILD_GENERAL1_MEDIUM
# Tests de charge - Large
LoadTestsProject:
Type: AWS::CodeBuild::Project
Properties:
Environment:
ComputeType: BUILD_GENERAL1_LARGE
8 - Deployment checklist
Pre-deployment
- Unit tests pass
- Integration tests pass
- Security scan OK
- Code review approved
- Documentation up to date
Post-deployment
- Health checks OK
- Normal metrics
- No errors in the logs
- Smoke tests pass
- Notification sent
Summary
In this chapter, we covered:
- The multi-environment reference architecture
- Branching strategies
- Tests in the pipeline
- Environment management
- Rollback and recovery
- Pipeline monitoring
- Cost optimization
Next step
In the next chapter, we will put things into practice with Exercises and Projects.
→ Next chapter: Exercises and Projects