简体   繁体   English

将 StackName 添加到 Cloudformation 资源

[英]Prepend StackName to Cloudformation resources

I want to have multiple stacks based on a single CloudFormation template, but I'm getting naming conflicts.我想要基于单个 CloudFormation 模板的多个堆栈,但我遇到了命名冲突。 The simplest way to resolve this would seem to be prepending (or appending) the StackName to each of the repeated resources, eg my lambda functions or roles.解决此问题的最简单方法似乎是将StackName添加(或附加)到每个重复的资源,例如我的 lambda 函数或角色。

AWS talks about AWS::StackName in the 'Template Reference' section of the documentation , but there's no clear demonstration of how to do this. AWS 在文档的“模板参考”部分讨论了AWS::StackName ,但没有明确说明如何执行此操作。

How can I prepend the StackName to a CloudFormation resource?如何将StackName到 CloudFormation 资源?

MyLambdaFunction
  Type: "AWS:Serverless::Function"
  Properties:
    FunctionName: AWS::StackName + "-myLambdaFunction"

I prefer using Fn::Sub , because I believe that it's easier to read than the alternatives:我更喜欢使用Fn::Sub ,因为我相信它比其他选择更容易阅读:

"RoleName": { "Fn::Sub" : "${AWS::StackName}-InstanceRole" },

You need to Ref the pseudoparameter and use the Fn::Join method to construct the name您需要Ref伪参数并使用Fn::Join方法构造名称

MyLambdaFunction
  Type: "AWS:Serverless::Function"
  Properties:
    FunctionName: !Join [ "", [ {"Ref": "AWS::StackName"}, "-myLambdaFunction" ]]

The YAML version: YAML 版本:

RoleName: !Sub ${AWS::StackName}-InstanceRole

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

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