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.