【问题标题】:How to access the return value of a lambda in a another cloudformation resource?如何访问另一个 cloudformation 资源中的 lambda 的返回值?
【发布时间】:2020-03-04 19:02:17
【问题描述】:
  GetClientId:
    Type: "AWS::Lambda::Function"
    Properties:
      Handler: index.handler
      Role: !GetAtt LambdaESCognitoRole.Arn
      Code:
        ZipFile: !Sub |
          var AWS = require('aws-sdk');
          const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
          var response = require('cfn-response');
          var responseData = {};
          exports.handler = async (event, context) => {
            console.log(JSON.stringify(event, null, 2));
            var params = {
              UserPoolId: event.ResourceProperties.UserPoolId
            };
            await cognitoidentityserviceprovider.listUserPoolClients(params, function(err, data) {
              if (err) console.log(err, err.stack); // an error occurred
              else {
                console.log(data); // successful response 
                responseData = {'ClientId': data.UserPoolClients[0].ClientId};
              }
            }).promise();
            response.send(event, context, response.SUCCESS, responseData);
            return;
            }
      Runtime: nodejs8.10 

   CallGetClientId:
     Type: 'Custom::CallGetClientId'
     Version: 1.0
     Properties:
       ServiceToken: !GetAtt GetClientId.Arn
       UserPoolId: !Ref CognitoUserPool

  IdentityPoolRoleMapping:
    Type: "AWS::Cognito::IdentityPoolRoleAttachment"
    Properties:
      IdentityPoolId: !Ref CognitoIdentityPool
      Roles:
        authenticated: !GetAtt AuthenticatedRole.Arn
        unauthenticated: !GetAtt UnauthenticatedRole.Arn
      RoleMappings:
        "cognito-identity-provider":
          IdentityProvider: !Join ['', [ !GetAtt CognitoUserPool.ProviderName, ':', !GetAtt CallGetClientId.ClientId ]] #Need to get the ClientID here
          AmbiguousRoleResolution: Deny
          Type: Rules
          RulesConfiguration:
            Rules:
              - Claim: "custom:groups"
                MatchType: "Contains"
                RoleARN: !GetAtt AuthenticatedRole.Arn
                Value: "user"
              - Claim: "custom:groups"
                MatchType: "Contains"
                RoleARN: !GetAtt AuthenticatedAdminRole.Arn
                Value: "admin"

【问题讨论】:

    标签: aws-lambda mapping amazon-cloudformation amazon-cognito clientid


    【解决方案1】:

    我看到了两种解决问题的方法。

    一 - 使用cfnresponse.send(...responseData) 参数。见这里:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function-code-cfnresponsemodule.html#w2ab1c17c25c14b9c11

    我的例子:

    cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, responseData['ClientSecret'])
    

    从 Lambda 返回数据后,您可以在 CFN 模板中使用 !GetAtt 引用它:

    Value: !GetAtt HapiUserPoolClientPostProc.ClientSecret
    

    二 - 我使用自定义资源作为“后处理器”组件,即创建资源,然后使用自定义资源更新它们的参数。此顺序将由自定义资源 lambda 输入参数(依赖项)来保证。

    我的示例是从我的 ElasticBeanstalk WebApp 中输入 Cognito AppClient 回调 URL。所以我创建了 UserPool AppClient 和 EB webapp,然后一个后处理器自定义资源 lambda 从 EB 获取 URL 并更新 Cognito 中的 CallbackURL。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-01-27
      • 2021-12-24
      • 2021-06-09
      • 2011-04-01
      • 1970-01-01
      • 2020-09-30
      • 2018-06-14
      • 1970-01-01
      • 2019-12-10
      相关资源
      最近更新 更多