AWS CDK 多个应用程序 [英] AWS CDK multiple Apps

查看:26
本文介绍了AWS CDK 多个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在同一个项目中拥有两个 CDK 应用程序,例如:

Would it be possible to have two CDK Apps in the same project, something like this:

from aws_cdk import core

from stack1 import Stack1
from stack2 import Stack2

app1 = core.App()
Stack1(app1, "CDK1")
app1.synth()

app2 = core.App()
Stack2(app2, "CDK2")
app2.synth()

然后部署它们?同步/异步?

And deploy them? Synchronously/Asynchronously?

是否可以从一个应用中引用另一个应用中的某些资源?

Would it be possible to reference some resources from one app in the other one?

推荐答案

是的,您可以在一个 CDK 项目中拥有多个应用程序,但有一些严重的警告.

Yes you can have multiple applications in a CDK project, but there are some serious caveats.

  1. 一个 CDK 进程一次只能合成/部署一个应用程序.

  1. A CDK process can only synth/deploy one app at a time.

它们不能在同一个文件中定义.

They cannot be defined in the same file.

他们不能直接引用彼此的资源.

They cannot directly reference each other's resources.

从这个角度来看,每个应用程序在功能上彼此隔离,大致相当于两个单独的 CDK 项目只是共享相同的代码库,因此其用例是有限的.

To put this in perspective, each app is functionally isolated from each other and it is roughly equivalent to having two separate CDK projects just sharing the same codebase, so the use cases for this are limited.

他们共享资源的唯一方法是将其提取到必须首先部署的其他通用应用程序中,或者让您将该资源的 ARN 存储在某物(例如,Parameter Store)中,然后在运行.您不能假设资源会存在,因为其中一个应用程序可能尚未部署,如果您直接将资源导入您的堆栈,您就完全无法将它们分开.

The only way for them to share resources is either to extract it to an additional common app that must be deployed first, or for you to store the ARN of that resource in something (e.g., Parameter Store), and load it at run time. You cannot assume that the resource will exist as one of the apps may not have been deployed yet, and if you import the resource into your Stack directly, you've defeated the whole point of splitting them apart.

也就是说,这样就可以了:

That is to say, this is ok:

stack1.lambda:

stack1.lambda:

    from ssm_parameter_store import SSMParameterStore

    store = SSMParameterStore(prefix='/Prod')
    ssn_arn = store['stack2.sns']
    if !ssn_arn
        // Doesn't matter
        return
    try:
        sns.publish(ssn_arn, 'something')
    except:
        // Doesn't matter

但是如果 stack1 中存在来自 stack2 的资源很重要,或者您想将 stack2 资源导入到 stack1,那么您要么需要对所有公共资源进行第三次拆分:common-resources.app.py,否则就没有必要拆分它们.

But if it's critical to stack1 that a resource from stack2 exists, or you want to import a stack2 resource into stack1, then you either need to do a third split of all the common resources: common-resources.app.py, or there's no point splitting them.

我们在项目中经常这样做,其中一个应用程序创建了一个自动部署另一个应用程序的 CodePipeline.但是,我们这样做只是因为我们更喜欢管道与它正在部署的代码相邻,并且将其提取到一个全新的项目中同样有效.

We do this a lot in our projects, with one app creating a CodePipeline that automatically deploys the other app. However, we only do this because we prefer the pipeline lives next to the code it is deploying and it would be equally valid to extract it into an entirely new project.

如果你想这样做,你需要这样做:

If you want to do this, you need to do:

app1.py:

from aws_cdk import core

from stack1 import Stack1

app1 = core.App()
Stack1(app1, "CDK1")
app1.synth()

app2.py:

from aws_cdk import core

from stack2 import Stack2

app2 = core.App()
Stack2(app2, "CDK2")
app2.synth()

然后通过并行或顺序运行来部署它:

You then deploy this by running in parallel or sequentially:

  • cdk deploy --app "python app1.py"
  • cdk deploy --app "python app2.py"

这篇关于AWS CDK 多个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆