This document covers steps on setting up this repository on various cloud hosting providers.
Deploying on AWS requires a basic understanding of the following tools and services:
- Docker
- GitHub actions & workflows
- GitHub environments and secrets
- AWS Elastic Container Registry (ECR)
- AWS Elastic Container Service (ECS)
- AWS Virtual Private Cloud (VPC)
- AWS Fargate
- AWS Elastic Load Balancer (ELB)
- AWS Elastic IPs
- AWS Identity and Access Management (IAM)
- AWS Relational Database Service (RDS)
An overview of how the continuous delivery cycle works in Plio with GitHub action and Amazon ECR & ECS.
Setting up staging environment on AWS is pretty straightforward.
-
Login to your AWS console.
-
Go to VPC. (skip this step if you've already created a VPC when setting up backend repository)
- Create a new VPC.
- Name it
plio-staging
. - In IPv4 CIDR block, enter
10.0.0.0/16
- this will reserve 255 * 255 IPs within the VPC. - Click on the create button. You will see the new VPC under the list of VPCs.
- Check out this AWS guide for more details on VPC.
- You'll need to attach an Internet Gateway to this VPC.
- Click on
Internet Gateways
in the VPC Dashboard. - Select
Create internet Gateaway
. - Name it as
plio-staging
and save it. - Click on
Attach to a VPC
in the next page and select the VPC created above.
- Click on
- Next, you'll need to attach Subnets to the VPC created above.
- Click on
Subnets
in the VPC Dashboard. - Click on
Create Subnet
. - Choose the VPC created above as VPC ID.
- Enter the
Subnet name
asplio-staging-1
. - Either choose the
Availability Zone
if you have a preference or leave it to the default - Under
IPv4 CIDR block
, add a range of IPv4s that belong to your subnet. If you followed the steps above exactly, you can set this value as10.0.0.0/24
. This will reserve the IPs10.0.0.0
to10.0.0.255
to this Subnet. - If you want to, you can create more subnets using
Add new subnet
but it's not needed going forward. If you do choose to do so, you'll need to choose a different non-overlapping range of IPv4s for theIPv4 CIDR block
option - for example, you could set it to:10.0.1.0/24
to reserve the IPs10.0.1.0
to10.0.1.255
. - Finally, create the subnet.
- Click on
- You need to update your
Route Tables
to give make your subnets publicly accessible.- Click on
Route Tables
in the VPC Dashboard - Select the entry corresponding to the VPC you created above.
- Navigate to the
Routes
tab. - Click on
Edit routes
. - Click on
Add route
. - Add
0.0.0.0/0
as theDestination
. - Select
Internet Gateway
underTarget
and link to the Internet Gateway you created above. - Click on
Save routes
.
- Click on
-
Create a new Elastic IP by going to EC2 dashboard and navigating to the
Elastic IP
section.- Click on
Allocate Elastic IP address
and click onAllocate
. - You will see a new IP address in the IPs list. Name it
plio-frontend-staging
. - This will be used in later steps to give the load balancer a permanent IP address.
- Click on
-
Go to Target groups.
- Create a new target group.
- Choose target type to be
IP addresses
. - Name the target group as
plio-frontend-staging
. - Set the protocol to
TCP
and port to80
. - Select the
plio-staging
for the target group VPC. - In the next step, add an IP address in the
IP
text area - the IP address should belong to your VPC - if you followed the steps above exactly, you can use any IP address between10.0.0.0
to10.0.255.255
. - Proceed to create target group. You will see target group in the list.
-
Go to Load Balancers (LBs).
- Create a new load balancer.
- Select
Network Load Balancer
option. We use NLB for easy support of web socket connections. - Name the LB as
plio-frontend-staging
. - Select the
plio-staging
for the load balancer. - In the subnet mappings, check the first desired zone and use Elastic IP under IPv4 settings for that subnet.
- Under listeners and routing, select the target group
plio-frontend-staging
for TCP port 80. - Proceed to create the load balancer. You will see the created load balancer in the list of all load balancers.
-
Go to ECR and create a new repository named
plio-frontend-staging
and set the settings as per your needs. -
Now go to ECS and create a new task definition.
- Select Fargate and name the task definition as
plio-backend-staging
- Set the task role as
ecsTaskExecutionRole
. - Set the task memory and task CPU based on your needs. Good defaults to use are: 2 GB for memory and 0.5 vCPU.
- Create a new container with name
plio-frontend-staging
. - In the image field, you can just type in
image_arn
. This is not a valid entry and just a placeholder for now as it'll be replaced by the actual image ARN once the GitHub workflow triggers. - Enter port
80
in the port mapping field. - Save the container definition and the task definition.
- You will see the new task definition within the list of all task definitions.
- Select Fargate and name the task definition as
-
Under ECS, go to
Clusters
and create a new cluster with nameplio-staging-cluster
. (skip this step if you've already created a Cluster when setting up backend repository)- Use
Networking only
option. We will go with serverless deployment so that we don't worry about managing our own server instances. - Don't create a new VPC for your cluster. We'll use the VPC created in previous step in the next step of creating a service.
- Click on the create button.
- You will see the new cluster within the list of clusters.
- Use
-
Get into
plio-staging-cluster
and create a new service.- Set launch type to Fargate. We'll use serverless deployments for Plio.
- Name the service as
plio-frontend-staging
. - Under task definition, select
plio-frontend-staging
and use latest revision. - Set the number of tasks to be one.
- Service type to be
REPLICA
. - Minimum healthy percentage should be 100 and maximum percent to be 200. Minimum healthy percentage defines the percentage of tasks that should be running at all times. Maximum percent defines how many additional tasks the service can create in parallel to running tasks, before killing the running tasks.
- Deployment type to be
rolling update
. - Keep other values as default.
- Use the Cluster VPC and the subnet that you configured previously with Elastic IP.
- Set Auto-assign public IP to
ENABLED
. Otherwise, it makes the task accessible only through VPC and not public. - Under load balancing, select the
Network Load Balancing
option and select theplio-frontend-staging
load balancer. - Inside
Container to Load Balancer
, click onAdd to load balancer option
and selectplio-frontend-staging
in the target group. - For auto-scaling, go with
Do not adjust the service's desired count
for staging. - Review and create the service.
-
Next, go to your GitHub repository and create a new environment from settings tab.
- Name the environment as
Staging
. - Make sure you have added the following GitHub secrets on repository level. If not, add these as your environment secrets.
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_REGION
- Additionally, refer to
.env.example
file to set all the other required environment variables for your project.
- Name the environment as
-
We are using Github Actions to trigger deployments. You can find the workflow defined in
.github/workflows/deploy_to_ecs_staging.yml
. It defines a target branch such that a deployment is initiated whenever a change is pushed to the target branch. -
Once done, push some changes to the target branch so that the GitHub workflow
deploy_to_ecs_staging.yml
gets triggered.
Setting up a production environment on AWS is same as staging. Take care of the following things:
- Rename all services as
plio-frontend-production
or a similar naming convention. - Go with auto-scaling option when creating a new service from ECS.
- When creating a service or when updating it, navigate to the service auto-scaling section.
- Select
Configure Service Auto Scaling to adjust your service's desired count
. - Set minimum number of tasks to
1
. This is the minimum count of running tasks when scale-in operation happen. - Set desired number of tasks to
1
. This may come pre-populated if you had set it before. - Set maximum number of tasks to
2
or more based on your desired need. This is the maximum count of total running tasks in case of scale-out operation. - In IAM role for Service Auto Scaling, select
AWSServiceRoleForApplicationAutoScaling_ECSService
. - Click on
Add scaling policy
. - Select policy type to
Target tracking
. - Set policy name to
plio-frontend-production-autoscaling-cpu
- In ECS service metric, select the option for average CPU utilization.
- Target value should be
60
(or as per your requirement). This is the threshold value when the service will trigger a new event and perform scale-out operation. - Scale-out & Scale-in cooldown period to be
300
seconds. Disable scale-in
to remain unchecked.- Save the scaling policy.
- Create or update the service name.
- Use k6.io or other load testing tool to check if auto-scaling is working fine or not. You can lower down the target threshold for testing purposes.