【问题标题】:Get Value from a Lambda using Cloud Formation and check condition to branch使用 Cloud Formation 从 Lambda 获取价值并检查分支的条件
【发布时间】:2020-08-08 14:23:00
【问题描述】:

我附上了示例,让您清​​楚我要解决的问题。

AWSTemplateFormatVersion: '2010-09-09'
Description: Project Service Catalog get lambda data

Parameters:
  Environment:
    Type: String
    Description: Environment of the SageMaker
  
  ProjectId:
    Type: String
    Description: Project ID of the SageMaker

  SsmRoleLambdaArn:
    Type: AWS::SSM::Parameter::Value<String>
    Default: '/data-science/role-lambda/arn'
    Description: Arn to lookup Role of the Session using project id

Resource:
  IdentifyUserRole:
    Type: Custom::GetParam
    Properties:
      ServiceToken: !Ref SsmRoleLambdaArn
      pl_role: !Sub '${Environment}-sso-data-science-${ProjectId}-pl-role'
      ds_role: !Sub '${Environment}-sso-data-science-${ProjectId}-ds-role'

  KmsKey:
    Type: AWS::KMS::Key
    Properties:
      Description: !Sub 'Encryption for ${Environment}-${ProjectId}-${Prid}-${NotebookInstanceNameSuffix}'
      EnableKeyRotation: true
      Tags:
        - Key: Environment
          Value: !Ref Environment
        - Key: Owner
          Value: !Ref Owner
        - Key: ProjectId
          Value: !Ref ProjectId
        - Key: PrincipalId
          Value: !Sub
            - "${RoleId}:${Prid}"
            - RoleId:
                Fn::If: [!Equals [!GetAtt IdentifyUserRole.value, true], !GetAtt PORoleId.value, !GetAtt DSRoleId.value]

PrincipalID 标记中的 IF 条件出现错误。请使用一些示例模板帮助解决这种情况。我也不能在 Conditions 块中使用 !GetAtt,因为我们不应该使用 get 属性。

错误消息 - 堆栈验证期间

调用 ValidateTemplate 操作时发生错误 (ValidationError):模板错误:Fn::If 需要一个列表参数,其中第一个元素是条件

【问题讨论】:

  • 以上模板只是一个示例。不需要调试参数错误。因为我想解决 IF 条件。我知道这里缺少一些参数

标签: amazon-web-services amazon-cloudformation aws-cloudformation-custom-resource


【解决方案1】:

您不能像您尝试的那样在 If 中硬编码条件:

 Fn::If: [!Equals [!GetAtt IdentifyUserRole.value, true], !GetAtt PORoleId.value, !GetAtt DSRoleId.value]

第一个参数必须是条件来自Conditions 部分 (docs):

条件部分中的条件的引用。

随后,您无法基于GetAttResources 部分中的任何其他资源构建条件。

同样的文档也写道:

您只能从模板的“参数”和“映射”部分引用其他条件和值。例如,您可以从输入参数中引用一个值,但您不能在条件中引用资源的逻辑 ID

【讨论】:

  • 感谢您提出这个问题,但任何机会。我可以用其他方式做到这一点,因为 Get Att 需要从 lambda 检查条件,然后继续进行最佳方式是什么?
  • @Arunachalam 我更新了答案,但我认为不可能。您可以拆分模板,以便在其他模板中创建 IdentifyUserRole,并作为参数传递给 lambda 模板。
  • 感谢 Marcin 抽出宝贵时间回答问题。但供您参考。经过深思熟虑后,我将“IdentifyUserRole”中的所有 IF 条件部分移到了 Lambda 函数中,并使用 GetAtt 来获取角色 ID。这是我最好的想法。正如你所说,我猜单独的模板也可以。谢谢干杯。
猜你喜欢
  • 2018-12-06
  • 2018-03-06
  • 2018-02-02
  • 1970-01-01
  • 2016-08-04
  • 2016-08-31
  • 2015-01-03
  • 2017-08-18
  • 2019-12-31
相关资源
最近更新 更多