我正在创建一个 ECS 任务,以便在由 EventBridge 事件触发时运行,但我无法为该任务分配公共 IP 地址,而这是访问 ECS 来获取该任务所必需的。我使用的 VPC 仅具有公共子网。
这是相关代码:
const cluster = new ecs.Cluster(this, "default-cluster", {
vpc,
clusterName: `xxxxx`,
enableFargateCapacityProviders: true,
});
const taskDefinition = new ecs.FargateTaskDefinition(
this,
`${process.type}-task`,
{
memoryLimitMiB: 512,
cpu: 256,
runtimePlatform: {
cpuArchitecture: ecs.CpuArchitecture.X86_64,
operatingSystemFamily: ecs.OperatingSystemFamily.LINUX,
},
}
);
taskDefinition
.addContainer(`xxxxxxxx`, {
image: ecs.ContainerImage.fromEcrRepository(repo, props.imageTag),
containerName: `xxxxxx`,
})
.addPortMappings({
containerPort: 3000,
});
const releaseTask = new eventBridgeTargets.EcsTask({
cluster,
taskDefinition,
taskCount: 1,
launchType: ecs.LaunchType.FARGATE,
subnetSelection: vpc.selectSubnets({
subnetType: SubnetType.PUBLIC,
}),
assignPublicIp: true,
});
我收到的错误是:
Error: assignPublicIp should be set to true only for PUBLIC subnets
我做错了什么?
subnetSelection
属性需要一个 SubnetSelection
对象,并且您传入 Vpc.selectSubets
函数的结果,该函数接受 SubnetSelection
并返回 SelectedSubnets
的实例。
而是直接传递
SubnetSelection
对象:
subnetSelection: {
subnetType: SubnetType.PUBLIC,
},
我建议正确配置您的 IDE/语言服务器 - 它应该为您突出显示诸如此类的类型错误。