3

I'm trying to deploy a lambda rest api with my own deployment and I don't want to use the default deployment that is created for you when deploy=True. I'm running into weird errors when trying to explicitly define my own deployment. Here is my stack so far:

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
    )

From other posts I've read, I think I need to attach my stage or deployment to the api but there are no methods with this functionality. I tried doing api_gateway.deployment_stage = stage but that didn't work. CDK is quite new so not much out there, any help would be appreciated.

2 Answers 2

2

api_gateway.deployment_stage = stage should be creating stage name api.

Is your API_gateway creating stage name prod?

Try removing this.
deployment.add_to_logical_id(str(api_gateway.latest_deployment))

Sign up to request clarification or add additional context in comments.

Comments

0

below will work:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.