0% found this document useful (0 votes)
180 views2 pages

Terraform Cheat Sheet

The document provides a cheatsheet on basic and advanced Terraform concepts like variables, maps, lists, objects, tuples, providers, resources, data sources, outputs, and commands. It covers infrastructure as code concepts and shows examples of deploying AWS resources like VPCs, EC2 instances, S3 buckets, security groups, and internet gateways as well as provider aliases and multi-cloud examples with AWS and Azure. The cheatsheet acts as a quick reference guide for common Terraform configurations, concepts, and commands.

Uploaded by

Towfiq Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views2 pages

Terraform Cheat Sheet

The document provides a cheatsheet on basic and advanced Terraform concepts like variables, maps, lists, objects, tuples, providers, resources, data sources, outputs, and commands. It covers infrastructure as code concepts and shows examples of deploying AWS resources like VPCs, EC2 instances, S3 buckets, security groups, and internet gateways as well as provider aliases and multi-cloud examples with AWS and Azure. The cheatsheet acts as a quick reference guide for common Terraform configurations, concepts, and commands.

Uploaded by

Towfiq Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy