API GW pointing to existing Lambda

Hello,

I’m trying to point to an existing Lambda function within the same API Gateway.

Sample code:

functions:
token-service:
name: spil-{opt:stage}-token-service handler: main/lambda_function.lambda_handler package: artifact: build.zip memorySize: 512 role: {env:lambdaRole}
timeout: 30
tags:
billingTeam: [redacted]
environment:
region: {opt:region} stage: {opt:stage}
events:
- http:
cors: true
path: generate_tokens
method: get
- http:
path: healthcheck
method: get
integration: lambda
arn: arn:aws:lambda:{opt:region}:{env:accountId}:function:lambda_health_check

I’d like the same API endpoint with path /healthcheck to point to an existing lambda function.

Currently, the /healthcheck path is created but it doesn’t point to the correct lambda function. It points to the function that serverless.yml creates.

You would need to add a custom ApiGateway::Resource and ApiGateway::Method in the resources section of your serverless.yml.

Which is expected. In the functions section of your serverless.yml you define your Lambda functions and which events trigger them. The syntax you’re using makes no sense.

Thanks for your response!

I’ve tried to create a custom resource with API GW and method. Perhaps I’m wrong, but that would create it’s own API gateway endpoint? I would end up with two API endpoints. One via serverless and another via the custom resource.

The Serverless Framework basically transforms your serverless.yml into a CloudFormation template. You can override and extend the template it outputs through the resources section in your serverless.yml. Just make sure you adding it to the API GW created by Serverless and you’re not creating a new one.

Hi,

As you said above. I have created custom API Gateway resource and method .

  Resources:
    ProxyResource:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId:
          Fn::GetAtt:
            - ApiGatewayRestApi # our default Rest API logical ID
            - RootResourceId
        PathPart: healthcheck # the endpoint in your API that is set as proxy
        RestApiId:
          Ref: ApiGatewayRestApi
    ProxyMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        ResourceId:
          Ref: ProxyResource
        RestApiId:
          Ref: ApiGatewayRestApi
        HttpMethod: GET # the method of your proxy. Is it GET or POST or ... ?
        MethodResponses:
          - StatusCode: 200
        Integration:
          IntegrationHttpMethod: GET
          Type: HTTP
          Uri: arn:aws:lambda:${opt:region}:${env:accountId}:function:lambda_health_check
          IntegrationResponses:
            - StatusCode: 200```
it gives an error.
An error occurred: ProxyResource - Another resource with the same parent already has this name: healthcheck (Service: AmazonApiGateway; Status Code: 409; Error Code: ConflictException; Request ID: 1ddbceb6-a405-4930-9962-dfaf4b376664).

Is it still the case that you cannot connect to an api gateway created outside of your cloud formation template via your serverless.yml file? My lambda function needs to get triggered from the api gateway. This API gateway is not created as part of my serverless stack. The api gateway is set up already and integrated with cognito and my serverless.yml needs to create a lambda function that gets triggered by that existing api gateway. Is this not possible?
So far I have only found the solution where I need to create api gateway via serverless.yml then import it in another serverless.yml as a resource. Unfortunately, how this api gateway gets created is not upto me and my lambda has to get triggered from that existing API gateway. Please do let me know if anyone has solved this or there is no solution for this yet.