Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get URL endpoint detail as variable in Serverless Framework's `serverless.yml` file?

Using Serverless Framework to deploy AWS Lambda functions, Serverless creates (or receives) the specific URL endpoint string. I want to use that string (as a variable) in another section of the serverless.yml specification file.

Is that URL endpoint available as a variable in serverless.yml? The Serverless Framework documentation on AWS-related variables does not seem to answer that case.


Details: my serverless.yml contains a provider: specification similar to:

provider:
  name: aws
  runtime: python3.6
  memorySize: 512
  region: ${opt:region, 'eu-west-1'}
  profile: ${opt:profile, 'default'}
  stage: ${opt:stage, 'staging'}

and a functions: section starting with:

functions:
  round-control:
    name: ${self:provider.stage}-round-control
    runtime: nodejs8.10
    handler: round/control/app.lambdaHandler
    events:
      - http:
          path: round/control
          method: get

After a

serverless deploy --profile [my-aws-profile]

the Lambda function sample-experiments-staging-round-control is reported to be available at endpoint https://1a234bc5de.execute-api.eu-west-1.amazonaws.com/staging/round/control.

Question: is there a variable in Serverless available that contains that 1a234bc5de, or 1a234bc5de.execute-api or perhaps even 1a234bc5de.execute-api.eu-west-1.amazonaws.com? (Obviously, I can also construct the last two if I know the first.)

With that variable, I can construct the full URL endpoint, which I need in another place in the serverless.yml file.

N.B. That 1a234bc5de isn't a dynamically generated random string - my current project is (per stage, per region) 'fixed' to the same string. Perhaps that string is generated at AWS Lambda or AWS API Gateway?

like image 597
Jochem Schulenklopper Avatar asked Apr 05 '19 19:04

Jochem Schulenklopper


People also ask

How do I pass an environment variable in serverless?

To reference environment variables, use the ${env:SOME_VAR} syntax in your serverless. yml configuration file. It is valid to use the empty string in place of SOME_VAR . This looks like " ${env:} " and the result of declaring this in your serverless.

How do I refer a property in serverless yml ML file?

Reference Properties In serverless. yml , use the ${self:someProperty} syntax in your serverless. yml . This functionality is recursive, so you can go as deep in the object tree as you want.

How do you trigger an HTTP event in serverless?

HTTP Trigger Azure Functions has an API endpoint created for each Function App. This service allows you to define public HTTP endpoints for your serverless functions. To create HTTP endpoints as Event sources for your Azure Functions, use the Serverless Framework's easy HTTP Events syntax.


1 Answers

I was able to pass the URL and unique ID for the API Gateway endpoint to a Lambda function as environment variables as follows:

  mylambda:
    handler: mylambda.handler
    runtime: python3.7
    events:
    - http:
        path: id
        cors: true
    environment:
      APIG_UID: !Ref "ApiGatewayRestApi"
      APIG_URL:
        !Join
          - ''
          - - 'https://'
            - !Ref ApiGatewayRestApi
            - '.execute-api.'
            - ${opt:region, self:provider.region}
            - '.amazonaws.com/'
            - ${opt:stage, self:provider.stage}

Thanks to goingserverless.

like image 143
jarmod Avatar answered Nov 10 '22 01:11

jarmod