You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Serverless.yml的Cognito Lambda配置中引用Lambda函数

如何在Serverless YAML中为Cognito关联自定义认证挑战Lambda函数

嘿,我来帮你搞定这个问题!在Serverless Framework里,要把你定义的Lambda函数关联到Cognito的LambdaConfig中,只需要用框架提供的内置变量引用函数ARN即可,非常直观。

修改后的完整配置示例

下面是更新后的YAML代码,重点关注LambdaConfig部分的写法:

functions:
  t-challenge-define:
    handler: t-auth-challenge.define
  t-challenge-create:
    handler: t-auth-challenge.create
  t-challenge-response:
    handler: t-auth-challenge.verifyResponse
resources:
  Resources:
    CognitoUserPool:
      Type: AWS::Cognito::UserPool
      Properties:
        UserPoolName: my_user_pool_name
        MfaConfiguration: "OFF"
        UsernameAttributes:
          - phone_number
        Schema:
          - Name: phone_number
            AttributeDataType: String
            Mutable: false
            Required: true
          - Name: locale
            AttributeDataType: String
            Mutable: true
            Required: true
        LambdaConfig:
          # 用Serverless内置变量引用对应Lambda的ARN
          DefineAuthChallenge: ${self:functions.t-challenge-define.arn}
          CreateAuthChallenge: ${self:functions.t-challenge-create.arn}
          VerifyAuthChallengeResponse: ${self:functions.t-challenge-response.arn}

关键说明

  • ${self:functions.<function-name>.arn}是Serverless Framework的内置变量,部署时会自动解析为对应Lambda函数的实际ARN地址,让Cognito能准确关联到你的自定义认证逻辑。
  • 自定义认证挑战通常需要三个Lambda函数配合(定义挑战规则、生成挑战内容、验证用户响应),所以我顺便把另外两个函数也配置上了,保证逻辑完整。

这样配置后,执行serverless deploy时,框架会自动完成Cognito与Lambda函数的关联,不用你手动复制ARN啦~

内容的提问来源于stack exchange,提问作者Rasikh Mashhadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:54:09