DevOps Training
  • DevOps
  • What is DevOps
    • What DevOps Look like
    • Why DevOps Needed
    • DevOps Automation Tools
    • DevOps Principles
  • cloud computing
    • How DevOps links with Cloud Computing
    • What is cloud computing?
      • Platform as a service (PaaS)
      • Infrastructure as a service (IaaS)
      • Software as a service (SaaS)
      • Function as a Service
      • SaaS, PaaS, IaaS
  • Version Control
    • Git as Version Control
      • Setting up Remote Repo
      • Git Hooks
      • github vs gitlab vs bitbucket
      • Quick Recap Git
  • DevOps #01 Continuous Integration
    • Continuous Integration & Continuous Delivery
      • Understanding CI Tools
      • Prerequisite
      • Continuous Integration
      • CI Tools
      • Travis-CI
        • Travis CI with S3
        • Static Site Build S3
        • Beanstalk with AWS CLI
          • Elastic Beanstalk with Travis CI
        • Travis using Code Deploy EC2
          • Github and Code Deploy
          • Travis CI with Code Deploy
      • Gitlab-CI
        • CI Setup for application
        • Gitlab Runners on EC2
        • CI Integration with AWS
          • Deploying App using Gitlab CI
          • Gitlab CI with AWS S3
          • Gitlab CI with ECS
          • CI Integration with EC2
            • Update and Clean Gitlab.yml
        • Install Gitlab on EC2
      • CI/CD using Jenkins CI
        • Jenkins Build on EC2
        • Jenkins Build EC2 Ubuntu
        • Jenkins CI/CD
          • Create a Build Item
          • Create a Build Pipleine
            • Pipeline Using Docker
            • Pipeline Examples
          • Jenkins CI with S3
            • Jenkins CI - S3
          • Jenkins CI with EC2
    • Jenkins CI Cluster Mode
    • AWS Code Pipeline CI/CD
      • AWS CI/CD Tools
        • AWS Code Build
        • AWS Code Deploy to Beanstalk
        • AWS Code Deploy to EC2
        • AWS Pipeline - Example CI/CD
  • Docker
    • Docker
      • Docker for Developers
        • Install and setup
        • Docker Commands
        • Docker Images Container
        • Docker Architecture
    • Docker Demos
      • Node JS Container
    • Docker-compose
      • Using Docker Compose
      • Docker Compose Demo
  • AWS Quick Refresh
    • AWS Quick Recap - Videos
    • AWS Quick Recap
  • AWS Architecture - Lab
    • Application Deployment - 01
    • Application Deployment - 02
    • Application 3 tier Architecture
  • Basic Networking
    • Computer Networking for Beginners
      • Basic of Networking
      • Networking Protocols
      • OSI Model
      • Network address and Host address
      • Subnetting Type
    • Network Architecture
    • Networking Layers OSI Model
    • Internet protocol
      • CIDR and subnetting
        • Examples
      • AWS VPC Subnets
  • VPC and Networking
    • AWS VPC
    • VPC Demo
      • Bastion Host | Jump Server
  • AWS Components
    • AWS Components In Depth
      • AWS Storage
        • AWS EBS
        • AWS Cloudfront
        • AWS S3
      • AWS Compute
        • ECS
        • AWS VPC
          • VPC Components
        • AWS EC2
        • AWS ELB
          • Application Load balancer
            • Example
        • AWS EC2 Auto Scaling
          • Demo
        • AWS Route 53
        • AWS Lambda Serverless
          • AWS Lambda Serverless Computing
  • Assignments
    • Assignment 01-Node JS app on EC2
    • Assignment 02-Node JS with Mysql
    • Assignment-03
  • Microservices
    • Microservices Architecture
      • Docker and Docker-Compose
      • Docker-Compose Example 01
      • Docker-Compose Example 02
      • Hand-on | Building Microservices
    • Architecture Components
  • AWS ECS
    • AWS ECS
      • Introduction of ECS
Powered by GitBook
On this page
  • Code deploy to EC2 instance using Code deploy Agent
  • Now Lets build Pipeline Step by Step
  • Code deploy application

Was this helpful?

  1. DevOps #01 Continuous Integration
  2. AWS Code Pipeline CI/CD
  3. AWS CI/CD Tools

AWS Code Deploy to EC2

PreviousAWS Code Deploy to BeanstalkNextAWS Pipeline - Example CI/CD

Last updated 5 years ago

Was this helpful?

Code deploy to EC2 instance using Code deploy Agent

Let’s start first by creating 2 IAM roles we will use in this tutorial:‌

  • IAM role for CodeDeploy to talk to EC2 instances.

  • IAM role for EC2 to access S3.

1 — CodeDeployRole‌

2 — EC2S3Role‌

Create another IAM role, but this time choose EC2 as the trusted entity. Then, attach “AmazonS3ReadOnlyAccess” policy:​‌

Now that we’ve created an IAM roles, let’s launch an EC2 instance which will be used by CodeDeploy to deploy our application.‌

3 — EC2 Instance‌

Launch a new EC2 instance with the IAM role we created in last section:​ and add proper tag on EC2 instance.

Next to User Data type the following script to install the AWS CodeDeploy Agent at boot time:Plain Text exit: ⌘↩

#!/bin/sh
 yum update
 yum install -y ruby
 cd /home/ec2-user
 aws s3 cp s3://aws-codedeploy-us-west-1/latest/install .
 chmod +x ./install
 ./install auto

Note: make sure to allow HTTP traffic in the security group.​‌

Once created, connect to the instance using the Public IP via SSH, and verify whether the CodeDeploy agent is running:​‌

4 — Application‌

Add the appspec.yml file to the application to describe to AWS CodeDeploy how to manage the lifecycle of your application:Plain Text exit: ⌘↩

‌version: 0.0
os: linux
files:
  - source: /
    destination: /home/ec2-user/api
permissions:
  - object: /
    pattern: "**"
    owner: ec2-user
    group: ec2-user
hooks:
  AfterInstall:
    - location: deploy/before_install.sh
      timeout: 1200
      runas: ec2-user
  ApplicationStart:
    - location: deploy/restart.sh
      timeout: 60
      runas: ec2-user
ValidateService:
    - location: deploy/validate.sh
      timeout: 60
      runas: ec2-user

Branch - travis-ci-codedeploy-ec2

Now Lets build Pipeline Step by Step

  1. Create Pipeline and name it as Code-deploy-ec2-node and ass source provider as Github

Add Build Stage Next after setting up Source Repository with Branch, From here create new Build Project

Add Build configuration with default build spec file which says just copy node JS code to Artifacts

finally add this build as a Stage to Pipeline

Add deployment PROVIDER as code deploy and Configure application and return back to same screen (Create code deploy application and deployment group )

Code deploy application

Crate code deploy application and code deploy group

Code deploy can push code to EC2 instance so lets spin up EC2 instance having S3 read only access role

In User data we can add installation commands to add code deploy agent to EC2 instance as startup script

Define Tag for EC2 instance so these EC2 instance can be provided as Target for Code deploy stage

Create Deployment Group

Once Code deployment created add Code deploy to our pipeline Last step

Now Our Pipeline has ben setup with Code Build and Code deploy to push Node JS Code TO ec2 instance

Trigger a dummy Commit to run pipeline

Here we see everything worked as expected and we are able to see application deployed on EC2 instance

Go to then navigate to “Roles“, choose “Create New Role“, Select “CodeDeploy” and attach “AWSCodeDeployRole” policy:​‌

AWS IAM Console
LogoGitHub - tkssharma/DevOpsTraining: DevOpsTrainingGitHub