0
0
Terraformcloud~5 mins

Creating your first resource in Terraform - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
When you want to build infrastructure using code, you start by defining resources. Terraform lets you write simple files to create things like servers or storage automatically.
When you want to launch a virtual server in the cloud without clicking in a web console.
When you need to create a storage bucket to save files for your app.
When you want to set up a database instance quickly and repeatably.
When you want to manage your infrastructure in a safe, trackable way using code.
When you want to share your infrastructure setup with teammates easily.
Config File - main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "my_bucket" {
  bucket = "example-terraform-bucket-12345"
  acl    = "private"
}

This file tells Terraform to use the AWS provider in the us-east-1 region.

The aws_s3_bucket resource creates a new S3 bucket named example-terraform-bucket-12345 with private access.

Commands
This command sets up Terraform in your folder by downloading the AWS provider plugin. It prepares Terraform to work with AWS.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.60.0... - Installed hashicorp/aws v4.60.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes required for your infrastructure.
This command shows what Terraform will do before making any changes. It helps you check that your resource will be created as expected.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.my_bucket will be created + resource "aws_s3_bucket" "my_bucket" { + acl = "private" + bucket = "example-terraform-bucket-12345" + force_destroy = false + id = (known after apply) + region = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.
This command creates the S3 bucket as defined in your file. The flag skips the manual approval step to speed up the process.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.my_bucket: Creating... aws_s3_bucket.my_bucket: Creation complete after 2s [id=example-terraform-bucket-12345] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips asking for confirmation before applying changes
This command displays the current state of your infrastructure as Terraform knows it, confirming your bucket was created.
Terminal
terraform show
Expected OutputExpected
aws_s3_bucket.my_bucket: id = example-terraform-bucket-12345 arn = arn:aws:s3:::example-terraform-bucket-12345 bucket = example-terraform-bucket-12345 acl = private region = us-east-1
Key Concept

If you remember nothing else from this pattern, remember: Terraform uses simple files to describe resources, then commands to create and check them safely.

Common Mistakes
Not running 'terraform init' before other commands
Terraform needs to download provider plugins first; without init, commands fail.
Always run 'terraform init' once before planning or applying.
Using a bucket name that already exists globally
S3 bucket names must be unique worldwide; duplicate names cause errors.
Choose a unique bucket name, often by adding random numbers or your company name.
Running 'terraform apply' without reviewing the plan
You might create or change resources unintentionally.
Run 'terraform plan' first to see what will happen, then apply.
Summary
Write a Terraform file describing the resource you want to create.
Run 'terraform init' to prepare Terraform and download providers.
Use 'terraform plan' to preview changes before applying.
Run 'terraform apply' to create the resource in your cloud account.
Use 'terraform show' to see the current state of your resources.