4

I'm using the Serverless framework, and I want to be able to reference my API Gateway URL in my acceptance tests.

My test environment is regularly destroyed and then recreated, so hardcoding a URL into the tests is not possible.

I can see there are ways to reference API Gateway as an AWS environment variable, but this doesnt help me to locally get the URL for my tests.

I was hoping that the cloudformation output would be referenced in the .serverless package, and accessible via json, but this doesnt seem to be the case.

Any idea how I can reference the API Gateway URL in my acceptance test files?

NOTE: These tests need to be run on AWS, not using a local server to mimic API Gateway

GWed
  • 15,167
  • 5
  • 62
  • 99
  • 1
    https://forum.serverless.com/t/how-do-i-get-the-url-for-a-function-in-my-serverless-yml-file/1386 – Harshit_Raj Nov 13 '18 at 09:41
  • @Harshit_Raj - That doesn't work in local testing though. Functions like Ref and Fn::Join don't seem to run locally (and probably can't since Ref won't exist before deployment). Only once they're uploaded to AWS are they parsed into actual strings. – Jeffrey Van Alstine Jun 25 '19 at 19:18

2 Answers2

2

The serverless-plugin-test-helper plugin can help here. It will generate a YAML file containing all of the outputs of your stack. This includes a couple of standard ones - the S3 bucket that was used (ServerlessDeploymentBucketName) and the base service endpoint (ServiceEndpoint).

If you are using Node and have your tests in the same directory as the stack being tested then there's also a module to read this file. Otherwise, it's just standard YAML and you can use whatever tools are convenient.

Graham
  • 4,095
  • 4
  • 29
  • 37
0

Consider adding an APIGateway custom domain for your API. You can then use a known DNS name for your acceptance tests.

You will need to add an ApiGateway base path mapping, apigateway domain name, and a route53 recordset to the resources section of your serverless.yml.

cementblocks
  • 4,326
  • 18
  • 24