'Creating an ec2 instance in localstack with terraform hangs forever

When I try to create an ec2 instance in Localstack using terraform, it never completes. I am able to create an S3 bucket (with a file) using terraform.

I have the following Localstack terraform configuration:

variables.tf

variable "ami_id" {
  default = "ami-0c2d06d50ce30b442"
}

variable "instance_type" {
  default = "t2.micro"
}

variable "vpc_id" {
  default = "vpc-bc102dc4"
}

variable "port" {
  default = 22
}

variable "cidr_block" {
  default = "0.0.0.0/0"
}

outputs.tf

output "instance_id" {
  value = aws_instance.ec2-instance.public_ip
}

output "security_group" {
  value = aws_security_group.mysg.id
}

main.tf

# Configuration for which S3 cloud to connect to
provider "aws" {
  region                      = "us-east-1"
  access_key                  = "localstacktest"
  secret_key                  = "localstacktestkey"
  skip_credentials_validation = true
  skip_requesting_account_id  = true
  skip_metadata_api_check     = true
  s3_use_path_style           = true
  endpoints {
    ec2 = "http://localhost:4566"
    iam = "http://localhost:4566"
  }
}

# Setup our security group
resource "aws_security_group" "mysg" {
  name   = "allow_ssh"
  vpc_id = var.vpc_id

  ingress {
    description = "Allow inbound ssh traffic"
    cidr_blocks = [var.cidr_block]
    from_port   = var.port
    protocol    = "tcp"
    to_port     = var.port
  }

  tags = {
    name = "allow_ssh"
  }
}

# This will create ab ec2 instance
resource "aws_instance" "ec2-instance" {
  ami                    = var.ami_id
  instance_type          = var.instance_type
  vpc_security_group_ids = ["aws_security_group.mysg.id"]
}

My localstack was started with this compose file using the command docker-compose -f localstack.yml up

localstack.yml

version: '2.1'

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack
    ports:
      - "4566-4599:4566-4599"
      - "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
    environment:
      - SERVICES=s3,dynamodb,cloudformation,ec2
      - DEBUG=${DEBUG- }
      - DATA_DIR=${DATA_DIR- }
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
      - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
      - DOCKER_HOST=unix:///var/run/docker.sock
      - HOST_TMP_FOLDER=${TMPDIR}
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

When I run terraform apply, it simply prints (forever):

aws_instance.ec2-instance: Creating...
aws_security_group.mysg: Creating...
aws_instance.ec2-instance: Still creating... [10s elapsed]
aws_instance.ec2-instance: Still creating... [20s elapsed]
aws_instance.ec2-instance: Still creating... [30s elapsed]
aws_instance.ec2-instance: Still creating... [40s elapsed]
aws_instance.ec2-instance: Still creating... [50s elapsed]
aws_instance.ec2-instance: Still creating... [1m0s elapsed]
aws_instance.ec2-instance: Still creating... [1m10s elapsed]
aws_instance.ec2-instance: Still creating... [1m20s elapsed]
aws_instance.ec2-instance: Still creating... [1m30s elapsed]
...elided...

What could be going wrong? Am I missing some configuration?



Solution 1:[1]

Probably because the following:

vpc_security_group_ids = ["aws_security_group.mysg.id"]

should be:

vpc_security_group_ids = [aws_security_group.mysg.id]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin