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

AWS SAM Serverless::Api模板部署后未生成Cognito用户池授权器

问题根因

你当前的授权器配置是绑定在单个Lambda函数的API事件层级的,这种写法只会对/product-get这个POST路径生效授权校验,不会在整个API网关的全局层级注册授权器,所以控制台的「Authorizers」标签页看不到对应的配置。

修复方案

根据你的使用场景可以选择以下两种方案任意一种:

方案1:在全局API配置中声明授权器

如果使用SAM自动创建的隐式API网关,直接在Globals块中新增API层级的授权配置即可,修改后的模板参考如下:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Description here

Globals:
  Function:
    Timeout: 3
  # 新增全局API配置
  Api:
    Auth:
      Authorizers:
        MyCognitoAuth:
          UserPoolArn: "user pool arn here"
          AuthType: "COGNITO_USER_POOLS"
      DefaultAuthorizer: MyCognitoAuth

Resources:
  ProductGet:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: ./
      Handler: product-get.lambda_handler
      Runtime: python3.8
      Role: "particular role here"
      Events:
        ProductGet:
          Type: Api 
          Properties:
            Path: /product-get
            Method: post
            # 无需再重复配置授权,自动继承全局默认配置

方案2:显式声明API网关资源

如果需要单独管理API网关配置,可显式声明AWS::Serverless::Api资源并关联到函数事件,修改后的模板参考如下:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Description here

Globals:
  Function:
    Timeout: 3

Resources:
  # 显式声明API网关资源
  ProductApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Auth:
        Authorizers:
          MyCognitoAuth:
            UserPoolArn: "user pool arn here"
            AuthType: "COGNITO_USER_POOLS"
        DefaultAuthorizer: MyCognitoAuth

  ProductGet:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: ./
      Handler: product-get.lambda_handler
      Runtime: python3.8
      Role: "particular role here"
      Events:
        ProductGet:
          Type: Api 
          Properties:
            Path: /product-get
            Method: post
            # 关联显式声明的API网关
            RestApiId: !Ref ProductApi
验证说明
  • 修改模板后重新执行sam deploy完成部署
  • 部署完成后等待1-2分钟刷新API网关控制台的「Authorizers」标签页,即可看到对应的Cognito授权器
  • 部署前可执行sam validate提前验证模板语法正确性,避免配置错误导致部署不生效

内容的提问来源于stack exchange,提问作者Muhammad Ali

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 18:30:04