0
0
Terraformcloud~5 mins

Resource arguments and attributes in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create infrastructure with Terraform, you tell it what you want using resource arguments. After creation, Terraform gives you resource attributes that show details about what was made. This helps you manage and connect your cloud resources easily.
When you want to create a virtual machine with specific settings like size and region.
When you need to set up a storage bucket and want to know its URL after creation.
When you want to connect one resource to another using information Terraform provides after deployment.
When you want to customize a resource by giving it names, tags, or other options.
When you want to check the current state or properties of a resource after Terraform applies changes.
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" "example_bucket" {
  bucket = "my-example-bucket-terraform-12345"
  acl    = "private"
  tags = {
    Environment = "Dev"
    Project     = "TerraformDemo"
  }
}

output "bucket_arn" {
  value = aws_s3_bucket.example_bucket.arn
}

This Terraform file sets up the AWS provider to work in the us-east-1 region.

The aws_s3_bucket resource creates a private S3 bucket with a specific name and tags.

The bucket and acl are resource arguments that tell Terraform what to create.

The output block shows the bucket's ARN attribute after creation, which is useful for connecting or referencing this bucket elsewhere.

Commands
This command sets up Terraform in the current folder by downloading the AWS provider plugin. It prepares Terraform to create resources.
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 that are required for your infrastructure.
This command shows what Terraform will do before making any changes. It uses the resource arguments to plan creating the S3 bucket.
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.example_bucket will be created + resource "aws_s3_bucket" "example_bucket" { + acl = "private" + arn = (known after apply) + bucket = "my-example-bucket-terraform-12345" + id = (known after apply) + tags = { + "Environment" = "Dev" + "Project" = "TerraformDemo" } } Plan: 1 to add, 0 to change, 0 to destroy.
This command creates the S3 bucket using the arguments defined. The -auto-approve flag skips asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Creating... aws_s3_bucket.example_bucket: Creation complete after 3s [id=my-example-bucket-terraform-12345] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command shows the ARN attribute of the created S3 bucket. It helps you see details Terraform knows after creation.
Terminal
terraform output bucket_arn
Expected OutputExpected
arn:aws:s3:::my-example-bucket-terraform-12345
Key Concept

If you remember nothing else from this pattern, remember: resource arguments tell Terraform what to create, and resource attributes show details about what was created.

Common Mistakes
Using resource attributes as arguments inside the same resource block.
Terraform cannot use attributes that only exist after creation as input arguments during creation.
Use resource arguments to define what you want, and access attributes only after the resource is created or in other resources.
Not running 'terraform init' before 'terraform plan' or 'apply'.
Terraform needs to download provider plugins before it can work, so commands fail without initialization.
Always run 'terraform init' first in a new Terraform project folder.
Summary
Use resource arguments in Terraform files to specify the settings for the infrastructure you want to create.
Run 'terraform init' to prepare Terraform, then 'terraform plan' to see what will happen.
Run 'terraform apply' to create resources, then use 'terraform output' to see resource attributes.