Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CDK API Gateway - how to deploy API without creating default deployment (python)

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.

like image 860
Rishi Kapadia Avatar asked Dec 18 '25 11:12

Rishi Kapadia


1 Answers

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

like image 157
Harshal Pagar Avatar answered Dec 20 '25 09:12

Harshal Pagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!