Terraform Cheat Sheet

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

1 Terraform Cheatsheet

Basic S3
provider "aws" { resource "aws_s3_bucket" "finance" {
region = "us-east-2" bucket = "towfiqhassan-20220903"
} tags = {
Description = "Towfiq Terraform"
resource "aws_vpc" "myvpc" { }
cidr_block = "10.0.0.0/16" }
tags = {
Name = "VPC-Trial" EC2
} resource "aws_instance" "webserver" {
} ami = "ami-0568773882d492fc8"
instance_type = "t2.micro"
}
Variables List
variable "mylist" {
String type = list
variable "vpcname" {
default = ["10.10.0.0/16", "10.20.0.0/16"]
type = string
}
default = "TH-vpc"
}
for_each = toset(var.mylist)
resource "aws_vpc" "myvpc" { each.value
cidr_block = "10.0.0.0/16"
tags = { each.key – refers to index position of the value (i.e: 0,1,2,3 etc)
Name = var.vpcname
}
}
object
variable "rg_config" {
Map type = object({
variable "mymap" { create_rg = bool
type = map name = string
default = { VPC1 = "10.10.0.0/16“ VPC2 = location = string
"10.20.0.0/16"} })
} }

for_each = var.mymap tuple


cidr_block = each.value variable "launchDate" {
Name = each.key type = tuple([number, string]) default = [4,
"September"]
}

Data and Output IGW


provider "aws" { resource "aws_internet_gateway" "ig" {
region = "us-east-2" vpc_id = aws_vpc.myvpc.id
}
tags = {
resource "aws_s3_bucket" "finance" { Name = "VPC-PLAY-IGW"
bucket = "towfiqhassan-20221031" }
tags = { }
Description = "Towfiq Terraform"
}
} Security Group
resource "aws_security_group" "webtraffic" {
data "aws_s3_bucket" "data-bucket" { name = "Allow HTTPS"
bucket = "towfiqhassan-20221031"
}
ingress {
output "s3_bucket_towfiq" { from_port = 443
value = data.aws_s3_bucket.data-bucket.arn to_port = 443
} protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 32768
to_port = 60999
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
}
}
AWS AZURE
terraform { terraform {
required_version = “~> 1.2.0” required_version = “~> 1.2.0”
required_providers { required_providers {
aws = { azurerm = {
source = "hashicorp/aws" source = "hashicorp/azurerm"
version = "4.37.0" version = "3.29.1"
} }
} }
} }

provider "aws" { provider "azurerm" {


region = "us-east-1" features {}
} }

Provider alias Terraform Command


provider "aws" {
region = "us-east-2"
alias = "OHIO"
}

provider "aws" {
region = "ap-southeast-1"
alias = "SINGAPORE"
}

resource "aws_vpc" "usEast-vpc" {

cidr_block = "10.10.0.0/16"
provider = aws.OHIO

tags = {
Name = "VPC-IN-OHIO"
}
}

resource "aws_vpc" "singapore-vpc" {

cidr_block = "10.20.0.0/16"
provider = aws.SINGAPORE

tags = {
Name = "VPC-IN-SINGAPORE"
}
}

2 Misc
Question: terraform plan, you notice that a resource has a tilde (~) next to it

Answer: the resource will be updated in place

You might also like