🎓 A1Learner
🔍
0 of 50 lessons completed

Your First Real Resource

A real resource is a cloud resource managed by a provider, such as an S3 bucket on AWS. This lesson creates one on a local AWS emulator, not on live AWS.

Point the provider at the emulator

The endpoints block sends S3 calls to the local emulator. The region is still set, as AWS expects one.

provider "aws" {
  region = "us-east-1"
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
  s3_use_path_style = true
  endpoints {
    s3 = "http://localhost:5000"
  }
}

Dummy credentials

The emulator accepts any values. These two lines set them for the terminal session.

export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test

One bucket

The resource block names the bucket. Terraform creates it on the emulator.

resource "aws_s3_bucket" "first" {
  bucket = "my-first-bucket-a1learner"
}

Example

# main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.72.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
  s3_use_path_style = true
  endpoints {
    s3 = "http://localhost:5000"
  }
}

resource "aws_s3_bucket" "first" {
  bucket = "my-first-bucket-a1learner"
}

# Commands:
# export AWS_ACCESS_KEY_ID=test
# export AWS_SECRET_ACCESS_KEY=test
# terraform init
# terraform plan
# terraform apply
# terraform destroy

Key points

  • An S3 bucket is a container for files in AWS storage.
  • The AWS provider talks to AWS, or to an emulator that acts like AWS.
  • This lesson uses moto_server, a local AWS emulator running on this machine.
  • The endpoint setting points the provider at the emulator instead of AWS.
  • Dummy credentials are set with environment variables, because the emulator does not check them.
  • No live account is used, and no cloud bill is created.

The provider points at the emulator on localhost. The bucket is created there, then destroy removes it, so practice costs nothing.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Where does the first real resource in this lesson run?