Terraform¶
Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can help with multi-cloud by having one workflow for all clouds. The infrastructure Terraform manages can be hosted on public clouds like Amazon Web Services, Microsoft Azure, and Google Cloud Platform, or on-prem in private clouds such as VMWare vSphere.
Terraform Installation¶
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform
Reference¶
Terraform Registry
Terraform Commands Cheat sheet Reference Link
https://acloudguru.com/blog/engineering/the-ultimate-terraform-cheatsheet
Terraform Sample Scripts¶
Launching Ec2 Instance
provider "aws" {
region = "us-east-2"
}
resource "aws_instance" "Instance" {
ami = "ami-0fb653ca2d3203ac1"
instance_type = "t2.micro"
key_name = "terraform"
vpc_security_group_ids = ["sg-0138c7796472ac9a9"]
tags = {
Name = "IAAC"
Team = "DevOps"
}
}
Terraform Vars¶
Using Variables
Create a File vars.tf
vim vars.tf
variable "REGION" {
default = "us-east-2"
}
variable "ZONE1" {
default = "us-east-2a"
}
variable "AMIS" {
type = map(any)
default = {
us-east-2 = "ami-0fb653ca2d3203ac1"
us-east-1 = "ami-0e1d30f2c40c4c701"
}
}
Terraform Provider¶
Example for AWS
vim provider.tf
provider "aws" {
region = var.REGION
}
Launching Instance with Vars File
vim Instance.tf
resource "aws_instance" "Instance" {
ami = var.AMIS[var.REGION]
instance_type = "t2.micro"
key_name = "terraform"
vpc_security_group_ids = ["sg-0138c7796472ac9a9"]
tags = {
Name = "IAAC"
Team = "DevOps"
}
}
Terraform Provisioning¶
Launching AWS Resources with Terraform Provisioning
vim instance_prov.tf
resource "aws_key_pair" "testing007" {
key_name = "testing007"
public_key = file("testing007.pub")
}
resource "aws_instance" "Instance" {
ami = var.AMIS[var.REGION]
instance_type = "t2.micro"
key_name = aws_key_pair.testing007.key_name
vpc_security_group_ids = ["sg-0138c7796472ac9a9"]
tags = {
Name = "IAAC"
Team = "DevOps"
}
provisioner "file" {
source = "./web.sh"
destination = "/tmp/web.sh"
}
provisioner "remote-exec" {
inline = [
"chmod u+x /tmp/web.sh",
"sudo /tmp/web.sh"
]
}
connection {
user = var.USER
private_key = file("testing007")
host = self.public_ip
}
}
output "PublicIP" {
value = aws_instance.Instance.public_ip
}
Variables file - vars.tf
variable "REGION" {
default = "us-east-2"
}
variable "ZONE1" {
default = "us-east-2a"
}
variable "USER" {
default = "ubuntu"
}
variable "AMIS" {
type = map(any)
default = {
us-east-2 = "ami-0fb653ca2d3203ac1"
us-east-1 = "ami-0e1d30f2c40c4c701"
}
}
To Store State Remotely in S3 Bucket¶
Create an S3 Bucket
terraform {
backend "s3" {
bucket = "terraform-state-009"
key = "terraform/remote"
region = "us-east-1"
}
}