Aws cdk 如何在AWS CodeBuild中设置AWS CDK应用程序执行?

Aws cdk 如何在AWS CodeBuild中设置AWS CDK应用程序执行?,aws-cdk,Aws Cdk,我希望使用AWS CodeBuild从Git存储库运行AWS CDK合成-即,如果我在repo中更新CDK应用程序代码,我希望CloudFormation堆栈自动更新。设置生成角色权限的最佳做法是什么?对于GitHub存储库,您的CodeBuild角色不需要额外的权限,但应该具有访问GitHub的oauthToken的权限 对于CodeCommit存储库,创建或导入一个CodeCommit.repository对象,并将CodeCommitSource对象用于source参数,生成角色权限将自动

我希望使用AWS CodeBuild从Git存储库运行AWS CDK合成-即,如果我在repo中更新CDK应用程序代码,我希望CloudFormation堆栈自动更新。设置生成角色权限的最佳做法是什么?

对于GitHub存储库,您的CodeBuild角色不需要额外的权限,但应该具有访问GitHub的oauthToken的权限

对于CodeCommit存储库,创建或导入一个
CodeCommit.repository
对象,并将
CodeCommitSource
对象用于
source
参数,生成角色权限将自动设置(特别是,将添加的权限将来自指定的存储库)


您可能还对CDK的软件包感兴趣。它不仅创建了一个CodeBuild项目,它还使用CodePipeline来获取、构建和部署CDK应用程序,因此它可能比您想要的要多。

AWS在一个月前发布了一个名为CDK套件的新类,其中包括一些实用程序,以简化设置自修改管道的工作。此外,还包括将管道连接到CodeCommit、GitHub、BitBucket等的结构

下面是一个完整的(链接博客文章中的详细内容),使用github作为源,通过代码管道部署lambda:

使用堆栈创建一个舞台

import { CfnOutput, Construct, Stage, StageProps } from '@aws-cdk/core';
import { CdkpipelinesDemoStack } from './cdkpipelines-demo-stack';

/**
 * Deployable unit of web service app
 */
export class CdkpipelinesDemoStage extends Stage {
  public readonly urlOutput: CfnOutput;
  
  constructor(scope: Construct, id: string, props?: StageProps) {
    super(scope, id, props);

    const service = new CdkpipelinesDemoStack(this, 'WebService');
    
    // Expose CdkpipelinesDemoStack's output one level higher
    this.urlOutput = service.urlOutput;
  }
}
使用管道创建堆栈

import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as codepipeline_actions from '@aws-cdk/aws-codepipeline-actions';
import { Construct, SecretValue, Stack, StackProps } from '@aws-cdk/core';
import { CdkPipeline, SimpleSynthAction } from "@aws-cdk/pipelines";

/**
 * The stack that defines the application pipeline
 */
export class CdkpipelinesDemoPipelineStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const sourceArtifact = new codepipeline.Artifact();
    const cloudAssemblyArtifact = new codepipeline.Artifact();
 
    const pipeline = new CdkPipeline(this, 'Pipeline', {
      // The pipeline name
      pipelineName: 'MyServicePipeline',
      cloudAssemblyArtifact,

      // Where the source can be found
      sourceAction: new codepipeline_actions.GitHubSourceAction({
        actionName: 'GitHub',
        output: sourceArtifact,
        oauthToken: SecretValue.secretsManager('github-token'),
        owner: 'OWNER',
        repo: 'REPO',
      }),

       // How it will be built and synthesized
       synthAction: SimpleSynthAction.standardNpmSynth({
         sourceArtifact,
         cloudAssemblyArtifact,
         
         // We need a build step to compile the TypeScript Lambda
         buildCommand: 'npm run build'
       }),
    });

    // This is where we add the application stages
    // ...
  }
}

谢谢,应用程序交付包看起来正是我需要的。