cdk CI/CD Pipeline - 销毁重复堆栈
我正在努力在打字稿中使用 cdk 设置 CI/CD 管道。我有一个非常模块化的堆栈结构,因此我有一个包含 3 个堆栈的 Stage:LambdasStack、EndpointsStack 和 TablesStack。顾名思义,我将所有 Lambda 都放在 LambdasStack 中,等等。
对于管道,我想要以下流程:
- 构建
- 为 PreProd
- 集成测试
- 部署堆栈销毁 PreProd 堆栈
- 在生产之前手动批准
- 部署生产堆栈
由于表的唯一名称,必须在部署生产堆栈之前销毁 PreProd 堆栈在 TablesStack 中。这就是我正在努力解决的问题。我销毁它们的代码是:
const deletePreProdStacks= new ShellStep('Delete deployed Stacks', {
commands: [
'npm install',
'cdk destroy -f --all'
]
});
使用 'cdk destroy -f --all'
找不到阶段的堆栈,因此无法删除它们。
我该如何解决这个问题?为表提供自动生成的名称不是正确的解决方案吗?或者是否可以选择将 PreProdStacks 覆盖到 ProdStacks?
我只能访问一个 AWS 账户。因为我读到在不同的帐户上有测试/预生产阶段和生产阶段。
也许有人有类似的最佳实践参考给我?
提前致谢:)
编辑1:标签更新 Edit2:添加了有关 PreProd 和 PreProd 部署的情况同一帐户中的产品
I'm working on setting up a CI/CD Pipeline with cdk in typescript. I have a very modular stack structure so I'm having a Stage with 3 stacks: LambdasStack, EndpointsStack and TablesStack. As the name suggest I have all my Lambdas in LambdasStack and so on.
For the pipeline I want the following flow:
- Build
- Deploy Stacks for PreProd
- Integration Test
- Destroy Stacks of PreProd
- Manual approval before Prod
- Deploy Stacks for Prod
The PreProd stacks have to be destroyed before the deployment of the Prod stacks because of the unique names of the tables within the TablesStack. And that's what I'm struggling with. My code to destroy them is:
const deletePreProdStacks= new ShellStep('Delete deployed Stacks', {
commands: [
'npm install',
'cdk destroy -f --all'
]
});
With 'cdk destroy -f --all'
the stacks of the stage are not found so they can't be deleted.
How can I solve this problem? Giving the tables autogenerated names can't be the right solution? Or is there an option to overwrite the PreProdStacks to ProdStacks?
I only have access to only one AWS Account. Because I read that having Testing/PreProd stage and Prod stage on different accounts.
Maybe someone has a similar best practice reference for me?
Thanks in advance :)
Edit1: tag update
Edit2: added situation about deployment of PreProd & Prod in same account
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了一个好的解决方案,通过在资源的属性中添加阶段名称来为无法重复的资源提供不同的名称,如下所示:
在堆栈中:
在阶段中:
我创建了 2 个表,其中一个名为
PreProd-BookingsTable< /code> 和另一个
Prod-BookingsTable
。所以我不需要销毁已经部署的堆栈
I found an ok solution for giving the resources that can't be duplicated different names by adding the stagename in their properties like this:
In the Stack:
In the Stage:
There by I create 2 tables one named
PreProd-BookingsTable
and the otherProd-BookingsTable
.So I don't need to destroy the already deployed stacks
您的 PreProd 阶段的堆栈不会被销毁,因为 cdk destroy 没有获得正确的堆栈名称。
cdk destroy
命令执行以下操作:--app
标志或的
。app
键中的命令合成应用程序cdk.jsoncdk.out/manifest.json
中)在步骤 #1 中创建。DeleteStack
API。您想要销毁像
PreProdLambdasStack
这样的堆栈,但app.ts
正在生成其他堆栈名称。所以什么都不会被删除。解决此问题的一种方法是将阶段名称作为环境变量传递到应用程序文件。修改app.ts
将阶段前缀连接到基本堆栈名称,以生成具有阶段名称前缀(如PreProd
)的堆栈。由于 stage.ts(堆栈创建)和 app.ts(堆栈删除)需要相同的堆栈代码,因此请使用在两者中调用的可重用堆栈创建包装器来保持代码干燥上下文:
注释
AWS 推荐setup 是每个环境的单独帐户。但是,如果您只能使用
PreProd
和Prod
的单个帐户,那么下一个最干净的选择是每个环境都有单独的堆栈。PreProd
部署并经过测试,然后被销毁。为了加快管道执行速度,Prod
部署和PreProd
销毁可以并行运行。另一个最佳实践是避免分配构造名称,或者AWS所说的“使用生成的资源名称,而不是物理名称”。如果需要漂亮的资源名称,请考虑仅在
Prod
环境中分配它们。Your PreProd stage's stacks are not being destroyed because
cdk destroy
isn't getting the stack names right. Thecdk destroy
command does the following:--app
flag or in theapp
key ofcdk.json
.cdk.out/manifest.json
) created in Step #1.DeleteStack
API for each stack name.You want to destroy stacks like
PreProdLambdasStack
, but theapp.ts
is producing other stack names. So nothing gets deleted. One way to fix this is to pass the stage name to your app file as an environment variable. Modifyapp.ts
to concatenate the stage prefix to the base stack name to produce stacks with a stage name prefix likePreProd
.Because
stage.ts
(stack creation) andapp.ts
(stack deletion) need the same stack code, keep your code DRY with a reusable stack-creation wrapper called in both contexts:Notes
The AWS-recommended setup is a separate account for each environment. If, however, you are stuck with a single account for
PreProd
andProd
, the next cleanest option is to have separate stacks per environment.PreProd
deploys and is tested, then is destroyed. To speed up pipeline execution, theProd
deploy andPreProd
destroy can run in parallel.Another best practice is to avoid assigning construct names at all, or AWS puts it, "Use generated resource names, not physical names". If pretty resource names are a requirement, consider assigning them only in
Prod
environments.那么,您不需要销毁堆栈,而是通过“环境”指定资源的物理名称(不是首选)。
我所说的环境是指阶段名称,例如“pre-prod”、“prod”等。
最好的解决方案是部署您的测试和环境。预生产&不同帐户中的产品阶段。
如果您使用 CDK popelines,部署到多个帐户应该不会有太多工作,您唯一要做的就是引导您部署到的所有帐户以信任部署帐户(查看引导文档)并设置阶段中的帐户 ID。
Well you don't need to destroy your stacks but specify physical names for resources By “environment” (not preferred).
By environment I mean the stage name, e.g. “pre-prod”, “prod”, etc.
The best solution is to deploy your testing & PreProd & Prod stages in difference accounts.
And if you're using CDK popelines, deploying to multiple accounts shoudn't be too much work, the only thing you have to do is bootstrap all the accounts you deploy to to trust the deployment account (review the bootstrapping docs) and set the account IDs in the stages.