Skip to content

(aws_apigatewayv2.HttpLambdaIntegration): add_routes integration to subroute doesn't take affect #18965

@mitchboulay

Description

@mitchboulay

What is the problem?

Given (Pre configuration required):
from aws_cdk.aws_apigatewayv2_integrations import HttpLambdaIntegration
API gateway configured with routes & subroutes, connected to a lambda function via CDK (Python)

Looks like a stack is not accepting multiple aws_apigatewayv2.HttpApi.add_routes() as done below in test.py

When (Action taken): Function looked up via Function.from_function_arn and applied to integration parameter of http_api.add_routes

Reproduction Steps

app.py

import os
from aws_cdk import core as cdk
from src.aws_modules.APIGateway_test.test import testStack as test

app = cdk.App()
environment = pc.getStackEnvironment()

if __name__ == "__main__":
    test(scope=app, id="test-stack-id",env=environment).run_stack()
    app.synth()

test.py

import os
from aws_cdk.aws_apigatewayv2_integrations import HttpLambdaIntegration
from aws_cdk import (aws_apigatewayv2 as apigwv2,aws_lambda as lambda_)
from aws_cdk import core as cdk
from aws_cdk.aws_apigatewayv2_authorizers import HttpLambdaAuthorizer, HttpLambdaResponseType

class testStack(cdk.Stack):


    def run_stack(self):

        current_region = os.environ['CDK_DEFAULT_REGION']
        current_account = os.environ['CDK_DEFAULT_ACCOUNT']
        authorizer_lambda_name = "test-authorizer-api-dev"
        integration_lambda_name = "test-service-dev"

        api_name = "my-test-API-for-AWS"
        http_api = apigwv2.HttpApi(self, "test-api-gatewayv2",
        cors_preflight=apigwv2.CorsPreflightOptions(
        allow_headers=["*"],
        allow_methods=[apigwv2.CorsHttpMethod.ANY],
        allow_origins=["*"],
        max_age=cdk.Duration.days(10)

    )
        )


        authorizer_function = lambda_.Function.from_function_arn(
            self,
            id="found-function-authorizer",
            function_arn="arn:aws:lambda:{}:{}:function:{}".format(current_region,current_account,authorizer_lambda_name)
            )
        authorizer_test = HttpLambdaAuthorizer("test-authorizer", authorizer_function,
            response_types=[HttpLambdaResponseType.SIMPLE]
        )

        integration_function = lambda_.Function.from_function_arn(
            self,
            id="found-function-integration",
            function_arn="arn:aws:lambda:{}:{}:function:{}".format(current_region,current_account,integration_lambda_name)
            )
        function_integration = HttpLambdaIntegration("Test-FunctionIntegration", integration_function)


        http_api.add_routes(
            path="/companies",
            methods=[apigwv2.HttpMethod.GET],
            integration=function_integration,
            authorizer=authorizer_test
            )

        http_api.add_routes(
            path="/companies/swagger",
            methods=[apigwv2.HttpMethod.GET],
            integration=function_integration
            )

What did you expect to happen?

Then (Expected behavior, per documentation): Route has lambda integration defined, attached and working.

What actually happened?

Actual (Observed behavior): Route gains integration ID and appears to have function assigned to it but this is not the case. This is verified by navigating to the lambda function under Configuration, the sub-route is not defined as an endpoint.

CDK CLI Version

2.8.0 (build 8a5eb49)

Framework Version

No response

Node.js Version

v14.17.3

OS

MacOS

Language

Python

Language Version

Python 3.9.8

Other information

This looks to be an issue with the add_routes method as the from_function_arn works for the authorizer.

Workaround:

  1. CDK deploys API gateway
  2. Navigate API Gateway > integrations > Select sub route > Select HTTP method
  3. Select "Manage integration" button
  4. Select "Edit"
  5. Select "Save" button at bottom right
  6. Check lambda function > Configuration
  7. You will see the route is now there.

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-apigatewayv2Related to Amazon API Gateway v2bugThis issue is a bug.effort/mediumMedium work item – several days of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions