amazon-web-services - AWS CDK - 将 Cognito 用户池绑定到 API Gateway
问题描述
我正在尝试使用 AWS CDK 设置一个非常基本的基础设施,其中包括
- Cognito 用户池
- 一个拉姆达
- 授权人
- 具有根路径和子路径以及少数方法的 API GW
- 应该使用 Cognito 用户池作为授权者的路径
问题:我不知道如何正确创建授权者,所以它被 API GW 接受
合成 CF 模板工作正常,但在部署过程中出现以下错误:
Invalid authorizer ID specified. Setting the authorization type to CUSTOM or COGNITO_USER_POOLS requires a valid authorizer.
所以,我的授权人似乎不够合适,即使授权人本身的创建在部署期间被标记为成功。
有任何想法吗?
这是我在代码中的内容:
from aws_cdk import (
core,
aws_lambda,
aws_apigateway,
aws_cognito
)
class PlayGroundStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Basic lamdba setup
my_lambda = aws_lambda.Function(
scope=self,
id="lambda-1",
runtime=aws_lambda.Runtime.PYTHON_3_6,
code=aws_lambda.Code.asset('lambda'),
handler='hello.handler'
)
# Few MethodOptions for the API GW
my_get_operation = aws_apigateway.MethodOptions(operation_name="GET")
my_post_operation = aws_apigateway.MethodOptions(operation_name="POST")
# The API itself
rest_api = aws_apigateway.LambdaRestApi(
scope=self,
id="rest-api-1",
handler=my_lambda,
proxy=False,
default_method_options=my_get_operation
)
# User Pool used for auth
api_userpool = aws_cognito.UserPool(
scope=self,
id="user-pool-1",
sign_in_type=aws_cognito.SignInType.EMAIL
)
# The authorizer that should be attached to API GW
my_cfn_authorizer = aws_apigateway.CfnAuthorizer(
id='api_authorizer_cfn',
rest_api_id=rest_api.rest_api_id,
name='api_authorizer_cfn_name',
scope=self,
type='COGNITO_USER_POOLS',
identity_source='method.request.header.Authorization',
provider_arns=[api_userpool.user_pool_arn]
)
# Set methods for root node
rest_api.root.add_method(my_get_operation.operation_name)
rest_api.root.add_method(my_post_operation.operation_name)
# Create a resource
my_resource = rest_api.root.add_resource("my_sub_url")
# And attach the Authorizer
# This is where I get the error
my_resource.add_method(http_method="GET",
authorization_type=aws_apigateway.AuthorizationType.COGNITO,
authorizer=my_cfn_authorizer)
解决方案
我想到了。你需要设置
authorization_type = my_cfn_authorizer.auth_type
这解决了一切。如果他们能在文档中告诉我们这样做会好得多,但是 python 文档很少。
class PlayGroundStack(core.Stack):
...
# The authorizer that should be attached to API GW
my_cfn_authorizer = aws_apigateway.CfnAuthorizer(
id='api_authorizer_cfn',
rest_api_id=rest_api.rest_api_id,
name='api_authorizer_cfn_name',
scope=self,
type='COGNITO_USER_POOLS',
identity_source='method.request.header.Authorization',
provider_arns=[api_userpool.user_pool_arn]
)
# Set methods for root node
rest_api.root.add_method(my_get_operation.operation_name)
rest_api.root.add_method(my_post_operation.operation_name)
my_resource = rest_api.root.add_resource("my_sub_url")
# FIX HERE!!!!!!!
my_resource.add_method(http_method="GET",
authorization_type=my_cfn_authorizer.auth_type,
authorizer=my_cfn_authorizer)
推荐阅读
- reactjs - 用酶和茉莉花测试高阶成分
- python - 类实例方法只需要两个参数,一个给定
- wordpress - 尝试在 wordpress 仪表板中嵌入 Angular 应用程序:是否可以在没有 add_menu_page 功能的情况下创建自定义管理页面?
- c++ - 受控序列与关联序列?
- python - dict_items 的实例排序/按值排序字典
- python - Python Pandas 根据条件创建运行的“id”
- ios - Swift iOS - 如何在两个 UIWindows 之间切换/切换并制作 keyWindow
- javascript - Firebase 规则允许用户阻止其他用户读取他们的数据
- event-handling - 在事件溯源中将快照作为领域事件
- javascript - 如何用 Enzyme 测试 antd Dropdown