16

Terraform can be configured with custom S3 endpoints and it seems that localstack can create local stacks for S3, SES, Cloudformation and few others services.

The question is what to write in Terraform configuration to use localstack's S3 endpoint?

TylerH
  • 20,799
  • 66
  • 75
  • 101
cphoover
  • 368
  • 3
  • 7

2 Answers2

13

Terraform does not officially support "AWS-workalike" systems, since they often have subtle quirks and differences relative to AWS itself. However, it is supported on a best-effort basis and may work if localstack is able to provide a sufficiently realistic impression of S3 for Terraform's purposes.

According to the localstack docs, by default the S3 API is exposed at http://localhost:4572 , so setting the custom endpoint this way may work:

provider "aws" {
  endpoints {
    s3 = "http://localhost:4572"
  }
}

Depending on the capabilities of localstack, you may need to set some other settings:

  • s3_force_path_style to use a path-based addressing scheme for buckets and objects.
  • skip_credentials_validation, since localstack seems to lack an implementation of the AWS token service.
  • skip_metadata_api_check if IAM-style credentials will not be used, to prevent Terraform from trying to get credentials from the EC2 metadata API.
Martin Atkins
  • 62,420
  • 8
  • 120
  • 138
11

Building off @martin-atkins’ answer, here’s a sample Terraform file that works with Localstack:

provider "aws" {
  region = "us-east-1"
  access_key = "anaccesskey"
  secret_key = "asecretkey"
  skip_credentials_validation = true
  skip_metadata_api_check = true
  s3_force_path_style = true
  endpoints {
    s3 = "http://localhost:4572"
  }
}

resource "aws_s3_bucket" "b" {
  bucket = "my-tf-test-bucket"
  acl    = "public-read"
}
Geoffrey Booth
  • 7,168
  • 5
  • 35
  • 42