Need to configure serverless resource output to get api gateway api id
Asked Answered
C

4

11

I have a serverless project that is creating an API Gateway API amongst other things. One of the functions in the project needs to generate a URL for an API endpoint.

My plan is to get the API ID using a resource output in serverless.yml then create the URL and pass it through to the lambda function as an env parameter.

My problem/question is how to get the API ID as a cloud formation output in serverless.yml?

I've tried:

resources:
  Outputs:
    RESTApiId:
      Description: The id of the API created in the API gateway
      Value:
        Ref: name-of-api

but this give the error:

The CloudFormation template is invalid: Unresolved resource dependencies [name-of-api] in the Outputs block of the template
Clarethaclaretta answered 5/7, 2017 at 23:47 Comment(1)
If it's inside the same project, can't you just output {Ref: myApiGateway} into an environment variable of the function?Nadler
R
3

You can write something like this in the serverless.yml file:

provider:
  region: ${opt:region, 'eu-west-1'}
  stage: ${opt:stage, 'dev'}
  environment:
    REST_API_URL:
      Fn::Join:
        - ""
        - - "https://"
          - Ref: "ApiGatewayRestApi"
          - ".execute-api."
          - ${self:provider.region}
          - Ref: "AWS::URLSuffix"
          - "/"
          - ${self:provider.stage}"

Now you can call serverless with optional commandline options --stage and/or --region to override the defaults defined above, e.g:

serverless deploy --stage production --region us-east-1

In your code you can then use the environment variable REST_API_URL

node.js:

const restApiUrl = process.env.REST_API_URL;

python:

import os
rest_api_url = os.environ['REST_API_URL']

Java:

String restApiUrl = System.getenv("REST_API_URL");
Ruddle answered 14/2, 2019 at 14:29 Comment(1)
It is not the answer to this qustion. Outputs can share between the stacks, but environments notCryptogam
B
1

The serverless framework has a documentation page on how they generate names for resources.

See. AWS CloudFormation Resource Reference

So the generated RestAPI resource is called ApiGatewayRestApi.

Brainless answered 6/7, 2017 at 9:40 Comment(0)
R
0

Unfortunately, the documentation doesn't mention it:

resources:
  Outputs:
    apiGatewayHttpApiId:
      Value:
        Ref: HttpApi
      Export:
        Name: YourAppHttpApiId
Rickettsia answered 6/1, 2022 at 22:8 Comment(0)
C
0

You need to define an API (resources.Resources.HttpApi) and then you can use the alias (HttpApi) to get the ID.

resources: {
    Resources: {
        HttpApi: {
            Type: 'AWS::ApiGatewayV2::Api',
            Properties: {
                Name: 'MyHttpApi',
                ProtocolType: 'HTTP' // Or 'WEBSOCKET',
            },
        },
    },
    Outputs: {
        Id: {
            Value: { Ref: 'HttpApi' },
            Export: { Name: 'MyHttpApiId' },
        },
    },
},

Note: I'm using Typescript to write my Serverless files, so you'll have to remove the "{", "}"

Cognomen answered 15/3 at 22:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.