简体   繁体   English

以编程方式利用 CloudFormation 创建的资源

[英]Programmatically utilize resources created with CloudFormation

I'm creating a bunch of application resources with AWS CloudFormation, and when the resources are created, CloudFormation adds a hash at the end of the name to make it unique.我正在使用 AWS CloudFormation 创建一堆应用程序资源,并且在创建资源时,CloudFormation 在名称末尾添加一个哈希值以使其唯一。

ie If you wanted to create a Kinesis stream names MyStream , the actually name would be something like my-stack-MyStream-1F8ISNCLP0W4O .即如果您想创建一个名为MyStream的 Kinesis 流,实际名称将类似于my-stack-MyStream-1F8ISNCLP0W4O

I want to be able to programmatically access the resources without having to know the hash, without having to query AWS for my resources to match the names myself, and without manual steps.我希望能够以编程方式访问资源,而无需知道哈希值,无需查询 AWS 以获取我自己的资源以匹配名称,也无需手动步骤。 Does anybody know a convenient way to use AWS resources in your application programmatically and predictably?有人知道在您的应用程序中以编程方式和可预测的方式使用 AWS 资源的便捷方法吗?

Here are the less ideal options I can think of:以下是我能想到的不太理想的选择:

  1. Set a tag on the resource (ie name -> MyStream ) and query AWS to get the actual resource name.在资源上设置标签(即name -> MyStream )并查询 AWS 以获取实际资源名称。
  2. Query AWS for a list of resources names and look for a partial match on the expected name.查询 AWS 以获取资源名称列表并查找与预期名称部分匹配的内容。
  3. After you create your resources, manually copy the actual names into your config file (probably the sanest of these options)创建资源后,手动将实际名称复制到配置文件中(可能是这些选项中最合理的)

You can use the CloudFormation API to get a list of resources in your stack.您可以使用 CloudFormation API 获取堆栈中的资源列表。 This will give you a list of logical ids (ie the name in your CloudFormation template without the hash) and matching physical ids (with the stack name and hash).这将为您提供一个逻辑 id 列表(即您的 CloudFormation 模板中没有散列的名称)和匹配的物理 id(带有堆栈名称和散列)。 Using the AWS CLI, this will show a mapping between the two ids:使用 AWS CLI,这将显示两个 ID 之间的映射:

aws cloudformation describe-stack-resources
    --query StackResources[].[LogicalResourceId,PhysicalResourceId]
    --stack-name <my-stack>

CloudFormation APIs to do the same query are provided in all the various language SDKs provided by Amazon. Amazon 提供的所有各种语言 SDK 中都提供了用于执行相同查询的 CloudFormation API。

You can use this as an alternative to #1, by querying CloudFormation at runtime, or #3, by querying CloudFormation at buildtime and embedding the results in a config file.您可以通过在运行时查询 CloudFormation 或 #3,通过在构建时查询 CloudFormation 并将结果嵌入到配置文件中,将其用作 #1 的替代方法。 I don't see any advantage to using your own tags over simply querying the CF API.与简单地查询 CF API 相比,我认为使用自己的标签没有任何优势。 #2 will cause problems if you want two or more stacks from the same template to coexist.如果您希望来自同一模板的两个或多个堆栈共存,#2 将导致问题。

I've used both the runtime and build time approaches.我同时使用了运行时和构建时方法。 The build time approach lets you remove the dependency on or knowledge of CloudFormation, but needs stack specific information in your config file.构建时方法可让您移除对 CloudFormation 的依赖或知识,但需要在配置文件中存储特定于堆栈的信息。 I like the runtime approach to allow the same build to be deployed to multiple stacks and all it needs is the stack name to find all the related resources.我喜欢允许将相同构建部署到多个堆栈的运行时方法,它所需要的只是堆栈名称来查找所有相关资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM