【问题标题】:AWS CDK API Gateway - how to deploy API without creating default deployment (python)AWS CDK API Gateway - 如何在不创建默认部署的情况下部署 API (python)
【发布时间】:2020-11-01 01:51:26
【问题描述】:

我正在尝试使用我自己的部署来部署 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 很新,所以没有太多,任何帮助将不胜感激。

【问题讨论】:

    标签: python-3.x amazon-web-services aws-lambda aws-api-gateway aws-cdk


    【解决方案1】:

    api_gateway.deployment_stage = stage 应该是创建舞台名称api

    您的 API_gateway 是否正在创建阶段名称 prod

    尝试删除它。
    deployment.add_to_logical_id(str(api_gateway.latest_deployment))

    【讨论】:

      【解决方案2】:

      以下将起作用:

              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)]
              )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-12
        • 2020-09-23
        • 2016-08-14
        • 2021-01-29
        • 2021-12-29
        • 2015-10-10
        相关资源
        最近更新 更多