您好,我正在使用 aws ecs 和 terraform 部署一些容器。我正在尝试创建一个带有 ec2 实例的 ecs 集群,并且我想在每个实例中运行两个 nginx 容器。我在公共子网中设置了一个负载均衡器,并在私有子网中创建了一个自动扩展组,并将其输入到我的集群中。正在创建 EC2 实例,注册服务并取消任务,但是当我转到 ecs 中的控制台时,没有任务正在运行,当我转到应用程序负载均衡器 dns 时,我收到 503 未找到错误。我列出了与 ecs 相关的代码和我正在尝试执行的操作的图表。 图表
resource "aws_launch_configuration" "ecs_launch_config" {
image_id = var.ami_id
security_groups = [aws_security_group.ecs_tasks.id]
user_data = var.user_data
instance_type = var.instance_type
key_name = var.ssh_key_name
}
resource "aws_autoscaling_group" "asg" {
name = "asg"
vpc_zone_identifier = aws_subnet.pt_sn[*].id
launch_configuration = aws_launch_configuration.ecs_launch_config.name
desired_capacity = 3
min_size = 1
max_size = 6
health_check_grace_period = 300
health_check_type = "EC2"
tag {
key = "Name"
value = "tf_asg"
propagate_at_launch = true
}
}
resource "aws_ecs_capacity_provider" "cp" {
name = "tf-ecs-cp"
auto_scaling_group_provider {
auto_scaling_group_arn = aws_autoscaling_group.asg.arn
}
}
resource "aws_ecs_cluster_capacity_providers" "ccp" {
cluster_name = aws_ecs_cluster.main_cluster.name
capacity_providers = [aws_ecs_capacity_provider.cp.name]
default_capacity_provider_strategy {
capacity_provider = aws_ecs_capacity_provider.cp.name
weight = 1
base = 0
}
}
resource "aws_ecs_task_definition" "task" {
family = "tf-ecs-task"
requires_compatibilities = ["EC2"]
container_definitions = var.container_definition
}
resource "aws_ecs_cluster" "main_cluster" {
name = "tf-ecs-cluster"
tags = {
Name = "tf-ecs-cluster"
}
}
resource "aws_ecs_service" "main" {
name = "tf-ecs-service"
cluster = aws_ecs_cluster.main_cluster.id
task_definition = aws_ecs_task_definition.task.arn
desired_count = var.app_count
launch_type = "EC2"
load_balancer {
target_group_arn = aws_alb_target_group.app.arn
container_name = var.container_name
container_port = var.app_port
}
tags = {
Name = "tf-ecs-service"
}
}
答案1
你好,问题是我没有为自动缩放组分配 iam 角色,我没有使用 ecs 优化映像,也没有将 ecs 集群名称写入 ec2 配置文件。