Deploy app to AWS
Usage services
- AWS EC2
- AWS IAM
- AWS Developer Tools(Code Pipeline, Github connection, Code Build, Code Deploy)
Tutorial
-
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.
-
Các bước cấu hình.
-
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. Providerchọ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ọnCreate - 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
- Cách 1: Dùng connection
-
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 platformlàEC2-> ClickCreate application - Truy cập vào
applicationvừa tạo -> ChọnDeployment groupsmenu -> ChọnCreate 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
-
2.3. Config source code
- Code build: Tạo một file buildspec.yml
- Code deploy: Tạo một file appspec.yml
-
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
-
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
- Environment image:
- Service role: Chọn new -> đặt tên
- Buildspec:
Use a buildspec file-> đường dẫn đến filebuildspec.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 namevàDeployment groupđược tạo ở bước 2.2 - Chọn
Next
- Chọn
- 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)
-
Need to prepare:
- Source code.
- EC2 instance to host the source code.
- Database (RDS instance).
- IAM Role:
- Create a role with the
AWSCodeDeployRolepolicy (AWS managed type), with Trust relationships as CodeDeploy service
-
Configuration steps:
-
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
Provideras 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.
- Method 1: Using connection
-
2.2. Creating a CodeDeploy Application
- Access {region}.console.aws.amazon.com/codesuite/codedeploy/applications
- Select
Create application - Name the application and select
compute platformasEC2-> ClickCreate application - Access the newly created
application-> Select theDeployment groupsmenu -> SelectCreate 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 balancingotherwise, leave it unchecked - Leave the rest at default settings
- Select
Create deployment group
-
2.3. Configuring source code
- Code build: Create a file buildspec.yml
- Code deploy: Create a file appspec.yml
-
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
-
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 -> Select
Next - 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
- Environment image:
- 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 nameandDeployment groupcreated in step 2.2 - Select
Next
- Select
- Review and select
Create pipeline