SaigonTech Vault
Loại: knowledge-doc 2026-09-29 active #wiki-import

Deploy app to AWS

Usage services

  • AWS EC2
  • AWS IAM
  • AWS Developer Tools(Code Pipeline, Github connection, Code Build, Code Deploy)

Tutorial

  1. Cần chuẩn bị

    • Source code.
    • EC2 instance để chứa source code.
    • Database(RDS instance).
    • IAM Role:
    • Tạo một role với policy AWSCodeDeployRole(AWS managed type), Trust relationships là codedeploy service.
  2. Các bước cấu hình.

  3. 2.1. Cấu hình nơi lấy source code.

    • Cách 1: Dùng connection
      • Truy cập {region}.console.aws.amazon.com/codesuite/settings/connections
      • Chọn Create connection.
      • Provider chọn GitHub.
      • Đặt tên cho connection rồi chọn Connect to GitHub
      • Nhập GitHub account để kết nối
    • Cách 2: Dùng Code Commit
      • Truy cập {region}.console.aws.amazon.com/codesuite/codecommit/repositories
      • Chọn Create repository.
      • Điền Repository name, Description, Tags -> Chọn Create
      • Còn lại sẽ như là một version control service, dùng để lưu trữ source code như GitHub #### -> Nên dùng cách 1 vì các projects thường lưu trữ ở GitHub, tạo connection thẳng đến GitHub để tracking sẽ dễ dàng hơn, tránh trường hợp phải quản lý source ở nhiều nơi
  4. 2.2. Tạo Code Deploy Application

    • Truy cập {region}.console.aws.amazon.com/codesuite/codedeploy/applications
    • Chọn Create application
    • Đặt tên và chọn compute platform là EC2 -> Click Create application
    • Truy cập vào application vừa tạo -> Chọn Deployment groups menu -> Chọn Create deployment group
    • Đặt tên
    • Role: Role được tạo ở bước chuẩn bị
    • Deployment type: In-place
    • Environment configuration: Amazon EC2 instances
      • Key-Value ứng với Name của EC2 sẽ deploy tới
    • Nếu có sử dụng load balancer thì chọn Enable load balancing, không thì bỏ tick
    • Phần còn lại cứ set theo mặc định
    • Chọn Create deployment group
  5. 2.3. Config source code

  6. 2.4. Cấu hình agent cho EC2

    • Làm theo các bước: https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-linux.html
  7. 2.5. Cấu hình Code Pipeline

    • Truy cập {region}.console.aws.amazon.com/codesuite/codepipeline/pipelines
    • Chọn Create pipeline
    • Pipeline settings
    • Nhập Pipeline name
    • Pipeline type: V1
    • Service role: New service role
    • Nhập Role name
    • Còn lại set default -> Chọn Next
    • Source provider
    • GitHub (Version 2), Code commit đối với bước 1(cách 2)
    • Chọn connection được tạo ở bước 1(cách 1)
    • Chọn repo cần được deploy
    • Trigger là Push in a branch -> một khi commit tới branch chỉ định thì pipeline sẽ được kích hoạt
    • Branch name: chỉ định branch sẽ được deploy
    • Còn lại set default -> Chọn Next
    • Build provider
    • AWS CodeBuild
    • Chọn region
    • Chọn Create project
      • Đặt tên
      • Environment: chọn env phù hợp với project, ở đây sẽ dùng linux
        • Environment image: Managed image
        • Compute: EC2
        • Operating system: Amazon Linux, Runtimes: Standard, Image: ...standard:5.0
      • Service role: Chọn new -> đặt tên
      • Buildspec: Use a buildspec file -> đường dẫn đến file buildspec.yml ở bước 2.3
      • Còn lại set default -> Chọn Continue to CodePipeline
      • Build type: Single build
      • Chọn Next
    • Deploy provider
      • Chọn AWS CodeDeploy
      • Chọn Region, Application name và Deployment group được tạo ở bước 2.2
      • Chọn Next
    • Review lại và chọn Create pipeline

Rườm rà quá hả, vậy thì xem thằng này: https://wiki.sgt-dev.space/65870047a2928a2b6cc4a413

Tutorial (English version)

  1. Need to prepare:

    • Source code.
    • EC2 instance to host the source code.
    • Database (RDS instance).
    • IAM Role:
    • Create a role with the AWSCodeDeployRole policy (AWS managed type), with Trust relationships as CodeDeploy service
  2. Configuration steps:

  3. 2.1. Configure where to get source code.

    • Method 1: Using connection
      • Access {region}.console.aws.amazon.com/codesuite/settings/connections
      • Select Create connection
      • Select Provider as GitHub
      • Name the connection and select Connect to GitHub
      • Enter GitHub account to connect
    • Method 2: Using Code Commit
      • Access {region}.console.aws.amazon.com/codesuite/codecommit/repositories
      • Select Create repository
      • Fill in Repository name, Description, Tags, then select Create
      • The rest will be like a version control service, functioning similarly to GitHub for storing the source code ###### -> It's advisable to use method 1 since projects typically store code on GitHub. Establishing a direct connection to GitHub for tracking purposes will be more straightforward, avoiding the need to manage the source in multiple places.
  4. 2.2. Creating a CodeDeploy Application

    • Access {region}.console.aws.amazon.com/codesuite/codedeploy/applications
    • Select Create application
    • Name the application and select compute platform as EC2 -> Click Create application
    • Access the newly created application -> Select the Deployment groups menu -> Select Create deployment group
    • Name the deployment group
    • Role: Use the role created in the preparation step
    • Deployment type: In-place
    • Environment configuration: Amazon EC2 instances

      • Key-Value corresponding to the Name of the EC2 instance to be deployed to
    • If using a load balancer, select Enable load balancing otherwise, leave it unchecked

    • Leave the rest at default settings
    • Select Create deployment group
  5. 2.3. Configuring source code

  6. 2.4. Configuring the agent for EC2

    • Follow the steps outlined here: https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-linux.html
  7. 2.5. Configuring CodePipeline

    • Access {region}.console.aws.amazon.com/codesuite/codepipeline/pipelines
    • Select Create pipeline
    • Pipeline settings
    • Enter Pipeline name
    • Pipeline type: V1
    • Service role: New service role
    • Enter Role name
    • Leave the rest as default -> SelectNext
    • Source provider
    • If in step 2.1 you use method 1: GitHub (Version 2), Code commit
    • If in step 2.1 you use method 2: Select the previously created connection
    • Select the repository to be deployed
    • Trigger: Push in a branch -> when a commit is made to the specified branch, the pipeline will be triggered
    • Branch name: pecify the branch to be deployed
    • Leave the rest as default -> Select Next
    • Build provider
    • AWS CodeBuild
    • Select region
    • Select Create project
      • Name the project
      • Environment: choose an env suitable for the project; in this case, using Linux
        • Environment image: Managed image
        • Compute: EC2
        • Operating system: Amazon Linux, Runtimes: Standard, Image: ...standard:5.0
      • Service role: Select new -> name it
      • Buildspec: Use a buildspec file -> path to the buildspec.yml file from step 2.3
      • Leave the rest as default -> Select Continue to CodePipeline
      • Build type: Single build
      • Select Next
    • Deploy provider
      • Select AWS CodeDeploy
      • Select Region, Application name and Deployment group created in step 2.2
      • Select Next
    • Review and select Create pipeline