使用 terraform 在 localstack 中创建 ec2 实例永远挂起
Creating an ec2 instance in localstack with terraform hangs forever
当我尝试使用 terraform 在 Localstack 中创建 ec2 实例时,它从未完成。我 能够使用 terraform 创建 S3 存储桶(带有文件)。
我有以下 Localstack terraform 配置:
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"]
}
我的 localstack 是使用命令 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"
当我 运行 terraform apply
时,它只是打印(永远):
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...
可能出了什么问题?我是否缺少某些配置?
可能是因为:
vpc_security_group_ids = ["aws_security_group.mysg.id"]
应该是:
vpc_security_group_ids = [aws_security_group.mysg.id]
当我尝试使用 terraform 在 Localstack 中创建 ec2 实例时,它从未完成。我 能够使用 terraform 创建 S3 存储桶(带有文件)。
我有以下 Localstack terraform 配置:
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"]
}
我的 localstack 是使用命令 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"
当我 运行 terraform apply
时,它只是打印(永远):
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...
可能出了什么问题?我是否缺少某些配置?
可能是因为:
vpc_security_group_ids = ["aws_security_group.mysg.id"]
应该是:
vpc_security_group_ids = [aws_security_group.mysg.id]