使用子模块容器定义的 terraform ecs 模块

问题描述 投票:0回答:1

我正在使用以下ECS-Service terraform子模块来创建ecs服务。我还使用同一存储库中的以下ECS-TaskDefinition 子模块来生成服务的任务定义。

以下是任务定义

module "ecs_container_tr_def" {
  source  = "terraform-aws-modules/ecs/aws//modules/container-definition"
  version = "5.2.2"

  name      = "test"
  cpu       = 256
  memory    = 512
  essential = true
  image     = "1234567890.dkr.ecr.eu-west-2.amazonaws.com/test:latest"
  port_mappings = [
    {
      containerPort = 5000
      hostPort      = 5000
      protocol      = "tcp"
    }
  ]
}

我创建了服务并尝试像这样导入任务定义:

module "ecs_tr_service" {
  source  = "terraform-aws-modules/ecs/aws//modules/service"
  version = "5.2.2"

  name        = "test-serv"
  cluster_arn = var.ecs_cluster_arn

  cpu    = 512
  memory = 1024

  launch_type  = "FARGATE"
  network_mode = "awsvpc"

  desired_count            = 1
  enable_autoscaling       = true
  autoscaling_min_capacity = 1
  autoscaling_max_capacity = 1

  container_definitions = {
    test = module.ecs_container_tr_def.container_definition
  }

我遇到的问题是,由于某种原因,

port_mapping
中的
task-definition
部分被忽略,因此
apply
总是失败

从地形计划中我可以看到

portMappings = []
是空的

# module.ecs_tr_service.aws_ecs_task_definition.this[0] will be created
  + resource "aws_ecs_task_definition" "this" {
      + arn                      = (known after apply)
      + arn_without_revision     = (known after apply)
      + container_definitions    = jsonencode(
            [
              + {
                  + cpu                    = 256
                  + environment            = []
                  + essential              = true
                  + image                  = "12345678.dkr.ecr.eu-west-2.amazonaws.com/terrareg:latest"
                  + interactive            = false
                  + logConfiguration       = {
                      + logDriver = "awslogs"
                      + options   = {
                          + awslogs-group         = "/aws/ecs/terrareg-serv/terrareg"
                          + awslogs-region        = "eu-west-2"
                          + awslogs-stream-prefix = "ecs"
                        }
                    }
                  + memory                 = 512
                  + mountPoints            = []
                  + name                   = "terrareg"
                  + portMappings           = []
                  + privileged             = false
                  + pseudoTerminal         = false
                  + readonlyRootFilesystem = true

当我这样做时

terraform apply
我收到以下错误:

module.ecs_tr_service.aws_ecs_service.this[0]: Creating...
╷
│ Error: creating ECS Service (terrareg-serv): InvalidParameterException: The container terrareg did not have a container port 5000 defined.
│ 
│   with module.ecs_tr_service.aws_ecs_service.this[0],
│   on .terraform/modules/ecs_tr_service/modules/service/main.tf line 30, in resource "aws_ecs_service" "this":
│   30: resource "aws_ecs_service" "this" {

我认为问题在于将

task-definition
导入
service
test = module.ecs_container_tr_def.container_definition
的行。我尝试过不同的方法,但都失败了。

如果有人可以向我展示如何正确使用

task-definition
子模块并使用其
output
将其正确地
import
放入
service
那就太好了

terraform amazon-ecs terraform-modules
1个回答
0
投票

我认为“ecs_tr_service”中的“Container_definitions”缺少部分。

代码(ecs_tr_service部分):

module "ecs_tr_service" {
  source  = "terraform-aws-modules/ecs/aws//modules/service"
  version = "5.2.2"

  name        = "test-serv"
  cluster_arn = var.ecs_cluster_arn

  cpu    = 512
  memory = 1024

  launch_type  = "FARGATE"
  network_mode = "awsvpc"

  desired_count            = 1
  enable_autoscaling       = true
  autoscaling_min_capacity = 1
  autoscaling_max_capacity = 1

  container_definitions = jsonencode([{
    name  = "test"  # This should match the name in your container definition
    image = "1234567890.dkr.ecr.eu-west-2.amazonaws.com/test:latest"
    portMappings = [
      {
        containerPort = 5000
        hostPort      = 5000
      }
    ]
  }])
}

我直接使用资源,而不是模块。但我在这里找到了该模块的示例:

© www.soinside.com 2019 - 2024. All rights reserved.