Terraform Cheatsheets
Cheatsheets
Terraform CLI Usage
Terraform Format:
terraform fmt
Validate terraform syntax:
terraform validate
Validate terraform syntax and skip backend validation:
terraform validate -backend=false
Initialize and download providers:
terraform init
Terraform plan to review what will be changed:
terraform plan
Terraform Apply to create/delete resources:
terraform apply
To do a targeted plan:
terraform plan -target=module.platform -out=platform.tfplan
Then we can do a targeted destroy from the computed plan:
terraform apply "platform.tfplan"
Merge function
In this scenario, you can have default tags, but if you want to replace Environment
, you can define the var.environment
and include it in the merge function.
variable "default_tags" {
default = {
ManagedBy = "terraform"
Environment = "local"
}
}
variable "name" {
default = "testing"
}
variable "environment" {
default = "dev"
}
output "merge_tags" {
value = "${merge(var.default_tags, tomap({"Name" = "prefix-${var.name}", "Environment" = "${var.environment}"}))}"
}
Validations
Length:
variable "service_name" {
type = string
validation {
condition = length(var.service_name) < 40
error_message = "The service_name value cant be more than 40 characters."
}
}
Regex match:
variable "env_name" {
type = string
default = null
validation {
condition = can(regex("^(dev|staging|production|ephemeral-.*)+$", var.env_name))
error_message = "For the env_name value only dev, staging, production and ephemeral-* are allowed."
}
}
Booleans to control
We want to use a boolean variable to set the number:
- true = 1
- false = 0
warm_pool = {
pool_state = "Running"
min_size = var.warm_pool_enabled ? 1 : 0
max_group_prepared_capacity = var.warm_pool_enabled ? 1 : 0
}
Resources
Terraform Tutorials:
Terraform Examples:
- rtfm.co.ua - AWS Examples
- Jeff Geerling - Terraform and Vagrant Example
- Jeff Geerling - Mac Playbook Examples
- Kulasangar - AWS with IAM Policies Example
- AWS with IAM Policy Exampe #2
- phillipsj.net Terraform Blogposts
- Deploy EKS with Terraform
- ECS with Terraform
- Cross Account VPC Peering
- VPC Peering with AWS
- Multiple AWS Examples
- AWS API Gateway
- API GW: Moving from Serverless Framework
- Nodejs API GW with Lambda
- Minimal Viable CICD with Terraform and AWS CodePipeline
Learn Guides:
Terraform Cheatsheets
Terraform AWS Workshops:
Terraform with Ansible Examples:
- Terraform with Ansible Example #1
- Terraform with Ansible Example #2
-
Using a JumpHost in Ansible with Terraform Lambda Auto Package
Terraform States:
Terraform Providers: