Reader small image

You're reading from  The Linux DevOps Handbook

Product typeBook
Published inNov 2023
PublisherPackt
ISBN-139781803245669
Edition1st Edition
Concepts
Right arrow
Authors (2):
Damian Wojsław
Damian Wojsław
author image
Damian Wojsław

Damian Wojsław has been working in the IT industry since 2001. He specializes in administration and troubleshooting of Linux servers. Being a system operator and support engineer he has found DevOps philosophy a natural evolution of the way sysops work with developers and other members of the software team.
Read more about Damian Wojsław

Grzegorz Adamowicz
Grzegorz Adamowicz
author image
Grzegorz Adamowicz

Grzegorz Adamowicz has been working in the IT industry since 2006 in a number of positions, including Systems Administrator, Backend Developer (PHP, Python), Systems Architect and Site Reliability Engineer. Professionally was focused on building tools and automations inside projects he is involved in. He's also engaged with the professional community by organizing events like conferences and workshops. Grzegorz worked in many industries including Oil & Gas, Hotel, Fintech, DeFI, Automotive, Space and many more.
Read more about Grzegorz Adamowicz

View More author details
Right arrow

CI/CD with Terraform, GitHub, and Atlantis

In this chapter, we are going to build on the previous chapters in this book by introducing pipelines for continuous integration (CI) and continuous deployment (CD). There are many CI and CD tools available for you, both open source and closed source, as well as self-hosted and Software-as-a-Service (SaaS). We are going to demonstrate an example pipeline, starting from committing source to the repository where we store Terraform code to applying changes in your infrastructure. We will do this automatically but with reviews from your team.

In this chapter, we are going to cover the following topics:

  • What is CI/CD?
  • Continuously integrating and deploying your infrastructure
  • CI/CD with Atlantis

Technical requirements

For this chapter, you will need the following:

  • A Linux box
  • A free account on GitHub or similar platform (GitLab or Bitbucket)
  • The latest version of Terraform
  • The AWS CLI
  • Git

What is CI/CD?

CI/CD is a set of practices, tools, and processes that allow software development teams to automate the building, testing, and deployment of their applications, enabling them to release software more frequently and with greater confidence in its quality.

Continuous integration (CI) is a practice where developers regularly integrate their code changes into a repository, and each integration triggers an automated build and test process. This helps catch errors early and ensures that the application can be built and tested reliably.

For example, using Docker, a developer can set up a CI pipeline that automatically builds and tests their application whenever changes are pushed to the code repository. The pipeline can include steps to build a Docker image, run automated tests, and publish the image to a Docker registry.

Continuous delivery is the practice of getting software to be available for deployment after the successful integration process. For example, with...

Continuously integrating and deploying your infrastructure

Testing application code is now a de facto standard, especially since the adoption of test-driven development (TDD). TDD is a software development process in which developers write automated tests before writing code.

These tests are designed to fail initially, and developers then write code to make them pass. The code is continuously refactored to ensure it is efficient and maintainable while passing all tests. This approach helps reduce the number of bugs and increase the reliability of the software.

Testing infrastructure is not as easy as that as it’s hard to check whether Amazon Elastic Compute Cloud (EC2) will be successfully started without actually starting the instance. It’s possible to mock API calls to AWS, but it won’t guarantee that the actual API will return the same results as your testing code. With AWS, it would also mean that testing will be slow (we will need to wait for this EC2...

CI/CD with Atlantis

Armed with the knowledge about tooling and principles around CI/CD (both delivery and deployment), we will create a CI/CD pipeline with the use of Git and the open source tool Atlantis. We will automatically test and deploy changes to our AWS infrastructure with it and do basic testing along the way.

Deploying Atlantis to AWS

We will use the Terraform module created by Anton Bobenko from the terraform-aws-modules project on GitHub. Here is the Terraform Registry link to the module: https://registry.terraform.io/modules/terraform-aws-modules/atlantis/aws/latest.

You can use this module in two ways. First, which is natural, is using it in your existing Terraform code to deploy it in AWS. The second, which we will use for this demonstration, is using the module as a standalone project. The module will also create a new Virtual Private Cloud (VPC) for you in the eu-west AWS zone and Atlantis will be running inside the AWS ECS service. This will generate some...

Summary

In this chapter, we explored the benefits of using Terraform for IaC and discussed the importance of incorporating CI/CD processes in Terraform workflows. We covered testing infrastructure and various tools for automating deployment.

In the final section, we explained how to deploy Atlantis, an open source tool for automated Terraform pull request previews, to AWS and configure GitHub to trigger terraform plan and terraform apply. With Atlantis, Terraform users can collaborate on infrastructure changes through GitHub pull requests, allowing for infrastructure changes to be reviewed and approved before they are applied to production. By incorporating Atlantis into your Terraform workflow, you can improve collaboration, reduce errors, and achieve faster and more secure infrastructure changes.

In the final chapter, we will slow down a little and talk about DevOps misconceptions and antipatterns, and how to avoid them.

Exercises

Try out the following exercises to test your knowledge of this chapter:

  1. Try to deploy Atlantis locally by following the documentation found at https://www.runatlantis.io/guide/testing-locally.html.
  2. Create a repository and configure the webhook and PAT for yourself. Run a plan for your new repository (hint: instead of AWS resources, you can use a null resource for testing).
  3. Create an account on one of the CD solution websites and try to run a plan using this SaaS. There’s usually a free plan for public repositories.
lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Linux DevOps Handbook
Published in: Nov 2023Publisher: PacktISBN-13: 9781803245669
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Damian Wojsław

Damian Wojsław has been working in the IT industry since 2001. He specializes in administration and troubleshooting of Linux servers. Being a system operator and support engineer he has found DevOps philosophy a natural evolution of the way sysops work with developers and other members of the software team.
Read more about Damian Wojsław

author image
Grzegorz Adamowicz

Grzegorz Adamowicz has been working in the IT industry since 2006 in a number of positions, including Systems Administrator, Backend Developer (PHP, Python), Systems Architect and Site Reliability Engineer. Professionally was focused on building tools and automations inside projects he is involved in. He's also engaged with the professional community by organizing events like conferences and workshops. Grzegorz worked in many industries including Oil & Gas, Hotel, Fintech, DeFI, Automotive, Space and many more.
Read more about Grzegorz Adamowicz