Terraform on AWS [Cheat Sheet]
So you need to deploy new or manage existing cloud infrastructure. What’s the best approach to get it done? The best way is to use…
So you need to deploy new or manage existing cloud infrastructure. What’s the best approach to get it done? The best way is to use Infrastructure as Code (IaC), which allows you to define and manage your infrastructure through code.
Rather than manually navigating the AWS console to create, configure, or update resources, (aka “ClickOps”) you describe your desired infrastructure state using code, and your IaC of choice will make sure that your infrastructure is in that state.
This has a lot of benefits for your cloud infrastructure, including:
- It makes it consistent and repeatable and helps prevent and correct drift
- It becomes version controlled
- You can run it through functional and security tests before deploying
- It enables automation that wouldn’t otherwise be possible
- It simplifies multi-account and multi-cloud deployments
The coding language you use depends entirely on the IaC tool you’re using. For example, Terraform created its own configuration language called HCL. Whereas an alternative like Pulumi gives you the choice between JS/TypeScript, Go, Python, YAML, and more. CloudFormation gives you either YAML or JSON as options. Etc..
To celebrate Cybr’s latest course Terraform on AWS: From Zero to Cloud Infrastructure, we created a cheat sheet to help you get started using Terraform.
This cheat sheet covers essential CLI commands, outlines the basic file structure (with guidance on what to include in each file), and explains how Terraform state tracks the status of resources, mapping them to your configuration. It also provides insights into providers, resources, defining and using variables, and introduces you to creating modules to effectively define, organize, and reuse your code.
Feel free to download it, share it, and use it as a reference guide!
Infrastructure as Code on AWS with Terraform
Deploy infrastructure securely and repeatably on the AWS cloud.
What is Terraform and Infrastructure as Code (IaC)?
Terraform is an Infrastructure as Code (IaC) tool that allows you to define and manage your infrastructure through code. Rather than manually navigating the AWS console to create, configure, or update resources, you describe your desired infrastructure state in code. Terraform then automates the process by provisioning and managing the resources to match the specified configuration, ensuring consistency and reducing the potential for human error.
This does a few things to your cloud infrastructure:
- It makes it consistent and repeatable
- It becomes version controlled
- You can run it through functional and security tests before deploying
- It enables automation that wouldn’t otherwise be possible
- It simplifies multi-account and multi-cloud deployments
- Pulumi
- OpenTofu
- CloudFormation / CDK
- Azure Resource Manager
Important Commands
› terraform -hUsage: terraform [global options] [args]...› terraform initInitializes a Terraform working directory. Run this after writing Terraform configuration files or cloning an exiting project.
› terraform planShow changes required by the current configuration.
› terraform applyApply changes to reach the desired state of the configuration.
› terraform destroyDestroy Terraform-managed infrastructure.
› terraform fmtFormat Terraform code according to style conventions.
› terraform validateValidate the Terraform configuration files.
› terraform outputDisplay output values from the state file.
› terraform stateView, move, or remove items in the Terraform state.
› terraform importImport existing infrastructure into Terraform.
Basic File Structure
main.tfCore infrastructure codevariables.tfDefines variables used in the projectoutput.tfDefine output valuesterraform.tfvarsSets values for variables defined in variables.tfprovider.tfConfigure providers like AWS, Azure, ...backend.tfConfigure the backend to store the stateTerraform State
State tracks the current status of resources, mapping them to your configuration code. This enables Terraform to determine what changes are needed to align your infrastructure with the desired state.
- State file(
terraform.tfstate) - Stores the current state of your infrastructure. - Remote state - Store the state file remotely (ie: for team collaboration).
- State locking - Prevent simultaneous modifications to state.
- Terraform state list - List resources currently in the state.
- Terraform state rm - Remove a resource from the state.
Terraform Configuration
Providers
Define providers (ie: AWS, Azure, GCP) and their configuration (ie: version, credentials, etc)
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.63.1"
}
}
}provider "aws" {
region = var.aws_region
}Resources
Define infrastructure components
resource "aws_s3_bucket" "example" {
bucket = "my-example-bucket"
acl = "private"
}Variables
Defining & using variables:
variable "bucket_name" {
description = "The name of the S3 bucket"
type = string
default = "my-default-bucket"
}
variable "bucket_acl" {
description = "The access control list for the bucket"
type = string
default = "private"
}bucket_name = "cybr-is-awesome"
bucket_acl = "private"resource "aws_s3_bucket" "example" {
bucket = var.bucket_name
acl = var.bucket_acl
}Outputs
output "instance_ip" {
value = aws_instance.example.public_ip
}Modules
Modules enable you to define, organize, and reuse code
module "network" {
source = "./modules/network"
vpc_id = var.vpc_id
}module "s3_bucket" {
source = "./modules/s3_bucket"
bucket_name = var.bucket_name
bucket_acl = var.bucket_acl
}Modules File Structure
$ tree complete-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
├── ...
├── modules/
│ ├── nestedA/
│ │ ├── README.md
│ │ ├── variables.tf
│ │ ├── main.tf
│ │ └── outputs.tf
│ └── nestedB/
│ └── .../
└── examples/
├── exampleA/
│ └── main.tf
└── exampleB/
└── .../Use this cheat sheet with AI
Copy a ready-to-use prompt into your preferred AI assistant.
Preview prompt
If you want to learn more and get started with IaC and Terraform on AWS, check out our hands-on and project-based course.
