如何使用 terraform 在 LocalStack 上创建 EC2 实例?

Posted

技术标签:

【中文标题】如何使用 terraform 在 LocalStack 上创建 EC2 实例?【英文标题】:How to create EC2 instance on LocalStack with terraform? 【发布时间】:2020-07-19 21:11:03 【问题描述】:

我正在尝试使用 Terraform 在 LocalStack 上运行 EC2 实例。

尝试创建实例 50 分钟后 我收到了 terraform apply 的回复:

错误:获取 EC2 实例时出错 (i-cf4da152ddf3500e1) 信用 规格:SerializationError: failed to unmarshal error message 状态码:500,请求 id:引起:UnmarshalError:失败 由以下原因引起的解组错误消息:预期的元素类型 但有

在 main.tf 第 34 行,在资源“aws_instance”“示例”中:34: 资源“aws_instance”“示例”

对于 LocalStack 和 Terraform v0.12.18,我使用以下配置:

provider "aws" 
  access_key                  = "mock_access_key"
  region                      = "us-east-1"
  s3_force_path_style         = true
  secret_key                  = "mock_secret_key"
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true

  endpoints 
    apigateway     = "http://localhost:4567"
    cloudformation = "http://localhost:4581"
    cloudwatch     = "http://localhost:4582"
    dynamodb       = "http://localhost:4569"
    es             = "http://localhost:4578"
    firehose       = "http://localhost:4573"
    iam            = "http://localhost:4593"
    kinesis        = "http://localhost:4568"
    lambda         = "http://localhost:4574"
    route53        = "http://localhost:4580"
    redshift       = "http://localhost:4577"
    s3             = "http://localhost:4572"
    secretsmanager = "http://localhost:4584"
    ses            = "http://localhost:4579"
    sns            = "http://localhost:4575"
    sqs            = "http://localhost:4576"
    ssm            = "http://localhost:4583"
    stepfunctions  = "http://localhost:4585"
    sts            = "http://localhost:4592"
    ec2            = "http://localhost:4597"
  


resource "aws_instance" "example" 
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

当我直接从最新的 github (https://github.com/localstack/localstack) 使用 docker-compose up 运行 LocalStack 从日志中我看到 EC2 相关端点已设置。

感谢任何可以帮助我在 LocalStack 上运行 EC2 的建议。

【问题讨论】:

LocalStack 没有实际上在您的本地机器上部署任何 AWS 服务;只是模拟 API 事务。所以,这很可能是一个错误。请在 LocalStack 上提交问题以进行进一步调查。 是否应该模拟它,以便即使没有运行真正可访问的实例,我也能够查看和测试我的 terraform 配置? 虽然 LocalStack 提供了一个模拟库,但这并不意味着每个 API 调用都能成功模拟请求。在使用 Terraform 或 LocalStack 时,您并没有做错什么;但是,这是 LocalStack 内部的一个 bug,我建议使用真正的服务。 【参考方案1】:

使用以下本地堆栈的 docker 图像正常工作。

docker run -it -p 4500-4600:4500-4600 -p 8080:8080 --expose 4572 localstack/localstack:0.11.1

resource "aws_instance" "web" 
  ami           = "ami-0d57c0143330e1fa7"
  instance_type = "t2.micro"

  tags = 
    Name = "HelloWorld"
  


provider "aws" 
  region                      = "us-east-1"
  s3_force_path_style         = true
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true

  endpoints 
    apigateway     = "http://localhost:4567"
    cloudformation = "http://localhost:4581"
    cloudwatch     = "http://localhost:4582"
    dynamodb       = "http://localhost:4569"
    es             = "http://localhost:4578"
    firehose       = "http://localhost:4573"
    iam            = "http://localhost:4593"
    kinesis        = "http://localhost:4568"
    lambda         = "http://localhost:4574"
    route53        = "http://localhost:4580"
    redshift       = "http://LOCALHOST:4577"
    s3             = "http://localhost:4572"
    secretsmanager = "http://localhost:4584"
    ses            = "http://localhost:4579"
    sns            = "http://localhost:4575"
    sqs            = "http://localhost:4576"
    ssm            = "http://localhost:4583"
    stepfunctions  = "http://localhost:4585"
    sts            = "http://localhost:4592"
    ec2            = "http://localhost:4597"

  

地形应用

aws_instance.web: Destroying... [id=i-099392def6b574255]
aws_instance.web: Still destroying... [id=i-099392def6b574255, 10s elapsed]
aws_instance.web: Destruction complete after 10s
aws_instance.web: Creating...
aws_instance.web: Still creating... [10s elapsed]
aws_instance.web: Creation complete after 12s [id=i-9c942d138970d44a4]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

注意:它是一个虚拟实例,因此不适用于 ssh 和所有。但是,适合在 ec2 上测试 terraform apply/destroy 用例。

【讨论】:

以上是关于如何使用 terraform 在 LocalStack 上创建 EC2 实例?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用服务托管标识在 Azure 中使用 Terraform 预配资源

如何使用 Terraform 在 OVH 上创建私有子网?

如何使用 Terraform 为 Azure 资源创建警报

Terraform - 如何将列表转换为地图(如何使用 terraform 获取 AMI 标签)

如何使用 Terraform 配置 EKS ALB

如何使用 Terraform 在管理组范围上定义和分配 Azure 策略?