Infrastructure as Code on AWS with Terraform

Deploy infrastructure securely and repeatably on the AWS cloud.

Terraform

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
Common alternatives:
  • Pulumi
  • OpenTofu
  • CloudFormation / CDK
  • Azure Resource Manager
AWS
AWS CloudFormation Amazon EC2 Amazon CloudWatch Amazon S3
Google CloudAzure

Important Commands

› terraform -hUsage: terraform [global options] [args]...
› terraform init

Initializes a Terraform working directory. Run this after writing Terraform configuration files or cloning an exiting project.

› terraform plan

Show changes required by the current configuration.

› terraform apply

Apply changes to reach the desired state of the configuration.

› terraform destroy

Destroy Terraform-managed infrastructure.

› terraform fmt

Format Terraform code according to style conventions.

› terraform validate

Validate the Terraform configuration files.

› terraform output

Display output values from the state file.

› terraform state

View, move, or remove items in the Terraform state.

› terraform import

Import existing infrastructure into Terraform.

Basic File Structure

main.tfCore infrastructure code
variables.tfDefines variables used in the project
output.tfDefine output values
terraform.tfvarsSets values for variables defined in variables.tf
provider.tfConfigure providers like AWS, Azure, ...
backend.tfConfigure the backend to store the state

Terraform 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)

provider.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.63.1"
    }
  }
}
provider.tf
provider "aws" {
  region = var.aws_region
}

Resources

Define infrastructure components

main.tf
resource "aws_s3_bucket" "example" {
  bucket = "my-example-bucket"
  acl    = "private"
}

Variables

Defining & using variables:

variables.tf
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"
}
terraform.tfvars
bucket_name = "cybr-is-awesome"
bucket_acl  = "private"
main.tf
resource "aws_s3_bucket" "example" {
  bucket = var.bucket_name
  acl    = var.bucket_acl
}

Outputs

output.tf
output "instance_ip" {
  value = aws_instance.example.public_ip
}

Modules

Modules enable you to define, organize, and reuse code

modules/vpc/main.tf
module "network" {
  source = "./modules/network"
  vpc_id = var.vpc_id
}
modules/s3/main.tf
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/
        └── .../
Learn Terraform By DoingTerraform on AWS: From Zero to Cloud InfrastructureCourse & Hands-On Labs↗ https://cybr.com/terraform

Use this cheat sheet with AI

Copy a ready-to-use prompt into your preferred AI assistant.

Preview prompt