Doing a 302 redirect in python?

Hi Folks-

I’m pretty new to lambda and Serverless (and python) so please bear with me…

I’m trying to write a GET function that is passed an id, ans uses that id to look up a redirect URI from a map.
The map is built from a bundled JSON file.

If a redirect uri is found for that code, it returns a 302, and the location of the url.

If no uri is found for that id, it returns a 404.

I have found some posts that explain how to do this in lambda from the AWS Console, but I cannot figure out what my serverless.yml file needs to look like for the API part of this. If anyone’s got an example of something like this, and can share it, I’d be most appreciative.

Thanks,

Mark

This should be pretty easy if you’re using the (default) Lambda Proxy integration for your function.
Here’s a code example in the AWS docs.

Basically:

  • Set your responseCode to “302” and include a Location key/value in your headers to point them to the new location.
  • If nothing is found, just change the responseCode to 404.
  • Don’t forget to stringify your body (for the error message) - I aways do.
1 Like

Thanks for the answer. I was making it much harder that it was. All the redirect examples I could find online for python were written before the advent of LAMBDA_PROXY, and I was following them. It works now.