Home > Enterprise >  Error deploying a CDK stack with a Rest API with a Lambda Function Integration (AWS CDK 2 Python API
Error deploying a CDK stack with a Rest API with a Lambda Function Integration (AWS CDK 2 Python API

Time:05-11

I am trying to create a simple Rest API using, AWS Lambda, API Gateway and AWS CDK 2.16 Python API.

This Rest API should have a resource with a method with the CORS property enabled and a POST method with a lambda function integration.

But, the error happens when I add the method (using add_method) and deploy the stack.

Code:

    lambda_predictor = lambda_.CfnFunction(self, "LambdaPredictor",
                                            code = lambda_.CfnFunction.CodeProperty(s3_bucket="my-bucket",
                                                                                    s3_key=lambda_predictor_location),
                                            role =lambda_glue_role.attr_arn,
                                            # properties
                                            architectures = ["x86_64"],
                                            description = 'Lambda function',
                                            function_name = f"{environment}-{project}-lambda-predictor",
                                            handler = "lambda_function.lambda_handler",
                                            memory_size = 256,
                                            layers=[layer.layer_version_arn],
                                            package_type = "Zip",
                                            runtime = "python3.8",
                                            timeout = 30) 
                                            
    
    lambda_predictor.add_depends_on(lambda_glue_role)  
    
    rest_api = apigateway.RestApi(self, "InferencePipelineRestApi",
                                  rest_api_name="InferencePipelineAPI")
                          
    api_resource = rest_api.root.add_resource('myResource',
                                      default_cors_preflight_options=apigateway.CorsOptions(
                                          allow_headers=['Content-Type', 'X-Amz-Date', 'Authorization', 'X-Api-Key', 'X-Amz-Security-Token'],
                                          allow_methods=['OPTIONS', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
                                          allow_origins=["*"])
                                    )
    api_resource.add_method("POST", apigateway.LambdaIntegration(lambda_predictor))

The error:

'''

jsii.errors.JavaScriptError: 
  TypeError: this.handler.addPermission is not a function

Do you have and idea what could be happening?

Thank you in advance

CodePudding user response:

You're passing a low-level (L1) lambda function construct, CfnFunction, whereas LambdaIntegration only accepts IFunction, which is implemented by higher-level (L2) constructs like Function.

Relevant docs: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.LambdaIntegration.html

Any reason why you're using the L1 Construct?

  • Related