如何在TypeScript中使用aws-cdk在每次部署时不重建DockerImageAsset?

我的应用程序是一个Python API,我打包为Docker映像,并与ECS Fargate(Spot Instances)一起使用。下面的代码有效。

我的问题是,每次我部署此映像时,它都会重建整个映像–这非常耗时(下载所有依赖项,制作映像,上传等)。我希望它重复使用aws-cdk本身上传到ECR的相同图像。

当我不触摸应用程序的代码并仅对堆栈进行更改时,是否有一种方法(环境变量或其他)可以跳过此操作?

#!/usr/bin/env node
import * as cdk from "@aws-cdk/core"
import * as ecs from "@aws-cdk/aws-ecs"
import * as ec2 from "@aws-cdk/aws-ec2"
import * as ecrassets from "@aws-cdk/aws-ecr-assets"

// See https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ecs-readme.html
export class Stack extends cdk.Stack {
    constructor(scope: cdk.Construct,id: string,props?: cdk.StackProps) {
        super(scope,id,props)

        /**
         * Repository & Image
         */

        const apiDockerImage = new ecrassets.DockerImageAsset(
            this,`my-api-image`,{
                directory: `.`,exclude: [`cdk.out`,`cdk`,`.git`]
            }
        )

        /**
         * Cluster
         */

        const myCluster = new ecs.Cluster(this,"Cluster",{})

        // Add Spot Capacity to the Cluster
        myCluster.addCapacity(`spot-auto-scaling-group-capacity`,{
            maxCapacity: 2,minCapacity: 1,instanceType: new ec2.InstanceType(`r5a.large`),spotPrice: `0.0400`,spotInstanceDraining: true
        })

        // A task Definition describes what a single copy of a task should look like
        const myApiFargatetaskDefinition = new ecs.FargatetaskDefinition(
            this,`api-fargate-task-definition`,{
                cpu: 2048,memoryLimitMiB: 8192,}
        )

        // Add image to task def
        myApiFargatetaskDefinition.addContainer(`api-container`,{
            image: ecs.ContainerImage.fromEcrRepository(
                apiDockerImage.repository,`latest`
            ),})

        // And the service attaching the task def to the cluster
        const myApiService = new ecs.FargateService(
            this,`my-api-fargate-service`,{
                cluster: myCluster,taskDefinition: myApiFargatetaskDefinition,desiredCount: 1,assignPublicIp: true,}
        )
    }
}

chener888 回答:如何在TypeScript中使用aws-cdk在每次部署时不重建DockerImageAsset?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2722523.html

大家都在问