<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abuchi Kingsley</title>
    <description>The latest articles on DEV Community by Abuchi Kingsley (@abuchikings).</description>
    <link>https://dev.to/abuchikings</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F268474%2Fd3cb9aa0-3de9-4cad-9ed3-6e699adc89eb.jpg</url>
      <title>DEV Community: Abuchi Kingsley</title>
      <link>https://dev.to/abuchikings</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abuchikings"/>
    <language>en</language>
    <item>
      <title>How To Automate Your Deployments To AWS EC2 Using CircleCI And Ansible</title>
      <dc:creator>Abuchi Kingsley</dc:creator>
      <pubDate>Sat, 21 Oct 2023 21:41:13 +0000</pubDate>
      <link>https://dev.to/abuchikings/how-to-automate-your-deployments-to-aws-ec2-using-circleci-and-ansible-14f0</link>
      <guid>https://dev.to/abuchikings/how-to-automate-your-deployments-to-aws-ec2-using-circleci-and-ansible-14f0</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;In today's world of fast paced releases and distributed teams working across flexible hours, the importance of automated deployments cannot be overstated. In this tutorial I will guide you through the steps to configure  CircleCi and Ansible for automated deployments to AWS EC2.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisite
&lt;/h3&gt;

&lt;p&gt;To get the most out of this tutorial and follow along it will be nice to have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic programming experience and be comfortable with the command line.&lt;/li&gt;
&lt;li&gt;A Github account and access to command line with git installed.&lt;/li&gt;
&lt;li&gt;An AWS account with EC2 server set up.&lt;/li&gt;
&lt;li&gt;A CircleCI account.&lt;/li&gt;
&lt;li&gt;A code editor.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This tutorial will focus on configuration and deployment only, we won't be setting up new servers. I will be using a backend server I wrote a while ago. You can find the starter code &lt;a href="https://github.com/AbuchiKings/aws_ec2_auto_deploy/tree/starter" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Fork and clone the repository. Switch to the &lt;code&gt;starter&lt;/code&gt; branch to follow along.&lt;/p&gt;

&lt;h3&gt;
  
  
  CircleCI Set-up
&lt;/h3&gt;

&lt;p&gt;Head over to &lt;a href="https://circleci.com" rel="noopener noreferrer"&gt;circleci.com&lt;/a&gt; and login. Ensure you are within your personal organization. On the "Projects" page search for &lt;code&gt;aws_ec2_auto_deploy&lt;/code&gt; repo and just click "Set Up Project". Select "Use the &lt;code&gt;. circleci/config.&lt;/code&gt; yml in my repo" option and set up project. Set up your project environment variables by copying the default values in &lt;code&gt;.env.example&lt;/code&gt; file over to &lt;code&gt;Project Settings &amp;gt; Environment Variables&lt;/code&gt; on CircleCI project dashboard. Now, let's get down to brass tacks.&lt;/p&gt;

&lt;h4&gt;
  
  
  Writing &lt;code&gt;config.yml&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;On your editor, open the &lt;code&gt;config.yml&lt;/code&gt; file inside &lt;code&gt;.circleci&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;CircleCI uses YAML files to configure pipelines which is usually made up of the following blocks of YAML codes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;YAML Block&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;version&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;This specifies the version of CircleCI API you wish to use&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;commands&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Contains list of reusable commands&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;orbs&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Makes it possible to use certain prewritten functionalities in a job&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jobs&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;List of jobs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;workflow&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Defines the order to run jobs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Before deploying your code to the servers, it is good practice to always ensure that all tests are passing. Update your &lt;code&gt;config.yml&lt;/code&gt; file with the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: 2.1

jobs:
  test:
    docker:
      - image: cimg/node:16.17.0
      - image: redis
        name: redis
      - image: mysql:8.0
        environment:
          MYSQL_ROOT_PASSWORD: 12345
          MYSQL_DATABASE: lend
          MYSQL_USER: abuchikings
          MYSQL_PASSWORD: 12345
    steps:
      - checkout
      - restore_cache:
          keys: [packages]
      - run:
          name: Test MYSQL connection
          command: |
            for i in `seq 1 30`;
            do
              nc -z 127.0.0.1 3306 &amp;amp;&amp;amp; echo Success &amp;amp;&amp;amp; exit 0
              echo -n .
              sleep 1
            done
            echo Failed waiting for MySQL &amp;amp;&amp;amp; exit 1

      - run:
          name: Run tests
          command: |

            npm install
            npm run migrate:latest
            npm run test

      - save_cache:
          paths:
            - "./node_modules"
          key: packages

workflows:
  default:
    jobs:
      - test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a break down of what is happening in the code above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;jobs&lt;/code&gt; keyword indicates that the code block is going to contain jobs.&lt;/li&gt;
&lt;li&gt;The very first job is named &lt;code&gt;test&lt;/code&gt; and the &lt;code&gt;docker&lt;/code&gt; keyword tells CircleCI that this job is going to be executed within a docker environment.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cimg/node&lt;/code&gt; is CircleCI's official nodejs image on docker hub. Node version 16.17.0 is used for this project. MySQL 8.0 database and redis are required to test the application.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;steps&lt;/code&gt;: As its name suggests defines the steps required to run a specific job. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;checkout&lt;/code&gt;: This checks out the source code from your version control repository.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;restore_cache&lt;/code&gt;: Restores cached dependencies. Caching is a mechanism that allows you to save and reuse dependencies (e.g., npm packages) to speed up the build process. In this case, it restores cached npm packages.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;run&lt;/code&gt;: This step runs a series of commands. It's responsible for running the actual CI tasks.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: This provides a descriptive name for this step, indicating that the following commands are related to running tests. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;command&lt;/code&gt;: Specifies the commands to be executed. The &lt;code&gt;|&lt;/code&gt; symbol is used to define a multi-line command block, allowing the execution of multiple commands within this step. The first command utilizes &lt;code&gt;nc&lt;/code&gt; (netcat) utility to check if a MySQL database server running on the localhost (127.0.0.1) and listening on port 3306 is available. The second command runs migration and automated tests on the code. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;save_cache&lt;/code&gt;: This is a built-in CircleCI command used to save cached dependencies or artefacts. It ensures that the specified files or directories are cached for future builds.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;workflows&lt;/code&gt;: This section defines one or more workflows. A workflow is a series of jobs that need to be executed in a particular order or under specific conditions. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you can commit your changes and push them to remote repository. Head over to CircleCI dashboard to see your pipeline run. &lt;/p&gt;

&lt;p&gt;Next update your &lt;code&gt;config.yml&lt;/code&gt; with the &lt;code&gt;setup_ansible&lt;/code&gt; job below. Remember to to also update your workflow as shown in the code snippet below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  setup_ansible:
    docker:
      - image: amazon/aws-cli
    steps:
      - checkout
      - run:
          name: Install dependencies
          command: |
            yum install -y tar gzip
      - run:
          name: Add Backend IP To Ansible
          command: |
            backend_ip=$(aws ec2 describe-instances \
            --query "Reservations[*].Instances[*].PublicIpAddress" \
            --filters "Name=tag:Name,Values=ec2_auto_deploy_test" \
            --output text)
            echo "$backend_ip" &amp;gt;&amp;gt; ~/project/.circleci/ansible/inventory.txt
            cat ~/project/.circleci/ansible/inventory.txt
      - persist_to_workspace:
          root: ~/
          paths:
            - project/.circleci/ansible/inventory.txt

workflows:
  default:
    jobs:
      - test
      - setup_ansible:
          requires: [test]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Remember to change &lt;code&gt;ec2_auto_deploy_test&lt;/code&gt; to the name of your EC2 server on AWS.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;setup_ansible&lt;/code&gt; job uses aws-cli to get the IP address of an EC2 server with name tag &lt;code&gt;ec2_auto_deploy_test&lt;/code&gt;. The IP address is saved to the &lt;code&gt;inventory.txt&lt;/code&gt; file within the Ansible folder and persisted to the workspace for use by the next job. Utilities &lt;code&gt;tar&lt;/code&gt; and &lt;code&gt;gzip&lt;/code&gt; are required to persist file to workspace. &lt;/p&gt;

&lt;p&gt;Before pushing your changes to github, head over to the project page on CircleCI =&amp;gt; Project Settings =&amp;gt; Environment Variables and add the following environment variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;AWS_SECRET_ACCESS_KEY&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AWS_DEFAULT_REGION&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AWS_ACCESS_KEY_ID&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;which can be obtained from your aws account.&lt;/p&gt;

&lt;p&gt;The final job configuration on the &lt;code&gt;config.yml&lt;/code&gt; file will be responsible for executing an Ansible playbook that uses ssh to access the server, pulls code from github and builds an updated docker image. See code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  deploy_server:
    docker:
      - image: python:3.11.6-alpine3.18
    steps:
      - checkout
      - add_ssh_keys:
          fingerprints: ["9f:66:26:48:fd:251:62:5d:73:c4:d5:90:2b"]
      - attach_workspace:
          at: ~/
      - run:
          name: Install dependencies
          command: |
            apk add --update ansible openssh-client
            eval $(ssh-agent -s)
      - run:
          name: Deploy Server
          command: |
            cd ~/project/.circleci/ansible
            echo "Contents  of the inventory.txt -------"
            cat inventory.txt
            ansible-playbook -i inventory.txt deploy-server.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On your CircleCI project settings page, go to &lt;code&gt;SSH Keys&lt;/code&gt;, add a new ssh key by copying and pasting the ssh key for your EC2 server. This will generate a key fingerprint. Copy the SSH key fingerprint and replace &lt;code&gt;fingerprints&lt;/code&gt; on the &lt;code&gt;deploy_server&lt;/code&gt; job.&lt;/p&gt;

&lt;p&gt;Also update your workflow with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;workflows:
  default:
    jobs:
      - test
      - setup_ansible:
          requires: [test]
      - deploy_server:
          requires: [setup_ansible]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Ansible Set-up
&lt;/h3&gt;

&lt;p&gt;Ansible is a powerful open-source automation tool and configuration management system that streamlines and simplifies the automation of routine IT tasks and the management of complex systems. It provides a framework for IT professionals to define and execute automation tasks through human-readable playbooks. &lt;/p&gt;

&lt;p&gt;With Ansible, you can automate the provisioning, configuration, deployment, and maintenance of servers, applications, and various infrastructure components. You should check out &lt;a href="https://docs.ansible.com/" rel="noopener noreferrer"&gt;Ansible docs&lt;/a&gt; for more on this interesting automation tool.&lt;/p&gt;

&lt;h4&gt;
  
  
  Writing A Playbook
&lt;/h4&gt;

&lt;p&gt;To automate Ansible, we use simple YAML files called playbooks to define automation tasks. The following shows the tree structure for the &lt;code&gt;Ansible&lt;/code&gt; folder which contains the playbook to be executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;├── ansible
│   ├── ansible.cfg
│   ├── deploy-server.yml
│   ├── inventory.txt
│   └── roles
│       └── deploy
│           └── tasks
│               └── main.yml
└── config.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a breakdown of each file and its function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ansible.cfg&lt;/code&gt;: A configuration file used to customize the behaviour of Ansible. This allows you to override built-in configuration settings like SSH connection options, default inventory file, define remote user privileges and so on.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;deploy-server.yml&lt;/code&gt;: This is an Ansible playbook. It defines a set of tasks or a task to be executed on remote hosts. In this project, I used roles to define playbook tasks.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;inventory.txt&lt;/code&gt;: This serves as a critical component in Ansible for defining the hosts and groups of hosts that Ansible will manage. It is a configuration file that specifies the remote servers or systems where Ansible tasks and playbooks will be executed. In this file, IP addresses of the servers or machines to be managed by Ansible are listed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;roles&lt;/code&gt;: Roles are a way to organize and structure tasks, handlers, variables, and other content for better code reuse and maintainability. This contains the tasks to be executed in playbooks. These tasks are defined within the &lt;code&gt;main.yml&lt;/code&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Update &lt;code&gt;deploy-server.yml&lt;/code&gt; with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- name: "Deploy Play"
  hosts: web
  user: ubuntu
  gather_facts: false
  vars:
    - ansible_python_interpreter: /usr/bin/python3
    - ansible_host_key_checking: false
    - ansible_stdout_callback: yaml
  roles:
    - deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: This is a user-defined name for this playbook that makes it easy for us to understand its function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;hosts&lt;/code&gt;: This specifies the target hosts or hosts group on which the tasks in this playbook will be executed. In this case it is set to web. When you open the &lt;code&gt;inventory.txt&lt;/code&gt; file, it contains a single line, &lt;code&gt;[web]&lt;/code&gt;. This defines a host group called &lt;code&gt;web&lt;/code&gt; and all IP Addresses listed under &lt;code&gt;[web]&lt;/code&gt; are the hosts or remote machines we wish to manage with Ansible. To add an IP address to the inventory we are using &lt;code&gt;aws-cli&lt;/code&gt; in the &lt;code&gt;setup_ansible&lt;/code&gt; job to pull the IP Address of our EC2 server and dynamically update the inventory file. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user&lt;/code&gt;: This specifies the remote user that Ansible should use when connecting to the target hosts. In this case it is &lt;code&gt;ubuntu&lt;/code&gt;. You may need to change that if your ssh user is different.
&lt;code&gt;gather_facts&lt;/code&gt;: This tells Ansible whether to collect facts about the target hosts, such as hardware details, network configuration, and more. Here it is set to false to improve playbook execution speed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;vars&lt;/code&gt;: This defines variables to be used in the playbook. &lt;code&gt;ansible_python_interpreter&lt;/code&gt; specifies the python intepreter Ansible should use when running jobs on the host. &lt;code&gt;ansible_host_key_checking&lt;/code&gt; is set to false to disable host key checking. Host key checking is a prompt that comes up on your command line when you are executing ssh login to a server with a key for the first time. This is a security measure to ensure the remote host identity.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;roles&lt;/code&gt;: This specifies which roles should be included in this job. In this case, it's a role named &lt;code&gt;deploy&lt;/code&gt;. When the playbook is executed, Ansible will look for a directory named "deploy" within the &lt;code&gt;roles&lt;/code&gt; path and execute the tasks and configurations defined in that role. Let's write the tasks for the deploy role.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Copy the code below to &lt;code&gt;main.yml&lt;/code&gt; within the &lt;code&gt;deploy/tasks&lt;/code&gt; role folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- name: Perform Git Pull
  become: false
  git:
    repo: git@github.com:AbuchiKings/aws_ec2_auto_deploy.git # Replace with github repo you prefer
    dest: ~/apps/aws_ec2_auto_deploy
    accept_hostkey: yes
    clone: false
    version: main # Replace with the desired branch or tag

- name: Run Docker build
  become: true
  shell: 
    chdir: apps/aws_ec2_auto_deploy
    cmd: make "{{ item }}"
  loop: 
      - down
      - up-d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first task uses Ansible's &lt;code&gt;git&lt;/code&gt; module to perform a git pull from the project's git repository. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;become&lt;/code&gt; is a boolean field that tells Ansible to perform a task as a root user.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dest&lt;/code&gt; is the destination or directory where the fetched items should be added. &lt;/li&gt;
&lt;li&gt;To prevent a fresh cloning of the repo, &lt;code&gt;clone&lt;/code&gt; is set to false. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;version&lt;/code&gt; defines the branch to pull from.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the second task, the &lt;code&gt;make&lt;/code&gt; command is used on two different arguments defined inside a &lt;code&gt;Makefile&lt;/code&gt; in the project root folder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;down&lt;/code&gt; which maps to &lt;code&gt;docker-compose -f docker-compose.yml -f docker-compose.prod.yml down&lt;/code&gt; takes down the current running container.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;up-d&lt;/code&gt; which maps to &lt;code&gt;docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d --build&lt;/code&gt; starts up the container with a new build in a detached mode. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, commit your changes and push to remote repository.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This is by no means means an exhaustive tutorial on CircleCi and Ansible. There are a wide range of automation tasks you could perform with Ansible. Nonetheless, I hope you find this helpful. Thank you for your time.&lt;/p&gt;

</description>
      <category>cicd</category>
      <category>devops</category>
      <category>ansible</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
