我试着使用自己的部署方式部署Lambda REST API,而不是使用deploy=True
时默认创建的部署方式。但是在尝试显式定义自己的部署方式时,我遇到了一些奇怪的错误。我的堆栈如下:
class Stack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# existing lambda with correct permissions
lambda_function = aws_lambda.Function.from_function_arn(self, "lambda",
"arn")
api_gateway = aws_apigateway.LambdaRestApi(
self,
id="APIGateway",
proxy=False,
description="poc apigateway",
rest_api_name="poc-voice-qa-api",
handler=lambda_function,
deploy=False
)
api_key = api_gateway.add_api_key(
id="ApiKey",
api_key_name="voice-qa-api-key"
)
deployment = aws_apigateway.Deployment(
self,
id="Deployment",
api=api_gateway
)
deployment.add_to_logical_id(str(api_gateway.latest_deployment))
stage = aws_apigateway.Stage(
self,
id="DeploymentStage",
deployment=deployment,
stage_name="api"
)
stage_usage_plan = aws_apigateway.UsagePlanPerApiStage(
api=api_gateway,
stage=stage
)
api_gateway.add_usage_plan(
id="UsagePlan",
api_key=api_key,
api_stages=[stage_usage_plan],
description="poc usage plan",
name="poc-voice-qa-usage-plan"
)
resource = api_gateway.root.add_resource(
path_part="qa"
)
resource = resource.add_resource(
path_part="test"
)
lambda_integration = aws_apigateway.LambdaIntegration(
handler=lambda_function,
passthrough_behavior=aws_apigateway.PassthroughBehavior.WHEN_NO_MATCH
)
resource.add_method(
"GET",
lambda_integration,
api_key_required=True
)
resource.add_method(
"POST",
lambda_integration,
api_key_required=True
)
根据我阅读的其他文章,我认为我需要将我的“stage”或“deployment”附加到API上,但没有与此功能相关的方法。我尝试过 api_gateway.deployment_stage = stage
,但没有成功。由于CDK还比较新,所以相关信息不多,非常感谢您能提供帮助。