Lab 11: CI/CD and Automation Baseline
Lab นี้สร้าง pipeline สำหรับตรวจ Terraform, deploy infrastructure, deploy application ไป ECS หรือ Lambda, ทดสอบ rollback และเชื่อม EC2 ผ่าน Session Manager โดยไม่เปิด SSH port
Learning objectives
- สร้าง Pipeline สำหรับตรวจสอบ Terraform
- Deploy Infrastructure จาก Pipeline
- Deploy Application ไปยัง ECS หรือ Lambda
- ทดสอบ Rollback
- เชื่อม EC2 ผ่าน Session Manager โดยไม่เปิด SSH Port
Prerequisites
- มี repository ที่เก็บ Terraform และ application code
- มี AWS Account สำหรับ Lab พร้อม Budget
- มี IAM Role สำหรับ pipeline แบบ Least Privilege และ OIDC หรือ CodeBuild service role
- มี ECS/Lambda workload จาก Phase 10 หรือ workload ตัวอย่างขนาดเล็ก
- มี EC2 instance ที่ติดตั้ง SSM Agent และมี instance profile สำหรับ Systems Manager
Step 1: Create Terraform validation pipeline
- ตั้ง trigger เมื่อเปิด Pull Request หรือ push เข้า branch หลัก
- รัน
terraform fmt -check,terraform validateและ security scan - สร้าง
terraform planสำหรับ dev/staging - แนบ plan summary ใน Pull Request หรือ pipeline artifact
Step 2: Deploy infrastructure from pipeline
- ใช้ OIDC หรือ CodeBuild role แทน long-lived access keys
- ใช้ remote state แยก environment
- deploy dev อัตโนมัติหลัง merge
- ให้ production ต้องผ่าน manual approval
Step 3: Deploy application to ECS or Lambda
ตัวอย่าง GitHub Actions jobs สำหรับ build image และ deploy ECS:
jobs:
build:
steps:
- uses: actions/checkout@v4
- run: docker build -t learning-web:${{ github.sha }} .
- run: docker push "$ECR_REPOSITORY:${{ github.sha }}"
deploy:
needs: build
environment: production
steps:
- run: aws ecs update-service --cluster learning --service web --force-new-deployment
Step 4: Test rollback
- Deploy version ใหม่ที่ตั้งใจให้ health check fail ใน dev
- ตรวจว่า pipeline หรือ deployment service หยุด rollout
- rollback ไป artifact/image version ก่อนหน้า
- บันทึก command, owner และเวลาที่ใช้ rollback
Step 5: Connect EC2 through Session Manager
- ยืนยันว่า EC2 ไม่มี inbound SSH port จาก Internet
- ตรวจ instance profile มี
AmazonSSMManagedInstanceCoreหรือ permission เทียบเท่า - เริ่ม session ผ่าน Console หรือ CLI
- ตรวจว่า session logs ถูกส่งไป CloudWatch Logs หรือ S3
aws ssm start-session --target i-0123456789abcdef0
Expected results
- Pull Request มี Terraform validation และ plan summary
- Dev deploy อัตโนมัติ และ production ต้อง approval
- Application deploy ไป ECS หรือ Lambda ด้วย artifact/image version ที่ระบุชัด
- Rollback path ถูกทดสอบจริง
- EC2 เชื่อมผ่าน Session Manager ได้โดยไม่เปิด SSH inbound
Verification steps
aws sts get-caller-identity
terraform plan -detailed-exitcode
aws ecs describe-services --cluster learning --services web
aws ssm describe-instance-information
aws logs describe-log-groups --log-group-name-prefix /aws/ssm
Troubleshooting
| อาการ | แนวทางแก้ |
|---|---|
| OIDC assume role ไม่ได้ | ตรวจ provider URL, audience, trust policy sub, repository/environment และ role ARN |
| Terraform plan ไม่มี backend access | ตรวจ S3 backend permission, state key, Region และ locking |
| ECS deploy แล้วไม่เปลี่ยน version | ตรวจ image tag, task definition revision และ service deployment events |
| EC2 ไม่ขึ้นใน Systems Manager | ตรวจ SSM Agent, instance profile, outbound network หรือ VPC endpoints |
Cleanup procedures
- Disable หรือ delete pipeline ที่สร้างเพื่อ Lab ถ้าไม่ใช้ต่อ
- ลบ temporary IAM roles/policies ที่เปิดเพื่อทดลอง
- ลบ test ECS/Lambda deployment resources ถ้าสร้างใหม่
- ลบ old artifacts/images ที่ไม่ต้องเก็บ
- เก็บ session logs และ deployment evidence ตาม retention policy
Mini quiz
- ทำไม production deploy ต้องมี approval?
- OIDC ดีกว่า access key ระยะยาวอย่างไร?
- Rollback test ควรวัดอะไรบ้าง?