image block
869 Views 0 Comments
Hi guys!
I hope you miss me a bit :) Now I'm here.
And today I'm gonna to explain the simplest way to implement CD process inside Amazon Web Services. So let's start.


AWS CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances.
And it's actually cool because there are no needed to install additional soft for developing and maintaining our applications on the cloud. And also there is no limit on application content (configuration, multimedia or executable files) and big scope of sources.
As a source, we can use any provided by AWS systems: AWS S3, GitHub or Bitbucket repositories. In this case, we gonna use simple script by installation of Apache on our EC2 hosts and run simple start page from GitHub repository.
One more not less important thing which we will use on this post is IAM.
AWS Identity and Access Management (IAM) - is management system which give you possibility to control the access to AWS services and resources securely. Functionality of this system provide management not only for individual users, but for groups. And due them we can specify any role, any permission and sharing or denied any resources.
So, let's get start to configuring the system.

Users and roles

First what we gonna do, it's creation of two roles which we will use. The first role will use for contacting with our source (in our case it will be GitHub) and the second one which will working with our EC2 machines.
To do this, click on services of AWS and select IAM.

There is general page of IAM service. For creation new role, choose Roles and push on Create role button. Policies we will create during this process.
Specify that we are going to create role for our CodeDeploy on EC2 instances.

And in this step we will create new policies for out user, so click on "Create Policy".

You can choose any needed roles on list given by AWS, but for more clear understanding, better use direct specified permissions by JSON. To do this, click on JSON tab and specify needed permissions.
My example of these permissions you can download here (Policy Permissions).
And you will see confirmation that your policy has been created.

Great. We can continue creation of our role. The next step is specifying the name and the description to role, after this, we will get it on our AWS system.
Please do the same for the second role with permissions on installation our code on EC2 instances. My example you have find here (Policy Permissions).
Awesome! Now we have two roles, which we will use on our AWS Deploy. One role for manipulating with EC2 instances and one for working with CodeDeploy.
But we have to do one more thing. We should add "Trusted policy" to these roles. These rules are showing, to which AWS services you are giving access to this role. Without them, you will see the error like this :
To add these trust policies, just click on your role, go to tab "Trust relationships" and click on edit button.
My example of these policies, you can find on files specified before in section "Policy Trust Permissions".
Now we have two working roles and we can move on.

AWS CodeDeploy

Ok, and in the start of configuring our CodeDeploy, we should create EC2 instance. If you don't know how to do this, please read first part of introduction to AWS by this link : Amazon Web Services Part 1 - Monitoring
The next step is installing of CodeDeploy agent on our node. To do this, please login on host and run these commands ( in our case is Centos OS ) :
yum -y update .   # getting latest updates of os
yum install -y aws-cli    # installing aws command line libraries  
aws configure    # and configuring them
aws s3 cp s3://aws-codedeploy-us-east-1/latest/install . --region us-east-1  # setup location and access
chmod +x ./install    # giving execute permissions on install script
./install auto    # installing CodeDeploy agent
Now we have installed and configured CodeDeploy agent on our machine. So, like the last step here, we need to start it and ensure that it's running.
systemctl start codedeploy-agent
systemctl status codedeploy-agent
And after this you should see the process of agent, like present on my screenshot.

Nice! Let's create our first deployment.
In this post, we are going to automate process of installing Apache on our new instances, and also changing index.html file on our. How we can do this? Let's move on and will see :)
Go on our AWS panel to CodeDeploy and click on "Get Started Now" button.
And here we are, on page of creation of new deployment.
On next page, you can choose the type of deployment via CodeDeploy, and of course if you want, you can choose with more detailed manual and less flexible type "Sample" type of deployment, but for us needed "Custom" type, so choose this one and move on.

On next page, you should specify the name of your deployment and group. And lower you will have table with EC2 instances, where you can choose any instance (or group instances by tags) on which one you want to implement CodeDeploy.
On this step, please have a more attention, because if you will choose tag witch are connected to few of your EC2 instances, that the changes will be implemented on all of them, when you will run deployment.
On deployment configuration, you can choose on from 3 default types of deployment, Due selected type, the installation on your EC2 hosts, will be one by one or in parallel so on.

On services role, choose our created before EC2Deploy role.

And here we go! We have created our first deployment. Cool!


Now we can try to deploy our application. So, click on the "Actions" and "Deploy new revision".

Like a source, for this post, we will use GitHub, but you also can connect your AWS S3 database.
You should connect your GitHub account to AWS, and like repository of source and commit ID, specify needed your data, but we're going to use data from my GitHub account, so you can just copy it, and have a look how it works.
Like repository name you should specify GitHub repo name and also owner of this repo. And on commit space paste just last commit, you can find it here.

And when we click on deploy button, it will deploy our application on EC2 hosts.

Let's check our EC2 instance.

Amazing! Everything is working! But how it know, what should be done?
Logic of CodeDeploy is very simple. It's trying to get needed source (in our case this is GitHub), and get "init" file, which in CodeDeploy case, should be named appspec.yml. The same file is stored on GItHub which we specified, on root directory. There is example of it.
version: 0.0
os: linux
   - source: /CodeDeploy/
     destination: /var/www/html
   - object: /var/www/html
     pattern: "**"
     owner: apache
     group: apache
     mode: 755
       - file
    - location: CodeDeploy/scripts/
      runas: root
    - location: CodeDeploy/scripts/
      runas: root
    - location: CodeDeploy/scripts/
runas: root

Here we can specify files, which we want to copy on server, permissions on any resources inside EC2 and like hooks, which will be running on EC2 machines, we specifying steps, BeforeInstall and AfterInstall. And that's it! We can specify any needed script which we want to see on our EC2 instances, and everything other will be done by CodeDeploy.

GitHub Hooks

And last thing, which I want to discuss today, it's GitHub Hooks.
The main point of GIT hooks, it's implementing some actions, after each commit on repository. And in our case, we are able to make CodeDeploy deployment on each hook on our repo. Few minutes of configuring, will give for us simple CD process, which we can use where we want on AWS.
To do this, go to your repository, click on settings and choose "Integrations & services".

And in provided fields, just specify your credentials to AWS.

Now, in each commit on this repo, CodeDeploy will implement changes on your EC2 hosts automatically.

Resources from GitHub : CodeDeploy

I wanna thanks everyone who watching my posts and I really hope, that this is information can be useful for you guys. Because I'm doing this posts, in format, like I'd wanted to get it. So, hope you enjoyed it. Thanks and bye bye :)


    Leave a Comment