Terraform
Terraform
Infrastructure as a Code
Introduction
❖ IAAC | Automate Infrastructure
❖ Define Infrastructure State
❖ Ansible, puppet or chef automates mostly OS related
tasks.
➢ Defines machines state
❖ Terraform automates infra itself
➢ Like AWS, GCP, Azure, digital ocean etc
2
Introduction
❖ Terraform works with automation softwares like ansible
after infra is setup and ready.
❖ No Programming, its own syntax similar to JSON.
3
Everything
Needs
Automation
Infrastructure automation centralized.
4
Installation
Download Terraform binary from its website
❖ Linux
❖ Mac
❖ Windows
5
Launch ec2 instance
❖ AWS Account
6
Exercise
➔ Write instance.tf file
➔ Launch instance
➔ Make some changes to instance.tf file
➔ Apply changes.
Summarizing
instance.tf
provider "aws" {
access_key = "ACCESS_KEY"
secret_key = "SECRET_KEY"
region = "ap-south-1"
}
8
Summarizing
terraform plan
+ create
9
Summarizing
terraform apply
aws_instance.intro: Creating...
aws_instance.intro: Still creating... [10s elapsed]
aws_instance.intro: Still creating... [20s elapsed]
aws_instance.intro: Still creating... [30s elapsed]
aws_instance.intro: Creation complete after 31s [id=i-047d7ea789e081807]
10
Summarizing
terraform destroy
Plan: 0 to add, 0 to change, 1 to destroy.
11
Variables
➔ Move secrets to another file
provider "aws" {
#access_key = "ACCESS_KEY"
#secret_key = "SECRET_KEY"
region = "ap-south-1"
}
13
providers.tf vars.tf
provider "aws" {
variable REGION {
region = var.REGION
default = "us-west-1"
}
}
terraform.tfvars instance.tf
AWS_ACCESS_KEY = "" resource "aws_instance" "intro" {
AWS_SECRET_KEY = "" ami = "ami-009110a2bf8d7dd0a"
instance_type = "t2.micro"
}
14
providers.tf vars.tf
variable AWS_ACCESS_KEY {}
provider "aws" {
variable AWS_SECRET_KEY {}
region = var.REGION
variable REGION {
}
default = "us-west-1"
}
variable AMIS {
instance.tf type = "map"
resource "aws_instance" "intro" { default {
ami = var.AMIS[var.REGION] us-west-1 = "ami-06397100adf427136"
instance_type = "t2.micro" us-west-2 = "ami-a042f4d8"
} }
}
15
Exercise
➔ Write providers.tf file
➔ Write vars.tf file
➔ Write instance.tf file
➔ Apply Changes
➔ Make some changes to instance.tf file
➔ Apply changes.
Provisioning
❖ Build Custom Images with tools like packer
WinRM
SSH provisioner "file" {
provisioner "file" { source = "conf/myapp.conf"
source = "files/test.conf" destination = "C:/App/myapp.conf"
destination = "/etc/test.conf"
connection {
connection { type = "winrm"
type = "ssh" user = "Administrator"
user = "root" password = var.admin_password
}
password = var.root_password
}
}
}
18
More Provisioner
❖ The file provisioner is used to copy files or directories
19
More Provisioner
❖ The puppet provisioner installs, configures and runs the Puppet
agent on a remote resource.
➢ Supports both ssh and winrm type connections.
21
Key Pair & instance Resources
resource "aws_key_pair" "dove-key" {
key_name = "dovekey"
public_key = file("dovekey.pub")
}
23
Remote-exec Provisioner
provisioner "remote-exec" {
inline = [
"chmod u+x /tmp/web.sh",
"sudo /tmp/web.sh"
]
}
24
Exercise
● Generate key pair
● Write script
● Write providers.tf
● Write vars.tf
● Write instances.tf
○ key pair resource
○ aws_instance resource
■ provisioners
● file
● remote-exec
● Apply changes.
Output Information
● Terraform stores returned value of all resources created.
○ e:g aws_instance resource has the attribute public_ip
27
Store Output in File
resource "aws_instance" "out_inst" {
ami = var.AMIS[var.REGION]
instance_type = "t2.micro"
key_name = aws_key_pair.dino-key.key_name
provisioner "local-exec" {
command = "echo aws_instance.out_inst.private_ip >>
private_ips.txt"
}
}
28