Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
balldarrens opened this issue Mar 8, 2017 · 10 comments
Closed

Comments

@balldarrens
Copy link

balldarrens commented Mar 8, 2017

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?

@wjordan
Copy link

wjordan commented Jul 3, 2018

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):

@smarks
Copy link

smarks commented Oct 22, 2019

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

@jlhood jlhood reopened this Oct 22, 2019
@jlhood
Copy link
Contributor

jlhood commented Oct 22, 2019

Reopening to address improving documentation.

@maletor
Copy link

maletor commented Nov 11, 2019

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

@keetonian
Copy link
Contributor

@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

@awsjeffg
Copy link

awsjeffg commented Sep 4, 2020

@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 awsjeffg closed this as completed Sep 4, 2020
@vilunet
Copy link

vilunet commented Mar 9, 2021

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

@specious
Copy link

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.

@vilunet
Copy link

vilunet commented Aug 28, 2021

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

@specious
Copy link

What exactly are you trying to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants