简体   繁体   English

如何仅在资源存在时使用 Fn::GetAttr?

[英]How can I use Fn::GetAttr only when the resource exist?

I'm making one main stack and two nested stack.我正在制作一个主堆栈和两个嵌套堆栈。 The first nested stack is a stack which creates Lambda. And the second nested stack creates Apigateway.第一个嵌套堆栈是创建 Lambda 的堆栈。第二个嵌套堆栈创建 Apigateway。

When making the Apigateway I send some Lambda outputs to the Apigateway template.在制作 Apigateway 时,我将一些 Lambda 输出发送到 Apigateway 模板。 But some Lambda Outputs don't get created in some conditions.但是在某些情况下不会创建某些 Lambda 输出。

So when I use Fn::GetAttr to send some outputs to the Apigateway template.因此,当我使用Fn::GetAttr将一些输出发送到 Apigateway 模板时。 I get an error because the output doesn't exist.我收到错误消息,因为 output 不存在。

[Lambda Template (Nested Stack)] [Lambda 模板(嵌套堆栈)]

Because of the condition conditionNeedMock the MockServerArn output wasn't created.由于条件conditionNeedMock未创建 MockServerArn output。

...
Outputs:
  MockServerArn:
    Condition: conditionNeedMock
    Description: lambdaMock function Arn
    Value: !GetAttr lambdaMock.Arn
...

[Main Template] [主模板]

LambdaStack.Ouputs.MockserverArn wasn't created so I get an error when using !GetAttr .未创建LambdaStack.Ouputs.MockserverArn ,因此在使用!GetAttr时出现错误。

...
Resources:
  LambdaStack:
    Type: AWS::CloudFormation::Stack
    ...

  ApiGatewayStack:
    Type: AWS::CloudFormation::Stack
    DependsOn: LambdaStack
    Properties:
      TemplateURL: apigateway.yml
      TimeoutInMinutes: 20
      Parameters:
        paramMockServerArn: !GetAtt LambdaStack.Outputs.MockServerArn
...

The only way that could possibly work (without introducing custom resources) is if conditionNeedMock is also in the root stack:唯一可行的方法(不引入自定义资源)是如果conditionNeedMock也在根堆栈中:

paramMockServerArn: !If [conditionNeedMock, !GetAtt LambdaStack.Outputs.MockServerArn, !Ref "AWS::NoValue"]

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

相关问题 如何在定义另一个资源时引用一个 Kube.netes 资源的值 - How can I reference value from one Kubernetes resource when defining another resource 手动更改资源时如何修复 cloudformation 更新 - How can I fix cloudformation update when resource was changed manually 如何使用基于 azure 资源列表 api 中资源的 createdTime 的筛选结果 - How can I use filter results based on createdTime of a resource from the azure resource list api 如何检查资源是否由 CloudFormation 创建? - How can I check if a resource was created by CloudFormation? 仅当复合主键不存在时,如何批量插入行? - How can I bulk insert rows only if a compound primary key don't already exist? 如何克隆 CDK 资源? - How can I clone a CDK resource? Terraform:如何检查另一个资源是否可用 - Terraform: how can I check if another resource is available 如何在 AWS CDK 中引用其他区域的资源? - How can I reference a resource from other region in AWS CDK? 我可以将“资源所有者密码授予”流程与 Azure AD B2C 一起使用吗 - Can I use "Resource Owner Password Grant" flow with Azure AD B2C 当类型仅为服务时,如何获得新的列顺序 - how can I get a new column order sequence when type is only service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM