我正试图用自己的部署部署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
)
从我读过的其他帖子中,我认为我需要将我的阶段或部署附加到api,但没有具有此功能的方法。我试着做api_gateway.deployment_stage = stage
,但没有成功。CDK是一个很新的东西,所以没有太多,任何帮助都将不胜感激。
api_gateway.deployment_stage = stage
应该创建阶段名称api
。
您的API_gateway是否正在创建阶段名称prod
?
请尝试删除此deployment.add_to_logical_id(str(api_gateway.latest_deployment))
以下内容将起作用:
api_gateway.add_usage_plan(
id="UsagePlan",
name="poc-voice-qa-usage-plan",
description="poc usage plan",
api_stages=[apigw.UsagePlanPerApiStage(stage=api_gateway.deployment_stage)]
)