首页 > 解决方案 > 使用 Cloud Formation 从 Lambda 获取价值并检查分支的条件

问题描述

我附上了样本,让您清楚地了解我要解决的问题。

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条件处遇到错误。请使用一些示例模板帮助解决这种情况。我也不能在条件块中使用 !GetAtt ,因为我们不应该使用 get 属性。

错误消息 - 在堆栈验证期间

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

标签: amazon-web-servicesamazon-cloudformationaws-cloudformation-custom-resource

解决方案


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

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

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

参考条件部分中的条件。

随后,您不能基于部分GetAtt或任何其他资源构建条件Resources

相同的文档还写道:

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


推荐阅读