是否有任何命令可以覆盖现有的 `S3` 和 `ECR`,例如 CDK 上的 `--force`

Is there any command to overwrite existing `S3` and `ECR` such as `--force` on CDK

我正在使用 AWS CDK 进行开发,但经常遇到此错误。我明白这很重要。但是在开发时,我想做一些试验和错误 deploy/destroy.

这个错误很麻烦

是否有任何命令可以覆盖现有的 S3ECR,例如 --force

11:40:07 | CREATE_FAILED        | AWS::ECR::Repository          | TestRepoId440F76C3
my-repo-name already exists in stack arn:aws:cloudformation:ap-northeast-1:678100228133:stack/CdkVrStack/9e712790-7ebc-11ec-9b4b-0ad504232487

11:40:07 | CREATE_FAILED        | AWS::S3::Bucket               | testBucketDF4D7D1A
vr-resource-bucket already exists in stack arn:aws:cloudformation:ap-northeast-1:678100228133:stack/CdkVrStack/9e712790-7ebc-11ec-9b4b-0ad504232487

我的栈代码如下所示

export class CdkVrBaseStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

 
    const bucket_ = new s3.Bucket(this, 'testBucket', {
      bucketName: 'vr-resource-bucket',
      removalPolicy: RemovalPolicy.DESTROY,
      autoDeleteObjects: true,
      cors: [{
          allowedMethods: [
            s3.HttpMethods.GET,
            s3.HttpMethods.POST,
            s3.HttpMethods.PUT,
            s3.HttpMethods.DELETE,
            s3.HttpMethods.HEAD,
          ],
          allowedHeaders: ["*"],
          allowedOrigins: ["*"],
          exposedHeaders: ["ETag"],
          maxAge: 3000
        }]
    });


    const lambda_ = new lambda.Function(this, 'TestLambda', {
      functionName: 'testLambda',
      runtime: lambda.Runtime.PYTHON_3_9,
      code: lambda.Code.fromAsset('foo_lambda'),
      handler: 'index.handler',
      timeout: cdk.Duration.seconds(300),
      //role: executionLambdaRole,
      //environment: {
      //  TZ: "Asia/Tokyo",
      //  SLACK_CHANNEL: slackChannel.stringValue,
      //}
    });
    lambda_.addEventSource(new S3EventSource(bucket_, {
      events: [ s3.EventType.OBJECT_CREATED, s3.EventType.OBJECT_REMOVED ],
      //filters: [ { prefix: 'subdir/' } ], // optional
    }));
    
    const repo_ = new ecr.Repository(this, 'TestRepoId', {
      repositoryName: "my-repo-name",
      removalPolicy: RemovalPolicy.DESTROY
    });

    const image_ = new DockerImageAsset(this,'mydockerimageassets',{
      directory: path.join(__dirname, '../docker-lambda'),
      
    });
    new ecrdeploy.ECRDeployment(this, 'DeployDockerImage', {
      src: new ecrdeploy.DockerImageName(image_.imageUri),
      dest: new ecrdeploy.DockerImageName(`${repo_.repositoryUri}:latest`),
    });
    

不,那不是一个选择。

这就是您应该避免为资源提供物理名称的确切原因。让 CDK auto-generate 他们,你不会有这些问题。

参考以下问题的答案: