使用Terraform创建ECS时遇FARGATE启动类型不支持错误求助
ECS Fargate 任务定义不兼容错误排查
错误核心原因
你遇到的InvalidParameterException: Task definition does not support launch_type FARGATE错误,根源在于任务定义中包含了Fargate不支持的Volume配置:
- 在
aws_ecs_task_definition.api资源里,你定义了一个仅指定name = "static"的Volume块,但Fargate不允许这种无类型的空Volume配置。Fargate仅支持AWS EFS、FSx for Windows File Server或临时tmpfs类型的Volume。 - 且你的容器定义中并未挂载这个Volume,冗余的无效配置直接导致任务定义无法兼容Fargate模式。
修复方案
方案1:移除冗余Volume(推荐,若未实际使用)
直接删除任务定义中未使用的Volume块即可,修改后的代码如下:
resource "aws_ecs_task_definition" "api" { family = "${local.prefix}-api" container_definitions = data.template_file.api_container_definitions.rendered requires_compatibilities = ["FARGATE"] network_mode = "awsvpc" cpu = 256 memory = 512 execution_role_arn = aws_iam_role.task_execution_role.arn task_role_arn = aws_iam_role.task_role.arn tags = local.common_tags }
方案2:配置Fargate支持的Volume(若需使用存储)
如果确实需要挂载Volume(比如存储静态文件),需配置Fargate兼容的类型,以EFS为例:
- 提前创建EFS文件系统及访问点
- 修改任务定义中的Volume配置:
volume { name = "static" efs_volume_configuration { file_system_id = aws_efs_file_system.static.id root_directory = "/" transit_encryption = "ENABLED" authorization_config { access_point_id = aws_efs_access_point.static.id iam = "ENABLED" } } }
- 在容器定义中添加挂载配置:
"mountPoints": [ { "sourceVolume": "static", "containerPath": "/your/static/path", "readOnly": false } ]
额外检查
容器定义中的hostPort设置为80,与containerPort一致,符合Fargate要求(Fargate中hostPort必须等于containerPort或设为0),这部分无需修改。
内容的提问来源于stack exchange,提问作者Phuong Tran
相关产品推荐
相关产品推荐

