Configuring AWS Code Deploy

Written by Jai

3 min read ·

Here I describe how we could use AWS Code deploy to transfer your code to an EC2 server for deployment. AWS code deploy can be used for much more, but that is not the topic of this blog.

If you have a single server (and not a cluster) you could do a much simpler deployment by using SSH and shell scripts.

For AWS Code deploy to transfer the code to your production or staging server. You need to do the following

AWS Code Deploy Setup

  1. EC2 instance should have an IAM profile which allows it to access S3. For a new EC2 instance which has to be launched, follow the steps in this link For an existing EC2 instance, run the below command:
aws ec2 associate-iam-instance-profile --instance-id i-xyzabcdef --iam-instance-profile Name=Myapp-Instance-Profile

aws ec2 describe-iam-instance-profile-associations

The above is described in more detail in this link

  1. The EC2 instance should have the AWS code deploy agent running, you can get running by doing the following on a unix server. This downloads the code deploy from the ap-south-1 region.
wget https://aws-codedeploy-ap-south-1.s3.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
Now check status:
sudo service codedeploy-agent status

The above steps are further described in this link.

  1. An AWS Code deploy APP would need to be created. You will need to create a deployment group within the app. The deployment group will reference the ec2 instance above. This should be straightforward.
  1. You would need to create a policy which allows code deploy to access s3.
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:ListAllMyBuckets","s3:PutObject"],
      "Resource": "arn:aws:s3:::*"
    },
   {
     "Effect": "Allow",
     "Action": ["codedeploy:*"],"Resource": "*"
   }
 ]
}

Setup on your code structure

On your code structure, you would need to create an appspec.yml file which could contain commands which you need to execute on deployment.

An appspec yaml file is as simple as shown below, this tells AWS code deploy to run the installCode.sh shell script for Application Start. Like the ApplicationStart tag below, there are other life cycle hooks available.

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ec2-user/build
hooks:
  ApplicationStart:
    - location: installCode.sh
      timeout: 300
      runas: root

A Sample appspec.yml file, can be found in this link

Thats about it, for the setup. To push code to your EC2 instance you would have to run the below commands. [Assuming you have the aws cli and keys installed on your system]

$ aws deploy push --application-name <application-name> --s3-location s3://<bucket name>/<any build name> --source <directory>

$ aws deploy create-deployment --application-name <application-name> --s3-location bucket=<bucket-name>,key=<any build name>,bundleType=zip,eTag=<tag from deploy command> --deployment-group-name <deployment group name>

Hope this was useful. You could write to me at jaikant@gmail.com

Enjoyed this post? Receive the next one in your inbox!
Jaikant Kumaran
Jai

Jai is passionate about social issues and causes. He is the founder of sherpafeet and Tech47.

Copyright © 2018 Tech47.