Referencing HttpApi base URL in serverless.yml

I was previously referencing a REST API URL through:

    !Join
      - ''
      - - 'https://'
        - !Ref ApiGatewayRestApi
        - '.execute-api.'
        - ${opt:region, self:provider.region}
        - '.amazonaws.com/'
        - ${opt:stage, self:provider.stage}

However, since my app is not rest, I have moved to using HttpApi events instead of REST events, i.e.

- httpApi:
      method: POST
      path: /register

How can I reference the path to this to pass it as an environment variable to my lambda?

Serverless Framwork creates an output called “HttpApiUrl” when deploying. I looked in the template update stack that is generated and lifted this CloudFormation code from there. I’m using the JSON format here so you may have to adjust if you’re using yaml.

    environment: {
  ImportedValue: {
    "Fn::Join": [
      "",
      [
        "https://",
        {
          Ref: "HttpApi",
        },
        ".execute-api.",
        {
          Ref: "AWS::Region",
        },
        ".",
        {
          Ref: "AWS::URLSuffix",
        },
      ],
    ],
  },
}

I hope this helps.

1 Like

It does, thank you! Exactly what I was looking for. I haven’t worked with Serverless for long and even less with CloudFormation so this just saved me a ton of digging.

Thanks!

1 Like