aws-cdk S3到CodeCommit部署

时间:2020-08-03 13:44:05

标签: typescript amazon-web-services aws-cdk

我正在实现一个CDK堆栈,该堆栈创建一个Bucket,将文件放入其中,最后创建CodeCommit存储库,在其中存储文件。一切顺利,直到调用了新的codecommit.CfnRepository,然后出现以下错误:

CREATE_FAILED        | AWS::CodeCommit::Repository | CfnRepository
Not Found (Service: Amazon S3; Status Code: 404; Error Code: 404 Not Found;
Request ID: 2608A90CD11E9729; S3 Extended Request ID: 1iVrjbDpcwqSrsNc7s/aF
9UpNMg0DGe9ABTAJMuoRkA3f9qSYMqVN0sWeLRdT6ETck/DRx6dDCM=)

当我将s3(创建/部署和存储库创建)分为两个堆栈时,它可以工作,但是如果在一个构造函数中,则CDK部署会失败。

这是我的目标代码:

export class CdkBeeStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, name: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const myBucket = new s3.Bucket(this, 'Bucket', {
      bucketName: name,
      removalPolicy: cdk.RemovalPolicy.DESTROY,
    });

    new s3deploy.BucketDeployment(this, 'DeployFiles', {
      sources: [s3deploy.Source.asset('./lib/Files.zip')],
      destinationBucket: myBucket
    });

    new codecommit.CfnRepository(this, 'CfnRepository' , {
      repositoryName: 'MyFetucinyName',
      code: {
        s3: {
          bucket: name,
          key: 'Files/SourceCode.zip'
        }
      }
    });
  }
}

任何使它起作用的想法都会受到赞赏

1 个答案:

答案 0 :(得分:2)

我认为您想等到BucketDeployment完成,因此请尝试添加如下所示的依赖项:

    const s3deploy = new s3deploy.BucketDeployment(this, 'DeployFiles', {...});

    const ccr = new codecommit.CfnRepository(this, 'CfnRepository' , {...});

    ccr.node.addDependency(s3deploy);

但是请注意,根据aws cdk doco s3deploy.BucketDeployment将zip归档文件提取到存储桶中。

因此,您的“键:'Files / SourceCode.zip'”需要引用未压缩的内容。 另一个选择是引用您的zip文件所在的目录,该目录将原样上传,例如:

    new s3deploy.BucketDeployment(this, 'DeployFiles', {
      sources:  [s3deploy.Source.asset('./lib')],
      destinationBucket: myBucket
    });