Terraform apply fails with connection refused when LocalStack runs in separate Docker container
criticalWhen running Terraform with tflocal inside a Docker container against LocalStack in another container, terraform apply fails with 'dial tcp: lookup s3.localhost.localstack.cloud: no such host' or 'dial tcp 127.0.0.1:4566: connect: connection refused'. The root cause is that localhost inside the Terraform container refers to that container itself, not the LocalStack service. Terraform init succeeds but apply fails during actual AWS API calls like S3 bucket creation.
Option A (recommended): Create terraform_override.tf with provider AWS endpoints block setting s3 = "http://localstack:4566" to override tflocal's localhost-based configuration. Option B: Set S3_HOSTNAME environment variable to "localstack:4566" in the Terraform container. Verify both containers are on the same Docker network. Test connectivity using: docker compose run --rm --entrypoint curl local_terraform_setup http://localstack:4566/_localstack/health. Remove existing override files before applying.