docker-composeterraformlocalstackaws-rest-api

Dockerized Localstack Terraform Cannot Create Simple Resource


I have a simple setup where I only want to start localstack through docker-compose, then run a terraform file to create an aws_api_gateway_rest_api resource. However, it fails to connect to localstack's restapis endpoint "http://localhost:4567/restapis".

Here is the code for docker-compose.yml

version: '3'
services:

  localstack:
    image: localstack/localstack
    ports:
      - 4566:4566
      - 4567:4567
      - 4569:4569
      - 4572:4572
      - 4574:4574
      - 4575:4575
      - 4576:4576
      - 4593:4593
      - 4510-4559:4510-4559
    environment:
      - SERVICES=restapis,s3,lambda,dynamodb,sns,sqs,apigateway,iam

Here is the code for main.tf

provider "aws" {
  region = "ap-southeast-2"

  endpoints {
    s3      = "http://localhost:4572"
    lambda  = "http://localhost:4574"
    dynamodb = "http://localhost:4569"
    sns     = "http://localhost:4575"
    sqs     = "http://localhost:4576"
    apigateway = "http://localhost:4567"
    iam     = "http://localhost:4593"
  }
}

resource "aws_api_gateway_rest_api" "example_api" {
  name = "example_api"
}

Commands:

terraform init

terraform plan

terraform apply

The error I get is:

Error: creating API Gateway REST API (example_api): RequestError: send request failed
│ caused by: Post "http://localhost:4567/restapis": read tcp 127.0.0.1:46738->127.0.0.1:4567: read: connection reset by peer

Am I missing something? Shouldn't restapis be available on that port? I am able to hit the S3 endpoint (and various others) with no problems.


Solution

  • I was not able to reproduce your issue, as I'm missing some essential info, such as versions; however, here are a few ideas to get things working: it looks like you're using an older version, and LocalStack does not maintain earlier versions, so you're encouraged to use the latest image, with this docker-compose file. With that, all services are standardized to listen on port 4566. Lastly, to simplify things, please use the provided terraform wrapper, tflocal, which automatically configures the local service endpoints, allowing you to run the same config file as you would on AWS. Hope this helps.