# API GW pointing to existing Lambda

**URL:** <https://forum.serverless.com/t/api-gw-pointing-to-existing-lambda/4657>\
**Category:** Serverless Framework\
**Created:** [June 4, 2018, 6:15pm UTC](https://forum.serverless.com/t/api-gw-pointing-to-existing-lambda/4657 "2018-06-04T18:15:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashishxftw](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@ashishxftw](https://forum.serverless.com/u/ashishxftw)\
**Post date:** [June 4, 2018, 6:15pm UTC](https://forum.serverless.com/t/api-gw-pointing-to-existing-lambda/4657/1 "2018-06-04T18:15:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [June 5, 2018, 3:08am UTC](https://forum.serverless.com/t/api-gw-pointing-to-existing-lambda/4657/2 "2018-06-05T03:08:18Z")

</div>

> [@ashishxftw](#):
>
> I’d like the same API endpoint with path /healthcheck to point to an existing lambda function.

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

> [@ashishxftw](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![ashishxftw](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@ashishxftw](https://forum.serverless.com/u/ashishxftw)\
**Post date:** [June 5, 2018, 1:59pm UTC](https://forum.serverless.com/t/api-gw-pointing-to-existing-lambda/4657/3 "2018-06-05T13:59:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [June 5, 2018, 11:10pm UTC](https://forum.serverless.com/t/api-gw-pointing-to-existing-lambda/4657/4 "2018-06-05T23:10:29Z")

</div>

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.

> **[Serverless Framework - AWS Infrastructure Resources](https://www.serverless.com/framework/docs/providers/aws/guide/resources/)**
>
> The Serverless Framework documentation for AWS Lambda, API Gateway, EventBridge, DynamoDB and much more.

---

<div class="post-metadata">

**Author:** ![purna777](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@purna777](https://forum.serverless.com/u/purna777)\
**Post date:** [July 30, 2019, 2:13pm UTC](https://forum.serverless.com/t/api-gw-pointing-to-existing-lambda/4657/5 "2019-07-30T14:13:45Z")

</div>

Hi,

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

````auto
  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).
````

---

<div class="post-metadata">

**Author:** ![aiftiqhar](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@aiftiqhar](https://forum.serverless.com/u/aiftiqhar)\
**Post date:** [May 19, 2021, 3:12pm UTC](https://forum.serverless.com/t/api-gw-pointing-to-existing-lambda/4657/6 "2021-05-19T15:12:55Z")

</div>

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.
