Doing conditional redirects using AWS Lambda and AWS API Gateway
As a precondition, you’ve already got AWS Gateway configured.
After pouring through the sparse documentation and limited posts for how to the basic HTTP functionality of just doing a redirect I’ve come up with a way of doing this.
The things you need to do first is:
- Have your code display the URL somehow
- Use context.fail() for displaying the URL
Then
- Go to the API
-
Go to Resources
- Go to the API resource
- Go to the API method (in my case POST)
- Set up a Method Response
-
Add 302, with a location header. Leave Model blank
- Go back to Method Execution
- Go to Integration Response
- Add http.* as an error regex with 302 status
-
Go to Header Mappings, and for Location add “ integration.response.body.errorMessage” (without the quotes.
- Deploy the API, and you should be done
Another good resource you can use is this page too