Terraform – Notes – Day 2 – August
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
terraform init
terraform validate
terraform plan
terraform apply
terraform show
terraform output
terraform destory
Training Material
- https://devopsschool.com/slides/terraform/index.html
- https://www.devopsschool.com/blog/?s=terraform
- Recording
Revision
- What is Terraform?
- Terraform installation
- Terraform Basic Workflow
- Terraform Variables
Variables Labs
variable "instance_count" {
type = number
default = 1
}
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
}
variable "instance_name" {
type = string
default = "my-instance"
}
resource "aws_instance" "example-number" {
count = var.instance_count
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
}
variable "security_groups" {
type = list(string)
default = ["sg-0541801a7a059ba17"]
}
resource "aws_instance" "example-list" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
vpc_security_group_ids = var.security_groups
}
variable "instance_tags" {
type = map(string)
default = {
Name = "my-instance"
}
}
resource "aws_instance" "example-map" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = var.instance_tags
}
variable "create_vm" {
description = "If set to true, it will create vm"
type = bool
}
resource "aws_instance" "example-bool" {
count = var.create_vm ? 1 : 0
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = var.instance_tags
}
How to set conditions in Terraform?
How to set conditions in Terraform?
https://www.devopsschool.com/blog/terraform-conditioning-example-program
GLOBAL ARGUMENT == Meta Arugumnets OF each Resources
https://www.devopsschool.com/blog/terraform-tutorials-meta-arguments
https://developer.hashicorp.com/terraform/language/meta-arguments/depends_on
resources
arg1=value
arg2=value
arg3=value
count
for_each
depends_on
implicit
explicit
res1 should exec
if some vars are set or
variable "create_instance" {
description = "If set to true, it will create vm"
type = bool
}
resource "aws_instance" "example-expressor" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
count = var.create_instance ? 1 : 0
tags = {
Name = "My EC2 Instance"
}
}
variable "instance_type" {
default = "t2.micro"
}
resource "aws_instance" "example-depends-on" {
ami = "ami-053b0d53c279acc90"
instance_type = var.instance_type
tags = {
Name = "My EC2 Instance"
}
}
resource "aws_s3_bucket" "example_bucket" {
depends_on = [aws_instance.example-depends-on]
bucket = "my-unique-bucket-name-dksjdhsakjdhakjhdkj"
acl = "private"
}
How to loop / iterate in Terraform?
- count
- for_each
https://www.devopsschool.com/blog/how-to-do-looping-iterations-in-terraform/
resource "aws_instance" "example" {
count = 2
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
}
variable "repo-name" {
type = map
default = {
"repo1" = "devops1"
"repo2" = "devops2"
"repo3" = "devops3"
}
}
resource "github_repository" "repogithub" {
for_each = var.repo-name
name = "${each.value}-devopsschool-xyz"
}
each.key
each.value
Provisonar
Provisonar
----------------------------
https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax
Terraform provisioners Tutorials and Complete Guide
Terrafrom – Example Code for remote-exec, local-exec & file provisioner
file
remote-exec
local-exec
What is User Data and Custom data?
-------------------------------------
Ansible | Chef | Puppet? -----> Configuring servers
resource "aws_instance" "first-ec2" {
ami = "ami-053b0d53c279acc90" # us-west-2
instance_type = "t2.micro"
key_name = "ansiblesunday"
tags = {
Name = "RajeshKumar"
}
connection {
type = "ssh"
user = "ubuntu"
private_key = file("ansiblesunday.pem")
#host = aws_instance.web.public_ip
host = self.public_ip
}
provisioner "local-exec" {
command = "mkdir devopsschool-local"
}
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo apt-get install apache2 -y",
"sudo systemctl start apache2",
]
}
provisioner "file" {
source = "providers.tf"
destination = "/tmp/providers.tf"
}
}
Module
===========================================
https://www.devopsschool.com/blog/terraform-modules-tutorials-and-complete-guide/
Terraform Modules explained!!!
Decide factor to write Terraform Module or not?
How to use one module’s variable in another module in terraform?
Dir
contains
main.tf
mod dir1
mod dir2
1.tf
2.tf
3.tf
tfvars
dir2 = app
1.tf
2.tf
3.tf
tfvars
dir3 == db
1.tf
2.tf
3.tf
tfvars
dir4 == vpc
1.tf
2.tf
3.tf
tfvars
==================================
ROOT MODULE
CHILE MODULE
===============================================
Develop your own Module
Use a Module dev by Someone else ------>
VPC
SUBNET
ROUTET
IGW
NATGW
SECGR
VPN
----------------
learn --> Code -> test -> USE It ENV
===========MAIN.TF=========
module "appserver" {
source = "./db"
}
module "dbserver" {
source = "./app"
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "my-rajesh"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b", "us-east-1c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
enable_nat_gateway = false
enable_vpn_gateway = false
tags = {
Terraform = "true"
Environment = "dev"
}
}