Professional Documents
Culture Documents
1. Introduction ...................................................................................................................... 4
1.3. CircleCI………………………………………………………………………………..7
1.4. Terraform……………………………………………………………………………..8
2. Objectives .......................................................................................................................... 9
3. Prerequisites ...................................................................................................................... 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.
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.
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.
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"
}
}
}
bucket = "${var.app}.${var.label}.${random_uuid.randomid.result}"
force_destroy = true
}
10
content = file("./assets/index.html")
content_type = "text/html"
}
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
}
}
.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
15
5. Results
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