如何将arn cognito动态添加到lambda?

时间:2019-09-11 11:28:04

标签: aws-lambda yaml amazon-cognito serverless-framework aws-serverless

我想在我的lambda函数中添加cognito授权者,但是为此,我需要arn cognito,它是在堆栈代码结构(我的资源部分中的CognitoUserPool)中创建的。我正在使用无服务器框架。

serverless.yml文件的一部分

  graphql:
    handler: src/lambda-functions/graphql/index.handler
    timeout: 30
    memorySize: 2048
    events:
      - http:
          path: graphql
          method: any
          private: true
          cors: true
          authorizer:
            arn:
              Fn::Join:
                - ''
                - - 'arn:aws:cognito-idp:'
                  - ${self:provider.region}
                  - ':'
                  - Ref: AWS::AccountId
                  - ':userpool/'
                  - Ref: CognitoUserPool

在部署应用程序时出现错误:

 TypeError: functionArn.split is not a function

在调试时,我发现 Fn :: join 的输出应该传递给该对象:

{"Fn::Join":["",["arn:aws:cognito-idp:","eu-west-1",":",{"Ref":"AWS::AccountId"},":userpool/",{"Ref":"CognitoUserPool"}]]}

并且应该将已经解析的arn传递给该函数,例如:

arn:aws:cognito-idp:eu-west-1:XXXXXXXXXX:userpool/eu-west-XXXXXXX 

如何强制计算 Fn :: join 的输出并将该值传递给 arn 属性?

1 个答案:

答案 0 :(得分:1)

根据this,给授权者一个名称,就可以使用内部函数来引用ARN:

graphql:
  handler: src/lambda-functions/graphql/index.handler
  timeout: 30
  memorySize: 2048
  events:
    - http:
        path: graphql
        method: any
        private: true
        cors: true
        authorizer:
          name: CognitoAuthorizer #supply a unique name here
          arn:
            !GetAtt CognitoUserPool.Arn