Terraform Basics - To get Started
Terraform – Step by Step
Pre-requisites
Link to download VS Code: https://code.visualstudio.com
Link for the vscode terrafor plugin: https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
IAM USER with Administrator access just for learning purpose.
Install AWS CLI - https://aws.amazon.com/cli/
How to install Chocolatey and Terraform?
Install Chocolatey : https://chocolatey.org/install
Open Windows Powershell and Run as administrator, See screenshot below:
Run CommandSet-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
Once installed, type choco and enter you can see the latest version of chocolatey
Install Terraform
Install terraform using command -> choco install terraform (Run on windows power shell)
https://learn.hashicorp.com/tutorials/terraform/install-cli
Check version of terraform using command: terraform version
Now to get started with the terraform first we have to use AWS configure command to sync AWS IAM user with its Access key and Secret access key. Type aws configure and provide access key and secret access key and specify region too (type all this on CMD)
Brief Introduction
• Terraform is a DevOps tool for declarative infrastructure—infrastructure as code
• It simplifies and accelerates the configuration of cloud-based environments
• 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
What we will see in this blog?
• How Terraform deploy resources and set up immutable infrastructure in Amazon Web Services.
• Learn how to deploy servers /virtual machines.
• Discover how to use Terraform CLI commands
What is Immutable infrastructure?
It refers to servers (or VMs) that are never modified after deployment. With an immutable infrastructure paradigm, servers work differently. We no longer want to update in-place servers. Instead, we want to ensure that a deployed server will remain intact, with no changes made.
Lets get started :)
Open VS Code and create directory where you can write declarative scripts. I have created folder named as “Terraform0512” ..Create one more folder inside it named as “first-resource” Add below lines of code.
First Ex: How to Create VPC on AWS using Terraform?
provider "aws" {
region = "us-east-1"
}
resource "aws_vpc" "challenge1vpc" {
cidr_block = "192.168.0.0/24"
tags = {
Name = "TerraformVPC"
}
}
Go to VS Code terminal -> Go to Terraform Directory / First resource -> Then Run below commands one after another in VS Code Terminal
terraform init
terraform plan
terraform apply
Now, you can see in AWS console your VPC is created in the region that you mention in your declarative script.
Don’t forget to destroy VPC ;)
Run terraform destroy
Now lets see above Ex: With Slight modification – Introduced Variable and Output
provider "aws" {
region = "us-east-1"
}
variable "inputname" {
type = string
description = "Set the name of the VPC"
}
resource "aws_vpc" "challenge1vpc" {
cidr_block = "192.168.0.0/24"
tags = {
Name = var.inputname
}
}
output "vpcid" {
value = aws_vpc.challenge1vpc.id
}
The variable is introduced in this example so that it can store some input value and it can further be passed into the resource section. The only purpose to do this is we are asking user to enter the name of VPC of his choice
Output section indicates here that once VPC resource is created using terraform the script will return you the VPC id on terminal screen
aws_vpc.challenge1vpc.id -> Here id is attribute and all such other attributes you can found on Terraform documentation .. See the reference section below for the documentation link.
Run below commands one after another in VS Code Terminal ->Go to Terraform Directory -> go to First resource -> Run
terraform init
terraform plan
terraform apply
Now, you can see in AWS console your VPC is created in the region that you mention in your declarative script.
Run terraform destroy
Ex: 2 How to launch Ec2 and apply Elastic IP using terraform
provider "aws" {
region = "eu-west-2"
}
resource "aws_instance" "ec2" {
ami = "ami-06dc09bb8854cbde3" // for london
instance_type = "t2.micro"
tags = {
Name = "Linux EC2 "
}
}
resource "aws_eip" "elasticeip" {
instance = aws_instance.ec2.id
}
output "EIP" {
value = aws_eip.elasticeip.public_ip
}
On terminal, Run
terraform init
terraform plan
terraform apply
Now, you can see in AWS console your EC2 is created in the region that you mention in your declarative script.
Terminal returns the Elastic IP see below:
Again, Don't forget to Run terraform destroy
Explanation of above code: Here, Provider section indicates that which cloud platform terraform will interact with and region indicates that the resource is going to create in which region in AWS
First Resource section indicates that Terraform is going to create a EC2 instance and in that section ami and instance type are required thing which we need to pass. That means, Terraform will create linux ami and t2.micro instance on AWS in London region
Second resource section is used to create elastic ip for EC2. Mandatory parameter is instance and its value will be AWS instance and id indicates here is attribute which will elastic IP for EC2
Output section indicates here that once EC2 resource is created using terraform the script will return the EC2 Elastic IP on terminal screen
References:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs
https://www.udemy.com/course/terraform-fast-track/
https://k21academy.com/terraform-iac/terraform-workflow-and-its-use-case/
Watch horror movies online like never before with GeoFlixzPlus! Dive into a spine-chilling collection of terrifying classics and modern-day nightmares, all available to stream anytime, anywhere. Get ready for an edge-of-your-seat experience that will leave you craving more.
ReplyDelete