Pass dynamic path via API-Gateway to AWS-Lambda - amazon-web-services

I'm running a Flask-application on AWS-Lambda based on this tutorial:
https://andrewgriffithsonline.com/blog/180412-deploy-flask-api-any-serverless-cloud-platform/#create-flask-app
My problem now is that this setup works absolutely fine for the defined home-path ("/"), but whenever I call e.g. "/user/7" the API-Gatway returns 403, since it doesn't know the route, although it is defined in the Flask-Lambda.
Is there a possibility to setup API-Gateway in a way to pass the whole request through to the Lambda, regardless of whatever path the request has?

You can use the {proxy+} path to act as a catch all for API Gateway.
By creating the proxy resource anything that matches the prefix will automatically use that resource, if you add it to the root resource then it will process all other requests that do not match a specific URL pattern.
You could also use variables in your path resource names, for your user method for example the path would end up being /user/{userId}. This is the preferable solution as it is still being specific to the request type.
More information is available here.

Related

API Gateway URI Versioning

I keep seeing comments on how to do URI versioning in API Gateway, and these all say the same thing,
Do not create the version path (/v1) as a resource in your API. Instead, simply call you API "Names V1" and start creating the resources (/names). When you want to make a breaking change and create a new version of the API, we recommend you create an entirely new API called "Names V2". Once again, simply create your resources without the version path.
To bring the two APIs together, you can use custom domain names. A custom domain name in API Gateway includes both a fully qualified domain name and a base path. Create two custom domain names:
myapi.com/v1 -> points to the prod stage of the Names V1 API
myapi.com/v2 -> points to the prod stage of the Names V2 API
However, when you try to create a Custom Domain Name with a "/" in it, API Gateway responds with "Invalid Domain Name". So if you try to do it on the mapping, you get something similar mentioning the special characters you can use, and "/" is not one of them. So your only option is to use the Stage variables which these posts mention the challenges of doing it that way.
Additionally, if you just make it "v1" with no slash, then we are unable to have a custom domain like "api.whatever.com". Then makes the custom domain be specific to an API area that needs to be versioned. Ex. "stores.whatever.com". This causes each API to have their own subdomain.
URI-based Versioning for AWS API Gateway
API Versioning with AWS API Gateway
Sorry for asking a new question, but I'm not allowed to add a comment on the posts.
I'm not sure I understand the request here, let me try to clarify. When a request comes in to your custom domain name api.whatever.com, API Gateway needs to determine where to send the request. API Gateway will look at the path and then determine if there are any API:STAGE mappings for that path. You can configure an empty base path mapping on a custom domain name, but then all requests without a path to that custom domain name will be routed to the API:STAGE mapping. It seems like you're trying to route requests to either api.whatever.com or stores.whatever.com, you can do this with two custom domain names each with their own empty base path mapping. For example:
Custom domain name1: api.whatever.com
api-id: 12345
stage: Live
api-mapping-key: NULL
Custom domain name2: stores.whatever.com
api-id: 67890
stage: Beta
api-mapping-key: NULL
Your clients will have to specify the proper domain name when calling your APIs.

API Gateway Custom Domain - having both empty base path mapping to stages and a static html page on the empty base path

I have an AWS API Gateway set up with a custom domain name one the format api.example.com. I have a api.example.com/prod stage and a api.example.com/dev stage, but I would also like to be able to modify the response returned on api.example.com.
What I want is very similar to how the GitHub api behaves. Where https://api.github.com/v2 access version 2 of the api, and https://api.github.com/v3 access version 3, but https://api.github.com gives a custom response.
In my case I would like to have a small static html page on api.example.com, but a custom json response (like on api.github.com) is fine, anything more helpful than {"message":"Forbidden"} would do.
I know I set up base path mappings to the dev and prod stages in the custom domain window (as my partially redacted settings below), but I would like to add a empty path as well, and have that point to another API with just a GET method on the root resource that returns a small static html page. API Gateway does not allow combining an empty base path mapping with non-empty base path mappings (for a good reason I assume), but it feel there should be some way to achieve what I am trying to set up.
Anyone who knows a way to achieve this using AWS API Gateway?

How to create URL with path parameters inside API Gateway

I want to create a path as follows in the API Gateway:-
/users/{userId}/photos/{photoId}.
A clever guy may say why can't you go for /users/photos/{userId}/{photoId}. Well I can't club many path parameters under the same URL.
How can I create a precise URL path with API Gateway.
Refer to the section with the following heading in this link.
Integration 3: Create a GET Method with Path Parameters to Call the Lambda Function

AWS API GateWay can't have multiple paths?

I just got my custom domain name setup via AWS API Gateway, and now I have several domain names all routing to one lambda function. which just serves a webpage.
The setup looks like this:
And I have several of these with different domains that I want all to serve the same content.
Now, I'd like to add another path like /getdata or something which will just serve some data from a database instead of serving static HTML.
But when I try to add another path I get this error:
Error
Only one base path mapping is allowed if the base path is empty.
How can I have a single domain with multiple paths then?
I tried just using the wildcard path: *, and that works for multiple paths like /test or /getdata, but it doesn't work with just the domain name, and I can't tell every single user to make sure to type something in like /home everytime
Ahh I figured it out!
So, unfortunately, the page that shows you the custom domains is not where you need to be making your routes.
The correct procedure is to create an API (or use an existing API, and modify the resources) and give it a proxy resource, and a plain GET method that originates from the root path.
First, go to your API GateWay console, and create a new API.
Then once you give it a name, and choose the type (regional, or edge), it will show you the resources page
Here, you will do 2 things: Create a catch all proxy resource, and also a get resource to the root path.
Step 1: Make a catch all proxy resource:
Click Actions, and choose Create Resource
On the wizard, click Configure as proxy, and give it a name. Leave the resource path as it is. Then click create resource. The {proxy+} is notation that tells AWS that this resource should accept any path that has anything after the /. This means /test will work as well as /test/1/2/3/etc. However just / alone will not work!
Next, it will take you to this screen where you choose your integration type. We want lambda, which is the default. Make sure to select the correct region, for me the default was the right one. Then start typing in your lambda function name, and it should dynamically pop up a list of your lambda functions. select the one that you want for your application. If that doesn't work, you can copy and paste your lambda ARN from the lambda function console. Click Save.
Step 2: Make a get resource for the root path
Click the root slash at the top, right under where it says resources. Then click actions, and choose Create Method. It will pop up a little selection thing under the root slash, and there you should select GET, and then click the little checkmark.
Here, make sure to check Use Lambda Proxy Integration, and then the rest of this form should be the same as the last one you did. Just select your region, and your lambda function, and click Save.
Step 3: Deploy
Once that's done, go to actions, and choose Deploy API, give it a name for a stage, and some description, and then you are ready to attach this API to your custom domain.
Step 4: Attach
On the left tab scroll down to where it says Custom Domain Names, and create a new domain name (or attach it to an existing one if you have it)
Enter your domain name, and choose regional or edge. Then choose your certificate (there are many good guides for how to make a certificate through AWS)
Once you click save, it will look something like this:
Click Show Base Path Mappings, and then Edit.
In the path field just leave a slash, in the Destination field, choose from the dropdown the API that you just deployed. And on the right, select the stage that you made when you deployed your API.
Lastly, it will sit at initializing for a while, so while you wait for that, remember that you need to make a route53 record set for this domain, and map it to the cloudfront target URL that the API GateWay gave us. This target url looks like: www.u10dsa3s5iovdk.cloudfront.net. Copy that, and go to Route 53, Choose the hosted zone for your domain. Create a record set, and give it the same name as the domain you just created, so if you made www.example.com, in the name field of Route 53 you need to type in www. or if you made test.example.com you need to type in test. Then choose Alias: Yes, and for the Alias Target, paste in the cloudfront url from API GateWay.
When the custom domain name is done initializing, you can make calls to www.example.com as well as www.example.com/anything/else/you/want/to/put/here
Hope this helps anyone who was stuck as long as I was. Please let me know if there's anything I missed, or if something is inaccurate.
In this case you need to configure a path different to "/" for each api you need to serve through custom domain. AWS Api Gateway don't let you to serve several api into the same custom domain if you serve at least one api with no base path.

Lambda function custom domain

I have been messing around with AWS lambda today trying some things out. I am currently trying to trigger the function from a url in a browser.
The URL looks similar to this: https://abcdef.execute-api.eu-west-2.amazonaws.com/default/test
As I understand it I can assign a custom domain to my endpoint, but can I also get rid of the path part of the url, so for example:
GET: https://example.com/
GET: https://example.com/somefile.txt
POST: https://example.com/ ['some_post_field' => 'some data']
Will all be passed to my function, or do I need to configure an EC2 instance with NGINX to proxy-pass the requests to lambda?
Any thoughts would be useful.
There are now a couple different ways you can accomplish this in AWS:
The newest (arguably coolest!) is to use Cloudfront to run your code using their Lambda#Edge service. You can completely customize your URL path and have portions used as variables like any other REST endpoint. You attach your Lambda fn to "behaviour" endpoints which give you full URL control. Its fairly deep and beyond the scope of your question to explain it all here, but read through the docs at the link provided and you'll likely see lots of stuff you like.
Another older, more expensive but more documented method is to use AWS's API Gateway as you have eluded to in your question's tags. It has a great front end console and is easy to connect API endpoints to your Lambda backend logic by attaching them to REST methods. The console helps you "variable-ize" your URL with form field data. This service helps you the most with custom domains to trigger from. Setting up custom domains is a snap in API Gateway. Be sure to use AWS's SSL Certificate Manager for free SSL certs on your custom domain too!
How you specifically setup your endpoints depends on which service you choose. Personally, given your desire to serve up different types of content, I would lean towards CloudFront, and define a "behaviour" URL for your dynamic Lambda content. If the URL request does not match one of your defined behaviours, it defaults to the Cloudfront cache/origin to serve your static assets (somefile.txt). Only matches go to your attached Lambda fn with form data. Very slick!
A lot of example Lambda#Edge fn's are available here.
I have used both and have clients on both now. Lambda#Edge is ridiculously faster and less expensive, BUT is less documented, has a steeper learning curve, and console is not nearly as helpful. I would honestly try both to see which fits your situation and experience level best. Both will get the job done. EC2 is most definitely NOT needed (nor desired perhaps). Hope that helps — good luck!
Instead of directly exposing the Lambda function via URL, expose it through AWS API Gateway where you can define your own paths and map to a Domain.