Quay lại

Tự Động Triển Khai Container trên EC2 với Amazon EventBridge và Systems Manager Chuyên mục Devops    2025-03-12    0 Lượt xem    0 Lượt thích    comment-3 Created with Sketch Beta. 0 Bình luận

1. Giới thiệu

Trong môi trường DevOps, việc tự động hóa triển khai ứng dụng là rất quan trọng để đảm bảo hiệu suất và giảm thiểu lỗi do con người. AWS cung cấp giải pháp sử dụng Amazon EventBridge kết hợp với AWS Systems Manager để tự động cập nhật và triển khai container khi có image mới được đẩy lên Amazon Elastic Container Registry (ECR).

Trong bài viết này, chúng ta sẽ thiết lập một EventBridge Rule để theo dõi sự kiện PUSH image mới lên ECR, từ đó tự động kích hoạt SSM Document để triển khai image lên EC2.


2. Kiến Trúc Hoạt Động

  1. Developer push image mới lên Amazon ECR.

  2. Amazon EventBridge phát hiện sự kiện push image thành công.

  3. EventBridge Rule trigger AWS Systems Manager Document (SSM Document) để chạy các lệnh deploy trên EC2.

  4. SSM Document sẽ pull image từ ECR và khởi động container mới trên EC2.


3. Cấu Hình EventBridge để Trigger Deployment

3.1. Tạo EventBridge Rule

Truy cập AWS Console và vào Amazon EventBridge → Rules → Create Rule:

  • Name: stg-chime-backend-image-pushed

  • Event Bus: default

  • Event Pattern:

{
  "source": ["aws.ecr"],
  "detail-type": ["ECR Image Action"],
  "detail": {
    "action-type": ["PUSH"],
    "result": ["SUCCESS"],
    "repository-name": ["stg_chime_backend_php"],
    "image-tag": ["latest"]
  }
}
  • Target: AWS Systems Manager Automation Document

  • Document Name: deployWebsocketSTGCommand

  • Target Key: EC2 Instance ID

  • Input Transformer (Tùy chọn): Nếu cần truyền tham số vào SSM Document

Nhấn Create Rule để hoàn tất.


4. Cấu Hình AWS Systems Manager Document (SSM Document)

Truy cập AWS Console → Systems Manager → Documents → Create Document.

  • Name: deployWebsocketSTGCommand

  • Document Type: Automation

  • Content:

{
  "schemaVersion": "2.2",
  "description": "Deploy ECR image to EC2",
  "mainSteps": [
    {
      "action": "aws:runShellScript",
      "name": "runShellScript",
      "inputs": {
        "runCommand": [
          "runuser -l ec2-user -c 'docker stop ws || true'",
          "runuser -l ec2-user -c 'docker rm ws || true'",
          "runuser -l ec2-user -c 'docker rmi $(docker images -q)'",
          "runuser -l ec2-user -c 'aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin 533267253502.dkr.ecr.ap-northeast-1.amazonaws.com'",
          "runuser -l ec2-user -c 'docker pull 533267253502.dkr.ecr.ap-northeast-1.amazonaws.com/stg_chime_backend_php:latest'",
          "runuser -l ec2-user -c 'docker run --restart unless-stopped -d --name ws -p 6001:6001 533267253502.dkr.ecr.ap-northeast-1.amazonaws.com/stg_backend_php:latest'"
        ]
      }
    }
  ]
}

Nhấn Create Document để hoàn tất.


5. Kiểm Tra và Debug

5.1. Kiểm Tra EventBridge Rule

Vào Amazon EventBridge → Rules → Chọn stg-chime-backend-image-pushed → Metrics để kiểm tra số lần rule được kích hoạt.

5.2. Kiểm Tra Systems Manager Automation

Vào AWS Systems Manager → Automation → Execution history, kiểm tra lịch sử thực thi của deployWebsocketSTGCommand.

5.3. Kiểm Tra Trên EC2

Đăng nhập vào EC2 bằng SSH và chạy các lệnh sau:

# Kiểm tra container có chạy hay không
sudo docker ps

# Kiểm tra logs của container
sudo docker logs ws

Nếu có lỗi, kiểm tra lại xem EventBridge có trigger đúng rule hay không.


6. Tổng Kết

Với giải pháp này, chúng ta đã thiết lập một quy trình CI/CD tự động để triển khai container lên EC2 khi có image mới được đẩy lên ECR.

Ưu điểm: ✅ Tự động triển khai khi có update mới. ✅ Không cần SSH vào EC2 để deploy thủ công. ✅ Sử dụng EventBridge và SSM giúp tăng bảo mật và dễ quản lý.

Bạn có thể mở rộng giải pháp này để triển khai nhiều service khác nhau bằng cách thay đổi tên repository và cổng container.

Chúc bạn triển khai thành công! 🚀

Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough

Bài viết liên quan

Learning English Everyday