serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Share API Gateway and API Resources

Open hoangleitvn opened this issue 7 years ago • 6 comments

When using serverless framework, we are able to use shared api gatewate and resources to avoid creating multiple rest api for micro-services. Basically, we have another CF template to create a stack that create Rest API and export ID and root resources Id so that we can reference in other stacks. I reviewed SAM documentation but cannot find any reference. I would be great if SAM supports this feature.

Actually, the API event of the Lambda function supports the RestApiId property, but it must is a reference not a plaintext RestApiId. When I try with a hardcode, I got below error

Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HelloWorldFunction] is invalid. Event with id [HelloWorld] is invalid. RestApiId must be a valid reference to an 'AWS::Serverless::Api' resource in same template.

We can either: Support global of the API to define the API gateway we will create sub-resources

Globals:
  Function:
    Timeout: 30
  
  Api:
    RestApiId: xxxxxxxxxx # REST API resource ID. Default is generated by the framework
    RestApiRootResourceId: xxxxxxxxxx # Root resource, represent as / path

or support in the individual event

HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello-world/
      Handler: index.handler
      Runtime: nodejs8.10
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /service-a/hello
            RestApiId: xxxxxxxxxx # REST API resource ID. Default is generated by the framework
            RestApiRootResourceId: xxxxxxxxxx # Root resource, represent as / path
            Method: get

One more thing I wanted to note here is SAM does not support create explicitly API, SAM creates an API based on the events of the Lambda function, but for our root stack, we only want to create an API. We can use native CloudFormation resources, but if SAM supports that feature, it would be better, we can reduce the number of resources defined in the template.

Here is the screenshot that shows my setup Screen Shot 2019-06-06 at 2 32 58 PM

Reference: https://serverless.com/framework/docs/providers/aws/events/apigateway/

hoangleitvn avatar Jun 06 '19 06:06 hoangleitvn

@hoang-innomizetech I believe this use case would be covered by #866. The key difference is that you would specify the API events within the same template as the API, rather than in the template containing the function. Can you take a look at that issue and confirm if it would be sufficient to solve this issue?

jlhood avatar Jun 14 '19 17:06 jlhood

I read comments in #866 but cannot find out any idea to implement our requirement, currently, SAM does not provide any way to add resources to API Gateway created by another stack (or another serverless application). RestApiId is only allowed in the same template.

hoangleitvn avatar Jul 18 '19 02:07 hoangleitvn

Correct; #866 hasn't been implemented yet so those features aren't available. Please +1 that issue to help prioritize it 👍

keetonian avatar Jul 19 '19 17:07 keetonian

Hi! I was wondering if there is a way to get the ID of the default REST API generated by the framework when I integrate an HTTP event within a Lambda Function like this:

myFunction:
    handler: example.handler
    events:
      - http:
          path: execution
          method: get

I need to obtain the API GW endpoint because I'm implementing a Callback Pattern in the AWS StepFunctions service. I imagine that obtaining at least its ID from the template will be enough. Can someone point me in the right direction? Thanks

mgleria avatar Jan 21 '21 21:01 mgleria

Hi! I was wondering if there is a way to get the ID of the default REST API generated by the framework when I integrate an HTTP event within a Lambda Function like this:

myFunction:
    handler: example.handler
    events:
      - http:
          path: execution
          method: get

I need to obtain the API GW endpoint because I'm implementing a Callback Pattern in the AWS StepFunctions service. I imagine that obtaining at least its ID from the template will be enough. Can someone point me in the right direction? Thanks

I've just found it in a comment here based on this. It would be great if this was better documented.

The ID of the default REST API generated by Serverless can be referenced by: !Ref ApiGatewayRestApi

mgleria avatar Jan 21 '21 21:01 mgleria

Hi! It would be really great if we could have this feature. Any updates on this?

Thank you!

MatiasGaribotti avatar May 31 '23 13:05 MatiasGaribotti