codepipeline:在堆栈中创建VPC并将其在Post CodeBuildStep中使用

发布于 2025-01-23 18:28:49 字数 1168 浏览 2 评论 0原文

我正在使用AWS CDK创建管道。我首先创建由VPC和RDS组成的堆栈,我导出VPC对象并将其在迁移数据库的邮政CodeBuildStep中使用:

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

    const repo = new codecommit.Repository(this, 'CloudFrontCDKRepo', {
      repositoryName: 'backend',
    })
    const pipeline = new CodePipeline(this, 'Pipeline', {
      crossAccountKeys: false,
      pipelineName: 'MyPipeline2',
      synth: new ShellStep('Synth', {
        input: CodePipelineSource.codeCommit(repo, 'cdk'),
        commands: ['cd cdk', 'npm ci', 'npm run build', 'npx cdk synth'],
        primaryOutputDirectory: 'cdk/cdk.out',
      }),
    })

    const deploy = new DevStage(this, 'Deploy-dev')
    const deployStage = pipeline.addStage(deploy)
    deployStage.addPost(
      new CodeBuildStep('SLS Deploy', {
        commands: ['./build.sh'],
        vpc: deploy.vpc
      }),
    )
  }
}

但是我遇到了以下错误:

堆栈“ mypipeLineStack”无法消耗交叉点参考堆栈“ mypipelinestack/deploy-devev/s3stack”。仅支持部署到同一环境的堆栈或嵌套堆栈及其父堆栈

在此处如何正确导入VPC的堆栈?如果不可能,我如何运行CodeBuildStep作为单独的堆栈,仅在主堆栈完成后才执行?

I am creating a pipeline using AWS CDK. I first create my stack which consists in a vpc and an rds, I export the vpc object and use it in a post CodeBuildStep which migrates the database:

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

    const repo = new codecommit.Repository(this, 'CloudFrontCDKRepo', {
      repositoryName: 'backend',
    })
    const pipeline = new CodePipeline(this, 'Pipeline', {
      crossAccountKeys: false,
      pipelineName: 'MyPipeline2',
      synth: new ShellStep('Synth', {
        input: CodePipelineSource.codeCommit(repo, 'cdk'),
        commands: ['cd cdk', 'npm ci', 'npm run build', 'npx cdk synth'],
        primaryOutputDirectory: 'cdk/cdk.out',
      }),
    })

    const deploy = new DevStage(this, 'Deploy-dev')
    const deployStage = pipeline.addStage(deploy)
    deployStage.addPost(
      new CodeBuildStep('SLS Deploy', {
        commands: ['./build.sh'],
        vpc: deploy.vpc
      }),
    )
  }
}

However I am getting the following error:

Stack "MyPipelineStack" cannot consume a cross reference from stack "MyPipelineStack/Deploy-dev/S3Stack". Cross stack references are only supported for stacks deployed to the same environment or between nested stacks and their parent stack

How can I correctly import the vpc here? And if this is not possible, how do I run a codebuildstep as a separate stack which executes only after the main stack has finished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空‖城人不在 2025-01-30 18:28:49

正如@gshpychka在评论中所说的那样,您要求CodePipeline执行不可能的操作顺序:

  1. sls部署项目是作为管道堆栈(需要VPC)的一部分而创建
  2. 的 stack部署(创建VPC)
  3. sls部署 project

围绕此订购问题运行的一种方法是将sls exploy的逻辑添加到exploy- DEV堆叠本身。有几种方法可以做到这一点:

  1. 最简单的方法是将build.sh转换为 triggerFunction 可以访问VPC的lambda。 a 构造将lambda调用为deploy-debloy-dev stack的部署生命周期的一部分。正如文档所说,触发器是为诸如“数据启动:创建后资源添加到资源”之类的任务设计的”
  2. 如果您确实需要组合中的CodeBuild项目,则第二个选项是将sls部署将其移动到exploy-deploy-dev中,并使用 awscustomresource 。它是用于运行任意SDK调用的触发的低级表弟(例如codebuild:startbuild)作为堆栈部署周期的一部分。

NB这两种方法都具有对您的迁移脚本运行何时进行的更细粒度控制的额外优势(例如,每次更新之后仅在第一次部署?)吗?)

As @gshpychka says in the comments, you are asking CodePipeline to perform an impossible order of operations:

  1. The SLS Deploy Project is created as part of the pipeline stack (needs VPC)
  2. The Deploy-dev stack deploys (creates VPC)
  3. The SLS Deploy Project runs

One way around this ordering problem is to add SLS Deploy's logic to the Deploy-dev Stack itself. There are a couple ways to do this:

  1. The simplest approach is to translate build.sh into a TriggerFunction Lambda that has access to the VPC. A Trigger construct calls the Lambda as part of the Deploy-dev Stack's deployment lifecycle. As the docs say, Triggers are designed for tasks like "Data priming: add initial data to resources after they are created".
  2. If you really need a CodeBuild project in the mix, a second option is to move SLS Deploy into Deploy-dev and have it executed with a AwsCustomResource. It is a lower-level cousin of Trigger for runing arbitrary SDK calls (like codebuild:StartBuild) as part of the stack deployment cycle.

N.B. Both these methods have the added advantage of finer-grained control over when your migration script runs (e.g. on first deploy only? after each update?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文