This is one of the common DevOps interview questions. What is the
difference between Ansible and Terraform? When will you choose Ansible over Terraform?
We will be learning how to execute Terraform scripts automatically using Jenkins pipeline. We will create EC2 instance using Terraform and Jenkins in AWS cloud.
Click on Build with Parameters and choose apply to build the infrastructure or choose destroy if you like to destroy the infrastructure you have built.
Click on Build
Now you should see the console output if you choose apply.
Pipeline will look like below:
Login to AWS console, you should see the new EC2 instance created.
We will see how to connect to GitHub from Jenkins using SSH keys instead of using user name and password. It is also a good practice to use SSH keys in Jenkins jobs instead of using user name and password.
Watch the steps in YouTube channel:
Pre-requistes:
Jenkins is up and running
Credentials plug-in installed in Jenkins
Create SSH keys in your Jenkins EC2 instance
ssh-keygen
enter four times. this will create keys in .ssh folder.
Copy and paste the public key
sudo cat ~/.ssh/id_rsa.pub
Add public Keys into your respective GitHub
Add public keys into your Repository--> settings--> Deploy keys section
Click on Add Deploy Key and enter public keys and save.
Add Private Keys in Jenkins Master
Login Jenkins. Go to Manage Jenkins. click on Credentials
Click on Jenkins
Click on Global Credentials
Click on Add Credentials
Choose SSH username with private key
Choose SSH username with private key username can be anything Click on enter directly under private key option and Click Add
Copy and paste private key(not public key) of your from Jenkins instance. command is below: sudo cat ~/.ssh/id_rsa
copy the content of whole output from above command.
Click OK to save.
Now go to any Jenkins Job, you can choose this option for checking out from GitHub. Make sure you enter SSH url not https url.
That's it. This is how you use SSH url and private keys to checkout code from bitbucket or Github without entering username/password in Jenkins.
Hashicorp's Terraform is an open-source tool for provisioning and managing cloud infrastructure. Terraform can provision resources on any cloud platform.
Terraform allows you to create infrastructure in configuration files(tf files) that describe the topology of cloud resources. These resources include virtual machines, storage accounts, and networking interfaces. The Terraform CLI provides a simple mechanism to deploy and version the configuration files to Azure.
Watch the steps in YouTube:
Advantages of using Terraform:
Reduce manual human errors while deploying and managing infrastructure.
Deploys the same template multiple times to create identical development, test, and production environments.
Reduces the cost of development and test environments by creating them on-demand.
How to Authenticate with Azure?
Terraform can authenticate with Azure in many ways, in this example we will use Azure CLI to authenticate with Azure and then we will create resources using Terraform.
The above command will open the browser and will ask your Microsoft account details. Once you logged in, you can see the account info by executing below command:
az account list
Now create a directory to store Terraform files.
mkdir tf-acr
cd tf-acr
Let's create a terraform file to use azure provider. To configure Terraform to use the Default Subscription defined in the Azure CLI, use the below cod.
Now initialize the working directory
sudo vi create-acr.tf
provider "azurerm" { features {} } resource "azurerm_resource_group" "rg" { name = "rg-tf-acr" location = "southcentralus" } resource "azurerm_container_registry" "acr" { name = "azcontainerregistry321" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location sku = "Basic" admin_enabled = true } output "admin_password" { value = azurerm_container_registry.acr.admin_password description = "The object ID of the user" sensitive = true }
Perform the below command to initialize the directory.
terraform init
Once directory is initialized, you can start writing code for setting up the infrastructure. Now Perform the below command to validate terraform files.
terraform validate
perform plan command to see how many resources will be created.
terraform plan
terraform apply
Do you want to perform these actions?
type yes
Now Login into Azure Cloud to see the resources created.
How to destroy the resources ?
Execute terraform destroy
The above command to destroy both resource group and container registry created before.
One of the things that Terraform does (and does really well) is “tracks” your infrastructure that you provision. It does this through the means ofstate.
By default, Terraform stores state locally in a file named terraform.tfstate. This does not work well in a team environment where if any developer wants to make a change he needs to make sure nobody else is updating terraform in the same time.
Why state file should not be stored in your local machine?
Local state doesn't work well in a team or collaborative environment.
Terraform state can include sensitive information.
Storing state locally increases the chance of inadvertent deletion.
With remote state, Terraform writes the state data to a remote data store, which can then be shared between all members of a team. Terraform supports storing state in many ways including the below:
Terraform Cloud
HashiCorp Consul
Amazon S3
Azure Blob Storage
Google Cloud Storage
Alibaba Cloud OSS
Artifactory or Nexus
We will learn how to store state file in AWS S3 bucket. We will be creating S3 bucket and also create Dynamo Table where we will be storing lock.
AWS access keys and secret keys have been configured using AWS CLI.
Steps:
mkdir project-terraform
cd project-terraform
First let us create necessary terraform files.
Create tf files
sudo vi variables.tf
variable "region" { default = "us-east-2" }
variable "instance_type" { default = "t2.micro" }
sudo vi main.tf provider "aws" { region = "${var.region}" }
#1 -this will create a S3 bucket in AWS resource "aws_s3_bucket" "terraform_state_s3" { bucket = "terraform-coachdevops-state" force_destroy = true # Enable versioning to see full revision history of our state files versioning { enabled = true }
# 2 - this Creates Dynamo Table resource "aws_dynamodb_table" "terraform_locks" { name = "tf-up-and-run-locks" billing_mode = "PAY_PER_REQUEST" hash_key = "LockID" attribute { name = "LockID" type = "S" } }
now let us initialize terraform.
terraform init
terraform apply
this will create two resources - S3 bucket and AWS Dynamo table. But still terraform state file is stored locally. if you type below command, you will see the tfstate file locally.
ls -al
To store state file remotely, you need to add following code with terraform block. This will add backend configuration.
sudo vi main.tf
#Step 3 - Creates S3 backend terraform { backend "s3" { #Replace this with your bucket name! bucket = "terraform-coachdevops-state" key = "dc/s3/terraform.tfstate" region = "us-east-2" #Replace this with your DynamoDB table name! dynamodb_table = "tf-up-and-run-locks" encrypt = true } }
terraform init
and then type yes and enter. Now you see local state file has 0 bytes(empty)
Now login to AWS console, Click on S3, Click on the bucket name
Now you should be able to see tfstate file in S3.
Click on the terraform.tfstate file, you can see multiple version of your state file. Terraform is automatically pushing and pulling state data to and from S3.
How to perform Destroy?
It is not that straight forward as back end is referencing S3 bucket, if we delete S3 bucket, back end will not where to reference. So we need to perform below steps to perform destroy:
1. remove back end reference in the main.tf by commenting out backend section of the code.
sudo vi main.tf
remove the below code or comment out:
/*
terraform { backend "s3" { #Replace this with your bucket name! bucket = "terraform-coachdevops-state" key = "dc/s3/terraform.tfstate" region = "us-east-2" #Replace this with your DynamoDB table name! dynamodb_table = "tf-up-and-run-locks" encrypt = true } }
*/
We need to initialize again. so type below command
terraform init -migrate-state
type yes
Now you will see the local state file have been updated.
Now perform you can delete all the resources created by Terraform including S3 bucket and Dynamo table.
The Azure command-line interface (Azure CLI) is a set of commands used to create and manage Azure resources. The Azure CLI is available across Azure services and is designed to get you working quickly with Azure, with an emphasis on automation. Azure CLI is Microsoft's cross-platform command-line experience for managing Azure resources.
Azure CLI can be installed by executing the below command: