codepipeline:在堆栈中创建VPC并将其在Post CodeBuildStep中使用
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如@gshpychka在评论中所说的那样,您要求CodePipeline执行不可能的操作顺序:
sls部署
项目是作为管道堆栈(需要VPC)的一部分而创建sls部署
project围绕此订购问题运行的一种方法是将
sls exploy
的逻辑添加到exploy- DEV
堆叠本身。有几种方法可以做到这一点:build.sh
转换为 triggerFunction 可以访问VPC的lambda。 a 构造将lambda调用为deploy-debloy-dev
stack的部署生命周期的一部分。正如文档所说,触发器是为诸如“数据启动:创建后资源添加到资源”之类的任务设计的” 。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:
SLS Deploy
Project is created as part of the pipeline stack (needs VPC)Deploy-dev
stack deploys (creates VPC)SLS Deploy
Project runsOne way around this ordering problem is to add
SLS Deploy
's logic to theDeploy-dev
Stack itself. There are a couple ways to do this:build.sh
into a TriggerFunction Lambda that has access to the VPC. A Trigger construct calls the Lambda as part of theDeploy-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".SLS Deploy
intoDeploy-dev
and have it executed with a AwsCustomResource. It is a lower-level cousin ofTrigger
for runing arbitrary SDK calls (likecodebuild: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?)