Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform with Localstack basic setup doesn't work

I want to try basic setup for localstack with terraform. My docker-compose file, from the localstack docs

version: "3.8"

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack
    ports:
      - "127.0.0.1:4566:4566"            # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559"  # external services port range
      - "127.0.0.1:53:53"                # DNS config (only required for Pro)
      - "127.0.0.1:53:53/udp"            # DNS config (only required for Pro)
      - "127.0.0.1:443:443"              # LocalStack HTTPS Gateway (only required for Pro)
    environment:
      - DEBUG=1
      - LOCALSTACK_HOSTNAME=localhost
      - PERSISTENCE=${PERSISTENCE-}
      - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
      - LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY-}  # only required for Pro
      - DOCKER_HOST=unix:///var/run/docker.sock
      - SERVICES=s3
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

My terraform file to create s3 bucket

# Public Cloud Configuration
provider "aws" {
  region                      = "us-east-1"
  access_key                  = "test123"
  secret_key                  = "testabc"
  skip_credentials_validation = true
  skip_requesting_account_id  = true
  skip_metadata_api_check     = true
  endpoints {
    s3 = "http://localhost:4566"
  }
}

# Create Bucket
resource "aws_s3_bucket" "my_bucket" {
  bucket = "bucket"
}

I got the next error when running the command terraform apply

│ Error: creating Amazon S3 (Simple Storage) Bucket (bucket): RequestError: send request failed
│ caused by: Put "http://bucket.localhost:4566/": dial tcp: lookup bucket.localhost on 10.222.50.10:53: no such host
│
│   with aws_s3_bucket.my_bucket,
│   on main.tf line 15, in resource "aws_s3_bucket" "my_bucket":
│   15: resource "aws_s3_bucket" "my_bucket" {

I'm able to create s3 bucket manually in the localstack with command like aws --endpoint-url http://localhost:4566 s3 mb s3://user-uploads

docker ps output

c9497bcff0e3   localstack/localstack       "docker-entrypoint.sh"   23 minutes ago   Up 18 minutes (healthy)   127.0.0.1:53->53/tcp, 127.0.0.1:443->443/tcp, 127.0.0.1:4510-4559->4510-4559/tcp, 127.0.0.1:4566->4566/tcp, 127.0.0.1:53->53/udp, 5678/tcp   localstack_main

So why localstack is trying to access some different address like 192.168.178.1:53? Do I need to specify somewhere different address? Checked a number of tutorials and for everyone, the setup works fine.

like image 963
user2105282 Avatar asked Oct 20 '25 01:10

user2105282


1 Answers

I solve it adding a property in provider aws

s3_use_path_style = true

Sample:

terraform {
  required_version = ">= 0.12"
  backend local {}
}

provider "aws" {
  region                      = "localhost"
  access_key                  = "local"
  secret_key                  = "local"
  skip_region_validation      = true
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
  s3_use_path_style           = true // <<- this property here

  endpoints {
    dynamodb = "http://localhost:4566"
    s3   = "http://localhost:4566"
  }
}

// S3
resource "aws_s3_bucket" "my-bucket" {
  bucket = "teste"
}

I hope help you

like image 80
Vitor Momberg Avatar answered Oct 22 '25 20:10

Vitor Momberg