AWS Developer Tools (Code* Services)
CodeCommit, CodeBuild, CodeDeploy, CodePipeline, CodeArtifact
AWS Developer Tools là bộ công cụ CI/CD hoàn chỉnh giúp tự động hóa quy trình phát triển phần mềm từ source code đến deployment.
Tổng Quan
So Sánh Các Services
| Service | Mục đích | Tương đương |
|---|---|---|
| CodeCommit | Git repository | GitHub, GitLab |
| CodeBuild | Build & test | Jenkins, CircleCI |
| CodeDeploy | Deployment automation | Ansible, Octopus |
| CodePipeline | CI/CD orchestration | Jenkins Pipeline, GitLab CI |
| CodeArtifact | Package management | Nexus, Artifactory |
| CodeGuru | AI code review | SonarQube |
| CodeStar | Project management | Azure DevOps |
Config Files: buildspec.yml vs appspec.yml
AWS tách riêng Build và Deploy thành các services khác nhau, mỗi service có config file riêng:
| buildspec.yml | appspec.yml | |
|---|---|---|
| Service | CodeBuild | CodeDeploy |
| Mục đích | Define build steps | Define deployment steps |
| Phases | install, pre_build, build, post_build | Hooks: BeforeInstall, AfterInstall, etc. |
| Output | Artifacts (files để deploy) | Deployed application |
| Location | Root của source code | Root của artifact bundle |
Project Structure Example
Complete Flow
[!NOTE] Cả 2 files đều nằm trong source code repository, nhưng được đọc bởi services khác nhau ở stages khác nhau của pipeline!
1️⃣ AWS CodeCommit
Khái Niệm
CodeCommit là dịch vụ Git repository được quản lý hoàn toàn bởi AWS.
[!NOTE] Update 2024: AWS đã thông báo CodeCommit sẽ không nhận thêm khách hàng mới. Khuyến nghị sử dụng GitHub, GitLab, hoặc Bitbucket thay thế.
Đặc Điểm Chính
| Đặc điểm | Mô tả |
|---|---|
| Fully Managed | Không cần quản lý infrastructure |
| Highly Available | Multi-AZ replication |
| Secure | Encryption at rest (KMS), in-transit (HTTPS/SSH) |
| Scalable | Không giới hạn repositories |
| Integration | Tích hợp với AWS services |
Authentication
Cross-Account Access
Triggers & Notifications
2️⃣ AWS CodeBuild
Khái Niệm
CodeBuild là dịch vụ build được quản lý hoàn toàn, tự động scale và chỉ tính tiền khi build.
Build Triggers
CodeBuild có thể được trigger theo nhiều cách:
Trigger Methods Summary
| Trigger Method | Use Case | Configuration |
|---|---|---|
| CodePipeline | CI/CD pipeline tự động | Build stage trong pipeline |
| Webhooks | GitHub/Bitbucket direct integration | Enable trong CodeBuild project |
| EventBridge | CodeCommit, S3 changes, scheduled | EventBridge Rule → CodeBuild target |
| Manual/CLI | Testing, on-demand builds | aws codebuild start-build |
| Lambda | Custom logic triggers | Call StartBuild API |
| Step Functions | Orchestrated workflows | CodeBuild integration |
EventBridge Rule Example
Webhook Events (GitHub/Bitbucket)
| Event Type | Description |
|---|---|
| PUSH | Trigger on git push |
| PULL_REQUEST_CREATED | Trigger when PR created |
| PULL_REQUEST_UPDATED | Trigger when PR updated |
| PULL_REQUEST_MERGED | Trigger when PR merged |
[!TIP] Sử dụng Filter Groups để control chính xác khi nào build được trigger (ví dụ: chỉ build khi push vào branch
mainhoặc khi file path match pattern).
buildspec.yml
File cấu hình build, phải đặt ở root của source code hoặc chỉ định path.
Phases Chi Tiết
| Phase | Mô tả | Use Cases |
|---|---|---|
| install | Cài đặt dependencies | npm install, pip install |
| pre_build | Chuẩn bị trước build | Login ECR, run tests |
| build | Build chính | Compile, docker build |
| post_build | Sau khi build | Push images, deploy |
Environment Variables
Build trong VPC
[!IMPORTANT] Khi chạy CodeBuild trong VPC, cần NAT Gateway hoặc VPC Endpoints để access AWS services và download dependencies.
3️⃣ AWS CodeDeploy
Khái Niệm
CodeDeploy tự động hóa việc deploy application đến EC2, Lambda, ECS, On-premises servers.
Deployment Targets
CodeDeploy Agent
Tại sao cần Agent?
CodeDeploy service nằm trên AWS Cloud, nhưng EC2/server là máy riêng biệt. Agent đóng vai trò "tai mắt và tay chân" của CodeDeploy trên server:
Tại sao dùng Agent thay vì SSH?
| SSH Approach | Agent Approach ✅ | |
|---|---|---|
| Connection | Push: AWS SSH vào server | Pull: Agent polling từ server |
| Security | Mở port 22, quản lý SSH keys | Chỉ HTTPS outbound (443) |
| Firewall | Allow inbound SSH | Không cần mở inbound port |
| Scaling | Khó scale nhiều servers | Dễ scale - mỗi agent tự polling |
| Key mgmt | Phức tạp | Dùng IAM Role |
Cài đặt Agent
Agent Requirements
| Target | Agent? | Lý do |
|---|---|---|
| EC2 | ✅ Cần | Agent nhận lệnh và thực thi deploy |
| On-premises | ✅ Cần | Như EC2 |
| Lambda | ❌ Không | AWS quản lý, chỉ update function |
| ECS | ❌ Không | Deploy qua Task Definition |
[!IMPORTANT] EC2 instance cần IAM Role với quyền:
s3:GetObject- Download artifactscodedeploy:*- Giao tiếp với CodeDeploy service
appspec.yml (EC2/On-premises)
Deployment Hooks Lifecycle
Deployment Strategies
CodeDeploy có các deployment strategies khác nhau cho từng target:
Summary Table
| Target | Strategies | Cơ chế | Phí thêm? |
|---|---|---|---|
| EC2/On-prem | In-Place, Blue/Green | Instance replacement | Blue/Green có phí EC2 mới |
| Lambda | AllAtOnce, Canary, Linear | Traffic shifting giữa versions | Không |
| ECS | AllAtOnce, Canary, Linear | Traffic shifting qua ALB | Không (đã có ECS tasks) |
EC2: In-Place Deployment
EC2: Blue/Green Deployment
Cost Comparison
| Strategy | Chi phí | Mô tả |
|---|---|---|
| In-Place | Thấp | Chỉ phí CodeDeploy (có free tier) |
| Blue/Green | Cao hơn | Phí EC2 instances mới + Load Balancer |
[!WARNING] Blue/Green cost = Thời gian 2 environments chạy song song × Phí EC2 + Load Balancer. Sau khi deploy xong, terminate Blue environment để dừng tính phí.
Khi nào dùng strategy nào?
| Scenario | Strategy | Lý do |
|---|---|---|
| Dev/Test | In-Place (AllAtOnce) | Tiết kiệm, downtime OK |
| Production | Blue/Green | Zero downtime, easy rollback |
| Budget limited | In-Place (OneAtATime) | Không tốn thêm EC2 |
| Need instant rollback | Blue/Green | Chỉ cần switch traffic back |
| Stateful apps | In-Place | Giữ data trên instance |
appspec.yml (Lambda)
Lambda: Traffic Shifting Strategies
| Strategy | Mô tả | Use Case |
|---|---|---|
| Canary10Percent5Minutes | 10% traffic, đợi 5 phút, rồi 100% | Safe deployment |
| Linear10PercentEvery1Minute | Tăng 10% mỗi phút | Gradual rollout |
| AllAtOnce | 100% ngay lập tức | Fast deployment |
appspec.yml (ECS)
Rollback Strategies
4️⃣ AWS CodePipeline
Khái Niệm
CodePipeline là dịch vụ CI/CD orchestration, tự động hóa toàn bộ quy trình từ source đến production.
Pipeline Structure
Artifacts và S3
Action Types
| Category | Actions | Mô tả |
|---|---|---|
| Source | CodeCommit, GitHub, S3, ECR | Trigger pipeline |
| Build | CodeBuild, Jenkins | Compile, test |
| Test | CodeBuild, Device Farm | Testing |
| Deploy | CodeDeploy, ECS, S3, CloudFormation | Deployment |
| Approval | Manual Approval | Human gate |
| Invoke | Lambda, Step Functions | Custom actions |
CloudWatch Events Integration
Parallel và Sequential Actions
5️⃣ AWS CodeArtifact
Khái Niệm
CodeArtifact là artifact repository service để lưu trữ và chia sẻ software packages.
Supported Package Types
| Package Manager | Language |
|---|---|
| npm | JavaScript/TypeScript |
| pip/twine | Python |
| Maven/Gradle | Java |
| NuGet | .NET |
Usage Example
Upstream Repositories
6️⃣ AWS CodeGuru
Khái Niệm
CodeGuru sử dụng Machine Learning để automated code review và application performance recommendations.
Components
CodeGuru Reviewer Integration
7️⃣ AWS CodeStar
Khái Niệm
CodeStar là unified interface để quản lý software development activities.
[!NOTE] CodeStar đang được phased out. AWS khuyến nghị sử dụng các services riêng lẻ (CodeCommit, CodeBuild, CodePipeline, CodeDeploy).
Features
- One dashboard cho toàn bộ project
- Pre-configured templates
- Team management with IAM
- Issue tracking integration (JIRA)
- IDE integration
🔄 Complete CI/CD Pipeline Example
📋 Exam Tips
Key Points for AWS Certification
| Topic | Remember |
|---|---|
| buildspec.yml | Root of source code, phases: install, pre_build, build, post_build |
| appspec.yml | Root for EC2, hooks define lifecycle |
| CodeBuild VPC | Cần NAT Gateway hoặc VPC Endpoints |
| Lambda deploy | Traffic shifting: Canary, Linear, AllAtOnce |
| ECS deploy | Blue/Green only |
| Artifacts | Stored in S3, encrypted với KMS |
| Cross-region | Need S3 replication |
| CodeArtifact | Upstream từ public registries |
Common Scenarios
🔗 Related Resources
- CloudFormation - Infrastructure as Code
- CDK - Cloud Development Kit
- Lambda - Serverless compute
- ECS - Container orchestration