Skip to content

CICD Setup for EC2

Juhi edited this page May 25, 2020 · 14 revisions

Below is the process to setup continuous deployment to AWS EC2 instance with source code in github.

Create IAM roles (if not already created)

  • CodeDeploy Service Role -- This role gives codedeploy access to target instances (EC2 / lambda). It also provides cloudwatch, SNS access for alerts & notification
    • Go to IAM roles and click on create role
    • Select CodeDeploy service and CodeDeploy use case for deployment to EC2
    • Add tags if required, give appropriate name and create the role
  • EC2 Role -- This role gives code deploy agent installed on EC2 instance, access to read S3 srtifacts
    • To understand where S3 came into picture, we need to understand how codedeploy works. Check this page for a short brief. <>
    • Create a new role selecting EC2 service with AmazonS3ReadOnlyAccess policy
    • Add tags if required, give appropriate name and create the role

2. Create / attach the target EC2 instance with the IAM role #2

3. ssh into the EC2 instance and run following code

 #!/bin/bash
 	
 # Installing CodeDeploy Agent
 sudo yum update
 sudo yum install ruby
 	
 # Download the agent (replace the region)
 wget https://aws-codedeploy-eu-west-3.s3.eu-west-3.amazonaws.com/latest/install
 chmod +x ./install
 sudo ./install auto
 sudo service codedeploy-agent status

The status should look something like this -- The AWS CodeDeploy agent is running with pid xxxx
Refer FAQ if you get 0 pid

4. To the github source code repository, add appsec.yml

  • Specify source of the file / directory that needs to be synced to AWS instance
  • Specify destination absolute path on the AWS instance where source needs to be synced
  • Add the dependency installation script in AfterInstall
  • Specify the instructions to start / reload server in AfterInstall
  • Select EC2 / On-premises as the compute platform and create the application

6. Create Deployment group

  • Under the created application, go to deployment groups tab
  • Add a name & select IAM role #1
  • Select in-place deployment for maintaining the instance (There is option to select blue-green deployment which will create new instances against a load balancer and de-register original ones)
  • Select Amazon EC2 instances with default deployment settings
  • For the simplest configuration, load balancing & rollbacks can be disabled

7. Create AWS CodePipeline

  • In the source provider, select github with

Clone this wiki locally