---
title: "Infrastructure as Code on AWS with Terraform Cheat Sheet | Cybr"
description: "Deploy infrastructure securely and repeatably on the AWS cloud."
url: https://cybr.com/cheat-sheets/terraform-on-aws/
---

# Infrastructure as Code on AWS with Terraform

Deploy infrastructure securely and repeatably on the AWS cloud.

![Terraform](https://cybr.com/hands-on-labs/icons/terraform.svg)

 [**Cybr**](https://cybr.com/) [cybr.com](https://cybr.com/) [Christophe Limpalair](https://www.linkedin.com/in/christophelimpalair/)

## 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](https://cybr.com/aws-security-services/icons/Arch_AWS-CloudFormation_64.svg) ![Amazon EC2](https://cybr.com/aws-security-services/icons/Arch_Amazon-EC2_64.svg) ![Amazon CloudWatch](https://cybr.com/aws-security-services/icons/Arch_Amazon-CloudWatch_64.svg) ![Amazon S3](https://cybr.com/hands-on-labs/icons/amazon-s3.svg)

Google CloudAzure

## Important Commands

`› terraform -h`Usage: 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.tf`Core infrastructure code

`variables.tf`Defines variables used in the project

`output.tf`Define output values

`terraform.tfvars`Sets values for variables defined in variables.tf

`provider.tf`Configure providers like AWS, Azure, ...

`backend.tf`Configure 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 Doing**Terraform on AWS: From Zero to Cloud Infrastructure****Course & Hands-On Labs***↗ https://cybr.com/terraform*](https://cybr.com/courses/terraform-on-aws-from-zero-to-cloud-infrastructure/)

## Use this cheat sheet with AI

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

Goal

Preview prompt

Prompt

Sources and
more info: [**Terraform Documentation:** https://developer.hashicorp.com/terraform](https://developer.hashicorp.com/terraform) [**Cybr’s Terraform & AWS Security Hands-On Labs:** https://cybr.com/hands-on-labs](https://cybr.com/hands-on-labs/) [**Terraform on AWS Course:** https://cybr.com/terraform](https://cybr.com/courses/terraform-on-aws-from-zero-to-cloud-infrastructure/)

[More cheat sheets](https://cybr.com/cheat-sheets/)
