You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Cloud Map使DNS路由至ALB或正确指向Fargate任务?

问题解答

一、让DNS正确路由至Fargate任务(当前配置的验证与调整)

你当前的Cloud Map配置已经会自动将Fargate任务的私网IP注册为myapp.xxx.local(xxx为你的Cloud Map命名空间域名)的A记录,端口绑定8080。要确保DNS能正确路由到任务,需检查以下几点:

  • 访问DNS的客户端(如VPC内其他服务)所在安全组,需允许访问Fargate任务安全组sg的8080端口
  • 确认Cloud Map命名空间为VPC类型,仅VPC内资源可解析该DNS记录
  • 任务健康检查正常,只有健康任务会被Cloud Map注册到DNS中

若需直接通过DNS访问任务的8080端口,当前配置已满足需求,只要上述条件全部达标即可。

二、让DNS直接路由至ALB(推荐方案)

完全可以让Cloud Map的DNS记录直接指向ALB,这也是更合理的架构——借助ALB实现负载均衡、健康检查、流量转发等能力。你需要修改cloudMapOptions配置,不再绑定任务的容器端口,转而将ALB的私网IP注册到DNS中,具体修改如下:

修改后的代码示例

const service = new ecs_patterns.ApplicationLoadBalancedFargateService(this, 'MyAppWebstartFargateService', {
  serviceName: "myapp-service",
  cluster: cluster,
  cpu: 512,
  memoryLimitMiB: 2048,
  cloudMapOptions: {
    name: "myapp",
    cloudMapNamespace: namespace,
    dnsRecordType: svc_dsc.DnsRecordType.A,
    dnsTtl: Duration.seconds(300),
    // 指定注册负载均衡器的IP,而非任务IP
    registrationType: svc_dsc.RegistrationType.LOAD_BALANCER,
  },
  desiredCount: 1,
  publicLoadBalancer: false,
  securityGroups: [sg],
  listenerPort: 80,
  openListener: true,
  healthCheckGracePeriod: Duration.seconds(300),
  targetProtocol: elbv2.ApplicationProtocol.HTTP,
  protocol: elbv2.ApplicationProtocol.HTTP,
  enableExecuteCommand: true,
  taskImageOptions: {
    containerName: "myapp-container",
    containerPort: 8080,
    enableLogging: true,
    image: ecs.ContainerImage.fromEcrRepository(repository, "latest"),
  },
});

关键修改说明

  • 移除cloudMapOptions中的containerPort:该参数用于指定注册任务的容器端口,现在我们要注册ALB,无需保留
  • 添加registrationType: svc_dsc.RegistrationType.LOAD_BALANCER:告知Cloud Map注册Fargate服务关联的ALB私网IP,而非单个任务的IP

修改完成后,Cloud Map的myapp.xxx.local DNS记录会指向ALB的私网IP,默认使用ALB的80监听端口。客户端访问该DNS时,流量先进入ALB,再通过你已配置的规则转发到Fargate任务的8080端口。

额外注意事项

  • 确保ALB的安全组允许客户端访问80端口
  • 确认ALB目标组的健康检查正常,保障流量仅转发到健康的任务

内容的提问来源于stack exchange,提问作者Hines Bourne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 18:39:40