AWS, Cloud Computing, DevOps

5 Mins Read

A Guide to Deploying Amazon EC2 with Terraform, CI/CD, and Atlantis

Overview

In today’s rapidly evolving digital landscape, the ability to efficiently manage and deploy cloud infrastructure is paramount. As organizations adopt the scalability and flexibility offered by cloud computing, tools such as Terraform, Continuous Integration, and Continuous Deployment are becoming increasingly essential. CI/CD pipelines and Atlantis emerge as essential components in streamlining this process. This blog series delves into the intricacies of deploying Amazon EC2 instances on AWS using Terraform while integrating robust CI/CD practices and harnessing the power of Atlantis for seamless collaboration and infrastructure management.

Prerequisites

  • AWS Account and Access Credentials
  • GitHub Account
  • Terraform Installation
  • Atlantis Installation

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Steps to Perform

  1. Set Up AWS Account and Access Credentials:
  • Create an AWS account if you don’t have one: Go to the AWS website and follow the steps to create an account.
  • Retrieve the Access Key ID and Secret Access Key: Once logged into AWS, navigate to the AWS IAM service, create a new AWS IAM user or use an existing one, and generate an access key pair. Make sure to store the credentials securely.
  1. Set Up GitHub Repository:
  • Log in to GitHub and establish a new repository: Click on the “+” symbol located in the top-right corner, then choose “New repository”. Assign a name to your repository and, if desired, provide a description.
  • Initialize the repository: Optionally, kickstart the repository by including a README file.
  • Clone the repository: Copy the repository URL and use Git to clone it onto your local machine.
  • Next, create a secret in the settings, then navigate to actions and establish a new repository secret. Provide both the access key and secret access key as follows:

AWS_ACCESS_KEY_ID=your-access-key-id

AWS_SECRET_ACCESS_KEY=your-secret-access-key

  • Assign a repository name to the secret, which will facilitate its replacement in the GitHub workflow in the subsequent steps.

3. Install Terraform:

  • Download Terraform: Go to the Terraform website (https://www.terraform.io/downloads.html) and download the appropriate version for your operating system.
  • To set up Terraform, adhere to the installation guidelines available on the website corresponding to your operating system.
  • Once installed, validate the installation by launching a terminal or command prompt and executing the Terraform –version to confirm Terraform’s proper installation.

4. Write Terraform Configuration Files:

  • Create Terraform configuration files: In your local repository directory, create .tf files to define your infrastructure resources (e.g., main.tf, variables.tf, outputs.tf).
  • Define resources: Write Terraform code to define the AWS infrastructure resources you want to deploy (e.g., Amazon EC2 instances, Amazon VPC, security groups).

5. Set Up GitHub Actions Workflow:

  • Create a new workflow file: In your GitHub repository, navigate to the .github/workflows directory and create a new YAML file (e.g., terraform-ci.yml).
  • Define workflow steps: Write YAML code to define the workflow steps, including checking out the repository, installing Terraform, configuring AWS credentials, and running Terraform commands.

step5

The following is an example Terraform CI file:

The GitHub actions outlined below are activated every time the code is pushed to the repository.

step5b

step5c

step5d

step5e

6. Set Up Atlantis:

  • Install Atlantis: Follow the installation instructions provided in the Atlantis documentation (https://www.runatlantis.io/docs/install.html) to install Atlantis on a server or container.
  • Configure Atlantis: Configure Atlantis to work with your GitHub repository, including setting up webhooks and authentication. Create an atlantis.yaml file and include the repository details to establish a connection between the Terraform files in the repository and the Atlantis server.

atlantis.yaml file includes the following:

step6

Upon starting the Atlantis Server, the following output is displayed:

step6b

The following is the output from the Atlantis server, indicating a message regarding merging:

step6c

7. Testing and Validation:

  • Test GitHub Actions workflow locally: Use GitHub Actions Runner or push changes to the GitHub repository to trigger the workflow and ensure it runs without errors.
  • Validate Terraform configurations: Run Terraform validate and Terraform plan locally to validate your Terraform configurations and check for any syntax errors or resource conflicts.

8. Deploy Infrastructure:

  • Push changes to GitHub repository: Commit and push your Terraform configuration changes to the GitHub repository to trigger the GitHub Actions workflow.
  • Monitor workflow execution: Monitor the GitHub Actions workflow execution in the GitHub Actions tab of your repository to ensure it completes successfully.
  • Review Terraform plan: Review the Terraform plan generated by the workflow (terraform plan) to verify the planned changes before applying them.

step8

step8b

Conclusion

Streamlining AWS infrastructure deployment with Terraform, implementing CI/CD pipelines using GitHub Actions, and potentially leveraging Atlantis for collaborative infrastructure management offers organizations a powerful toolkit for achieving efficiency, reliability, and scalability in their cloud environments.

By adopting Terraform as an infrastructure as code (IaC) tool, teams can define and manage their AWS resources programmatically, promoting consistency and repeatability in deployments. Utilizing Terraform alongside CI/CD pipelines supported by GitHub Actions automates the deployment procedure, diminishing manual involvement and mitigating the likelihood of errors. Additionally, leveraging Atlantis facilitates collaborative infrastructure management, allowing teams to coordinate changes, enforce policies, and maintain visibility into infrastructure modifications.

Throughout this journey, we’ve explored key concepts, best practices, and practical steps for orchestrating AWS infrastructure deployments seamlessly. From setting up AWS credentials securely to configuring GitHub Actions workflows and potentially integrating Atlantis for enhanced collaboration, each step contributes to a streamlined and efficient workflow.

Drop a query if you have any questions regarding CI/CD pipelines and we will get back to you quickly.

Making IT Networks Enterprise-ready – Cloud Management Services

  • Accelerated cloud migration
  • End-to-end view of the cloud environment
Get Started

About CloudThat

CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, Microsoft Gold Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, and many more.

To get started, go through our Consultancy page and Managed Services PackageCloudThat’s offerings.

FAQs

1. What advantages come with integrating CI/CD pipelines into Terraform workflows?

ANS: – Integrating CI/CD pipelines streamlines the tasks of constructing, testing, and rolling out alterations to infrastructure, enhancing productivity and mitigating the potential for human mistakes. They facilitate quicker iteration cycles, foster enhanced teamwork, and offer greater insight into the deployment journey.

2. How do I secure my AWS credentials when using GitHub Actions for Terraform deployments?

ANS: – To secure AWS credentials in GitHub Actions workflows, you can use GitHub Secrets to store sensitive information like access keys and secret keys. Then, reference these secrets in your workflow YAML file and configure Terraform to use them during execution.

3. What is Atlantis, and how does it enhance collaborative infrastructure management?

ANS: – Atlantis is an open-source tool that automates Terraform workflows within version control systems like GitHub. It allows teams to manage infrastructure changes through pull requests, enforce policies, and maintain a single source of truth for infrastructure configurations, enhancing collaboration and governance.

WRITTEN BY Ramyashree V

Ramyashree V is working as a Research Associate in CloudThat. She is an expert in Kubernetes and works on many containerization-based solutions for clients. She is interested in learning new technologies in Cloud services.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!