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

Ref to API Gateway RestApi from AWS::Serverless::Function Api Event Source

Open balldarrens opened this issue 9 years ago • 10 comments

Update - I have figured this out. Using ${ServerlessRestApi}, of which is not documented in a clear way, this is possible to build the URL. This is in an example provide in the examples folder. Just was not clear, or I was just foggy when reading that.

Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Stage/"

I am wanting to output (Outputs) the underlying API Gateway RestApi from a AWS::Serverless::Function that is allowing this to be auto-generated. I have multiple AWS::Serverless::Function's in play, but as a short example:

Resources:
  Create:
    Type: AWS::Serverless::Function
    Properties:
      Handler: meta/create.create
      CodeUri: app/
      Runtime: nodejs4.3
      Policies: AWSLambdaBasicExecutionRole
      Environment:
        Variables:
          DYNAMODB_TABLE_ENV: !Sub '${ServiceName}-${Stage}'
      Events:
        ApiEventSource:
          Type: Api
          Properties:
            Path: /
            Method: POST

  Delete:
    Type: AWS::Serverless::Function
    Properties:
      Handler: meta/delete.delete
      CodeUri: app/
      Runtime: nodejs4.3
      Policies: AWSLambdaBasicExecutionRole
      Environment:
        Variables:
          DYNAMODB_TABLE_ENV: !Sub '${ServiceName}-${Stage}'
      Events:
        ApiEventSource:
          Type: Api
          Properties:
            Path: /meta/{metaId}
            Method: DELETE

How is one to access the API rest api, for example to output the url? Is there an example of this somewhere?

balldarrens avatar Mar 08 '17 15:03 balldarrens

For my own future reference (when I find this issue again via Google in the future), here are the documentation references (I agree it's not laid out in a clear way):

wjordan avatar Jul 03 '18 19:07 wjordan

It may be documented as described above, but not well enough to be functional. this issue should be reopened.

smarks avatar Oct 22 '19 15:10 smarks

Reopening to address improving documentation.

jlhood avatar Oct 22 '19 15:10 jlhood

I think it's more than documentation. You get a circular reference trying to input the API URL into an environment variable.

maletor avatar Nov 11 '19 03:11 maletor

@maletor I don't think you need to input the API URL into an environment variable, you get the API ID and anything else needed to create the URL from the event object.

Here's a sample API Event Object from the Lambda documentation: https://docs.aws.amazon.com/lambda/latest/dg/with-on-demand-https.html

keetonian avatar Nov 12 '19 18:11 keetonian

@balldarrens @maletor did the documentation mentioned by @keetonian help with this issue? We are closing this issue due to inactivity, but please re-open of you are still having issues with this feature, in particular with the documentation.

awsjeffg avatar Sep 04 '20 22:09 awsjeffg

I have a similar issue, I got SAM to generate the API and wanting to call one of the Lambda functions to call into the API (in StateMachine). How Can the ServerlessRestApi be referred in AWS::Serverless::Function code? Still, the documentation is not clear

vilunet avatar Mar 09 '21 06:03 vilunet

If you define one or more functions that have an API event source that triggers that function and do not also define a gateway API that you then link to these functions by specifying RestApiId: !Ref <api-name>, then sam-cli will create what is called an "implicit" API and it will name it ServerlessRestApi. That API will have one stage which will be named Prod.

This means that a template like this one will actually deploy a function behind an automatically created API gateway:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Lambda function behind an API gateway

Resources:
  MyLambdaFunction: # you define this
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs14.x
      CodeUri: lambda.zip
      PackageType: Zip
      Description: This lambda does something useful
      Policies:
        - AWSLambdaBasicExecutionRole
      Events:
        RootGet: # you define this
          Type: Api
          Properties:
            Path: /
            Method: GET

Outputs:
  API:
    Description: "API Gateway endpoint URL"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${ServerlessRestApi.Stage}/"

Given so much confusing information, it took me some time to figure all this out.

specious avatar Aug 21 '21 06:08 specious

Thanks for the comment. Issue is in defining the RestAPI within the same template. If Lambda is in the same template its not not get linked to the newly defined RestAPI

vilunet avatar Aug 28 '21 06:08 vilunet

What exactly are you trying to do?

specious avatar Aug 29 '21 00:08 specious