AWS CDK使用EcsDeployAction更新已有Fargate Service报错如何解决
报错原因
AWS CDK 中通过fromFargateServiceArn静态方法导入外部Fargate服务时,返回的IFargateService类型实例缺失cluster属性,而EcsDeployAction入参要求的service必须实现IBaseService接口,该接口强制要求cluster属性存在,因此触发TypeScript类型校验错误。
解决方案
提供两种可落地的处理方式:
方式1:使用fromFargateServiceAttributes手动补全属性(推荐)
先导入服务所属的ECS集群,再通过属性导入的方式获取完整的服务实例,示例代码如下:
// 先导入服务所属的ECS集群 const cluster = ecs.Cluster.fromClusterArn( this, "ImportedCluster", "你的ECS集群ARN" ); // 用属性导入替代ARN导入,补全cluster属性 const fargateService = ecs.FargateService.fromFargateServiceAttributes( this, "FargateService", { serviceArn: "MyFargateServiceARN", cluster: cluster } ); // 后续EcsDeployAction代码无需改动 new codepipeline_actions.EcsDeployAction({ actionName: "ECS-Service", service: fargateService, input: ... })
方式2:类型断言跳过校验
如果你确认导入的服务关联集群信息无误,可直接通过类型断言跳过TypeScript校验:
new codepipeline_actions.EcsDeployAction({ actionName: "ECS-Service", // 两层断言跳过类型校验 service: fargateService as unknown as ecs.IBaseService, input: ... })
内容的提问来源于stack exchange,提问作者andreav
相关产品推荐
相关产品推荐

