You are on page 1of 17

Table of Contents

1. Introduction ...................................................................................................................... 4

1.1. Continuous Integration ......................................................................................................... 5

1.2. Continuous Delivery/Deployment……………………………………………………6

1.3. CircleCI………………………………………………………………………………..7

1.4. Terraform……………………………………………………………………………..8
2. Objectives .......................................................................................................................... 9

3. Prerequisites ...................................................................................................................... 9

4. Project Design ................................................................................................................... 9

5. Results .............................................................................................................................. 16

6. References ........................................................................................................................ 18

2
LIST OF FIGURES
Figure 1.1: Overview of CircleCI .............................................................................................. 7
Figure 1.2: Overview of Terraform…………………………………………………………....8
Figure 5.1: CircleCI Dashboard ............................................................................................... 16
Figure 5.2: CI/CD Pipeline using CIrcleCI.............................................................................. 16
Figure 5.3: S3 Bucket deployed using CircleCI and Terraform .............................................. 17

3
1. Introduction
In the software industry, SDLC has more emphasis on the Continuous Integration
and Delivery as it helps the DevOps team deliver the software faster with good quality. In
older days Jenkins was the only CI-CD tool that one could think of! Many new CI-CD
tools have emerged with out-of-the-box features that make you wonder about which CI-
CD should you choose. One such tool in the CI-CD competition is CircleCI which
modernizes CI/CD practices by overcoming certain limitations of Jenkins. On the other
hand, being one of the oldest players in the CI-CD market, Jenkins has a large user base
with an easy learning curve as you get a lot of content & community forums around it.
But CircleCI overcomes some limitations of Jenkins.
CircleCI is a continuous integration and delivery (CI/CD) platform for automating
software builds, tests, and deployments. The CI/CD paradigm establishes version control
repositories as the source of truth for your deployments. It also helps teams quickly ship
new features and fixes by defining pipelines that help ensure the stability and resilience
of your services through testing and automation. You can build deployment pipelines of
varying complexity to satisfy your organization’s requirements for production
deployments.
There are a lot of Infrastructure as Code tools available in the market like Terraform,
Chef, Puppet, Ansible, and CloudFormation. But Terraform has many more advantages
as compared to other IAC tools. Terraform is an open-source, cloud-agnostic provisioning
tool that supported immutable infrastructure, a declarative language, a masterless and
agentless architecture, and had a large community and a mature codebase.
Using Terraform to manage infrastructure as code enables the benefits of the CI/CD
workflow for infrastructure deployments. Since infrastructure is codified, team can
collaborate and review it and deploy it using automated pipelines instead of manual
orchestration. To automate Terraform operations in a remote environment, In order to
configure remote state storage so Terraform can access and manage project's state across
runs.

4
1.1. Continuous Integration
Continuous Integration (CI) is a software development practice where
developers regularly integrate their code changes into a shared repository. This
integration is often automated using a CI tool, which builds and tests the code changes
before they are merged into the main codebase.
The main goal of continuous integration is to catch and address issues as early
as possible in the development process. By regularly integrating and testing code
changes, developers can quickly identify and fix issues before they become larger
problems. This can ultimately lead to a more stable, higher-quality codebase.
Here are the key steps involved in implementing a continuous integration
process:
 Choose a CI tool:
There are many CI tools available, such as CircleCI, Jenkins, Travis CI, and GitLab
CI/CD. Choose a tool that integrates well with your development environment and
provides the features you need.
 Set up your CI environment:
Configure your CI tool to connect to the code repository, build and test your code,
and provide feedback to developers.
 Create a build pipeline:
Define the steps needed to build and test the code, such as compiling code, running
unit tests, and generating code coverage reports.
Set up automated builds that trigger whenever code changes are pushed to the
repository.
 Configure notifications and feedback:
Set up notifications and feedback mechanisms that notify developers of build
failures or other issues. Provide developers with access to logs and reports to help them
quickly diagnose and fix issues.
 Monitor and iterate:
Continuously monitor your CI process and make improvements as needed.
Use data and metrics to identify areas for improvement, such as slow build times or
frequent build failures.

5
By implementing a continuous integration process, developers can work more
efficiently and catch issues earlier in the development process, resulting in a higher-
quality codebase and a more stable application.

1.2. Continuous Delivery/Deployment


Continuous Delivery/Deployment (CD) is a software development practice that
automates the process of deploying code changes to production as soon as they are
ready. With continuous deployment, code changes that pass the automated testing and
review process are automatically released into production without any manual
intervention.
The main goal of continuous delivery/deployment is to reduce the time between
developing a feature and making it available to users, while maintaining a high level of
confidence in the quality and stability of the application.
Here are the key steps involved in implementing a continuous deployment process:
 Implement Continuous Integration:
Implement continuous integration to ensure that code changes are automatically
built, tested, and validated before they are deployed to production.
 Create a Deployment Pipeline:
Define the steps involved in deploying code changes to production.
Create an automated deployment pipeline that includes stages for building, testing,
deploying, and verifying code changes.
 Implement Automated Testing and Quality Checks:
Implement automated tests and quality checks to ensure that code changes meet the
necessary criteria before they are deployed to production.
Include tests for functional requirements, performance, security, and user experience.
 Implement Infrastructure as Code:
Use Infrastructure as Code (IaC) tools, such as Terraform or CloudFormation, to
define and manage the infrastructure required to run the application. Use version control
to track changes to the infrastructure code.
 Implement Canary Releases:
Implement canary releases to gradually roll out code changes to production and
ensure that the changes do not cause any adverse effects on the application. Monitor
production metrics to ensure that the canary release is successful before fully deploying
the code changes.

6
 Monitor and Iterate:
Continuously monitor the deployment process and collect feedback from users and
stakeholders. Use data and metrics to identify areas for improvement and make iterative
improvements to the deployment process.

1.3. CircleCI
CircleCI is a cloud-based continuous integration and continuous delivery
(CI/CD) platform that automates the building, testing, and deployment of software
applications. CircleCI supports a wide range of programming languages, frameworks,
and tools, and integrates with popular version control systems like GitHub and
Bitbucket.

Figure 1.1. Overview of CircleCI


Key features of CircleCI:
Automated Testing: CircleCI supports a range of automated testing frameworks and
provides tools for parallel testing to accelerate the testing process.
Easy Integration: CircleCI integrates with many popular tools, including GitHub,
Bitbucket, AWS, and Docker, to help teams easily incorporate CircleCI into their
existing workflows.
Fast Builds: CircleCI allows teams to run builds in parallel and provides a range of
customization options to optimize build performance.
Deployment Automation: CircleCI provides tools for automating the deployment of
applications to a variety of platforms, including AWS, Google Cloud, and Heroku.
Scalability: CircleCI can scale to meet the needs of small teams as well as large
enterprise organizations.
7
Security: CircleCI provides a range of security features, including SSH access controls
and the ability to encrypt sensitive data.
Overall, CircleCI is a powerful and flexible platform that helps development teams
automate their CI/CD workflows, streamline the software delivery process, and
improve the quality and stability of their applications.

1.4. Terraform
Terraform is an open-source Infrastructure as Code (IaC) tool that allows
developers to define and manage infrastructure resources in a declarative way.
Terraform uses a domain-specific language (DSL) to describe the desired state of the
infrastructure and automatically provisions and manages the resources to match that
state.

Figure 1.2. Overview of Terraform


Features of Terraform:
Multi-Cloud Support: Terraform supports a wide range of cloud providers, including
Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP), and
many others.
Declarative Language: Terraform uses a declarative language to describe the desired
state of the infrastructure, allowing developers to focus on what they want their
infrastructure to look like rather than how to achieve that state.

8
Modular Design: Terraform allows developers to modularize their infrastructure code,
making it easier to reuse and maintain infrastructure resources.
State Management: Terraform tracks the state of infrastructure resources and can apply
changes incrementally, reducing the risk of errors and ensuring consistency across
environments.
Plan and Apply: Terraform provides a "plan" command that shows developers what
changes Terraform will make to the infrastructure before applying those changes.
Extensibility: Terraform is highly extensible and supports a range of plugins and third-
party providers.
Overall, terraform is a powerful tool that allows developers to automate the
management of infrastructure resources in a consistent, repeatable, and scalable way.
By using Terraform, teams can increase their productivity, reduce errors, and improve
the reliability and resilience of their infrastructure.

2. Objectives
 To learn and understand about CircleCI and Terraform.
 To know the advantages of CircleCI and Terraform over Other CI/CD tools like
Jenkins, ansible practically.
 To deploy an S3-backed web application using CircleCI and Terraform.

3. Prerequisites
 A GitHub account
 A CircleCI account. Sign up with your GitHub account so CircleCI can build and
deploy from your GitHub repositories. Review the CircleCI getting started guide
for an introduction to the workflow if you are unfamiliar.
 An AWS account.
 A Terraform Cloud account.

4. Project Design
 Create a Terraform Cloud token
In order to authenticate with Terraform Cloud to store your project's Terraform
state, you need to configure your Terraform Cloud integration with a Terraform Cloud

9
API token. Navigate to your organization settings, then select the Teams page. Under
the Team API Token section, click Create a team token.
Store this token in a secure place as Terraform Cloud will not display it again. Later
in this tutorial, you will set an environment variable in your CircleCI project to this
token value for your build to use.
 Fork and clone the repository that contains s3 backed web application.
Implementation:
main.tf
provider "aws" {
region = var.region

default_tags {
tags = {
hashicorp-learn = "circleci"
}
}
}

resource "random_uuid" "randomid" {}

resource "aws_s3_bucket" "app" {


tags = {
Name = "App Bucket"
}

bucket = "${var.app}.${var.label}.${random_uuid.randomid.result}"
force_destroy = true
}

resource "aws_s3_bucket_object" "app" {


acl = "public-read"
key = "index.html"
bucket = aws_s3_bucket.app.id

10
content = file("./assets/index.html")
content_type = "text/html"
}

resource "aws_s3_bucket_acl" "bucket" {


bucket = aws_s3_bucket.app.id
acl = "public-read"
}

resource "aws_s3_bucket_website_configuration" "terramino" {


bucket = aws_s3_bucket.app.bucket

index_document {
suffix = "index.html"
}

error_document {
key = "error.html"
}
}

terraform.tfvars
region = "us-east-1"
label = "hashicorp"
app = "terramino"

main.tf
terraform {
cloud {}

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.4.0"
11
}
}

required_version = ">= 1.2.0"


}
 Review CircleCI jobs and workflow
Navigate to the .circleci subdirectory
$ cd .circleci

.circleci/config.yml
jobs:
plan-apply:
working_directory: /tmp/project
docker:
- image: docker.mirror.hashicorp.services/hashicorp/terraform:light
steps:
- checkout
- run:
name: terraform init & plan
command: |
terraform init -input=false
terraform plan -out tfapply -var-file terraform.tfvars
- persist_to_workspace:
root: .
paths:
-.

apply job
apply:
docker:
- image: docker.mirror.hashicorp.services/hashicorp/terraform:light
steps:
- attach_workspace:
at: .
12
- run:
name: terraform
command: |
terraform apply -auto-approve tfapply
- persist_to_workspace:
root: .
paths:
-.
plan-destroy and destroy jobs:
plan-destroy:
docker:
- image: docker.mirror.hashicorp.services/hashicorp/terraform:light
steps:
- attach_workspace:
at: .
- run:
name: terraform create destroy plan
command: |
terraform plan -destroy -out tfdestroy -var-file terraform.tfvars
- persist_to_workspace:
root: .
paths:
-.

destroy:
docker:
- image: docker.mirror.hashicorp.services/hashicorp/terraform:light
steps:
- attach_workspace:
at: .
- run:
name: terraform destroy
command: |
terraform apply -auto-approve tfdestroy
13
Workflow job:

workflows:
version: 2
plan_approve_apply:
jobs:
- plan-apply
- hold-apply:
type: approval
requires:
- plan-apply
- apply:
requires:
- hold-apply
- plan-destroy:
requires:
- apply
- hold-destroy:
type: approval
requires:
- plan-destroy
- destroy:
requires:
- hold-destroy

Create Terraform Cloud workspace


This configuration uses Terraform Cloud for your project's state storage. You will
create a new workspace to use for this project and configure it for local execution. When
using local execution with Terraform Cloud, the Terraform operations occur in the
environment that runs the Terraform CLI (in this case, the Docker executor configured
for your build), and Terraform Cloud stores the state file for shared access across builds
and runs.
14
In the Terraform Cloud UI, create a new CLI-driven workspace named learn-terraform-
circleci. On the workspace overview page, click on the curent Execution Mode to
navigate to the general settings.
Configure CircleCI project
Navigate to the CircleCI dashboard. Make sure that it is in the correct organization
with access to your GitHub account by confirming the organization in the top left
corner.
Then, select Projects in the left sidebar. Search for your forked learn-terraform-circleci
repository. Then, click the Set Up Project button
Select the Fastest configuration option to use the CircleCI configuration file in the
repository. Enter the main branch as the brain to track. Then, click Set Up Project.
CircleCI will automatically attempt to run the job and fail because the project needs
your AWS credentials and Terraform Cloud integration details.Navigate to the project's
Project Settings, then select Environment Variables from the sidebar.
Set the following environment variables, which Terraform will access in your build
environment to configure both the AWS provider and the Terraform Cloud integration
for your project:
Set AWS_ACCESS_KEY_ID to the generated key for the AWS user running this job.
To generate an access key and secret access key file, log in to your AWS account and
create them in IAM.
Set AWS_SECRET_ACCESS_KEY to the secret access key you generated above.
Set TF_CLOUD_ORGANIZATION to your Terraform Cloud organization name
Set TF_WORKSPACE to learn-terraform-circleci, the Terraform Cloud workspace you
created and configured earlier in this tutorial.
Set TF_TOKEN_app_terraform_io to the API token you created at the beginning of the
tutorial.
When complete, your environment variables page will list the configured variables.

Trigger CircleCI workflow


git add terraform.tfvars
git commit -m "Update variable definitions"
git push

15
5. Results

Figure 5.1. CircleCI Dashboard

Figure 5.2. CI/CD Pipeline using CircleCI

16
Figure 5.3. S3 Bucket deployed using CircleCI and Terraform

17
6. References
1. https://developer.hashicorp.com/terraform/tutorials/automation/circle-ci
2. https://circleci.com/docs/getting-started/
3. https://k21academy.com/terraform-iac/terraform-beginners-guide/
4. https://spacelift.io/blog/terraform-tutorial

18

You might also like