使用 Cloud Formation 从 Lambda 获取价值并检查分支条件

Get Value from a Lambda using Cloud Formation and check condition to branch

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

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,因为我们不应该使用获取属性。

错误消息 - 堆栈验证期间

An error occurred (ValidationError) when calling the ValidateTemplate operation: Template error: Fn::If requires a list argument with the first element being a condition

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

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

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

reference to a condition in the Conditions section.

随后,您不能根据 GetAttResources 部分的任何其他资源构造条件。

同样的文档还写道:

You can only reference other conditions and values from the Parameters and Mappings sections of a template. For example, you can reference a value from an input parameter, but you cannot reference the logical ID of a resource in a condition.