Default path in AWS API gateway? - amazon-web-services

We have an Rest API config in AWS API gateway where we want to route to handle a default path
/
/api
/$default (default path here)
Note we are not looking for the greedy path {proxy+}
/
/api
/{proxy+}
the problem with the greedy path is no request will route to /api ...always the {proxy+}
will take precedence even though the request has /api (tried it out)
Any help from the community in pointing us in the right direction would be of great help.

You can use the $default route to catche requests that don't explicitly match other routes in your API.
When the $default route receives a request, API Gateway sends the full request path to the integration. For example, you can create an API with only a $default route and integrate it on the ANY method with the https://petstore-demo-endpoint.execute-api.com HTTP endpoint.
When you send a request to https://api-id.execute-api.us-east-2.amazonaws.com/store/checkout, API Gateway sends a request to https://petstore-demo-endpoint.execute-api.com/store/checkout.
You can read more about it here.

Related

How to redirect routes from cloudfront to an API Gateway that contains multiple services created by serverless framework?

I created an API Gateway using AWS CDK and I am using the serverless framework to create 3 different API's using the same API Gateway. My API Gateway structure looks like this:
/
/service1
/service1/{proxy}
/service2
/service2/{proxy}
/service3
/service3/{proxy}
I configured API Gateway as the origin of a cloudfront, redirecting requests coming from "domain/api" to API Gateway. The problem is that the path in API Gateway needs to match the selected path in cloudfront, so I would have to change it to something like:
/
/api/service1
/api/service1/{proxy}
/api/service2
/api/service2/{proxy}
/api/service3
/api/service3/{proxy}
This solution would be ok, but I can't share the "/api" path between multiple different services using the serverless framework. I would like the API Gateway rootPath to be "/api" and not "/", but I haven't been able to perform this configuration with the CDK. I wish I had something like this, is it possible?
/api
/service1
/service1/{proxy}
/service2
/service2/{proxy}
/service3
/service3/{proxy}
So that requests with /api were redirected by cloudfront to API Gateway, but could be defined as /service1 in the service, not /api/service1.

AWS API Gateway path based routing to private integrations

I am using AWS HTTP API Gateway to route requests to my integrations in the VPC.
I've added a custom domain and I want to route my requests to my integrations based on the paths in the following manner
Basically all the requests coming to the API gateway should be routed to different integrations based on the base paths but the integration should receive only the path after the base path. So all the requests coming to my.custom.domain/foo/<path1>/<path2>/<path3> should be routed to the integration Foo but the gateway should strip the base path i.e. /foo and forward the rest to the integration.
The functionality is same as below in nginx where nginx strips the foo from the request path and forwards the rest to the service
location /foo/ {
proxy_pass http://foo.service
}
I've tried adding custom domain and API mapping in the AWS API gateway but that doesn't work. My service still receives the whole path from the request and hence fails.
I am unable to find any documentation or mentions on the internet about this.
You can use routes and parameter mappings to achieve this.
Create 2 routes with 2 path mappings:
path: "/foo/{proxy+}" with parameter mapping: "/$request.path.proxy"
path: "/bar/{proxy+}" with parameter mapping: "/$request.path.proxy"
"proxy+" is a greedy path variable, so it will contain the path after /foo/ or /bar/.
You can use this variable with a parameter mapping to overwrite the path the backend will receive.
This is how it looks like in AWS CDK
const privateIntegration = new HttpServiceDiscoveryIntegration('test', service, {
parameterMapping: new ParameterMapping()
.overwritePath(MappingValue.custom('/$request.path.proxy'))
})

AWS S3 Route Rewrite

I would like to setup my S3 bucket to rewrite the path of the route /api to a API Gateway URL.
So take the following:
User goes to website bacon.com
Website goes from Cloudflare DNS to S3 bucket
Website is a react app
React app makes a call to api which is bacon.com/api
S3 rewrites /api path to api gateway url https://ljsdflkjlsdk.execute-api.us-east-1.amazonaws.com/dev
I was able to get this to work here:
however, it forwards the URL to that API gateway URL which is not desired.
I'm not entirely sure this is possible. I'm thinking it might require either me implementing something like Route53 or writing a cloudflare worker to handle the rewrite?
Thanks in advance for your help!
So you want to proxy requests to the /api path to your backend API, while other routes, e.g. /login are served by the React app.
I may be wrong but I think neither S3 (as a static hosting provider) nor Route 53 (as a DNS tool) can help with that.
Since you mentioned you're already using Cloudflare, you can do this with CF Workers with little effort though!
Make sure bacon.com is proxied by CF (orange cloud ON on DNS tab)
Create a new worker with this code:
const backendUrl = "https://ljsdflkjlsdk.execute-api.us-east-1.amazonaws.com/dev"
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* #param {Request} request
*/
async function handleRequest(request) {
return fetch(backendUrl, request)
}
Associate the worker with the /api path on the Workers tab for the bacon.com
bacon.com/api is now a proxy sending requests to the backend url!

How to configure $default path in AWS API gateway?

We are trying to leverage the $default path in AWS API gateway as per https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-routes.html
configured api gateway like this leveraging the $default as one of the routes
/
/-default
ANY
/api
/{proxy=}
when we are trying to invoke the api gateway on the $default path and GET call
https://apigateway.amazonaws.com/prod/test
we assumed it will invoke the default path but it didn't
message: "Missing Authentication Token"
but when we do
https://apigateway.amazonaws.com/prod/api/test
the api integration is invoked
Note : we already tried configuring greedy path{proxy+} instead of $default that does not work as the greedy path always takes precedence and /api routes also get routed to greedy path
Any help from the community in pointing us in the right direction would be of great help
It seems like you have not set up your API Gateway HTTP API routes correctly causing the routing to not work as expected. Would also like to mention that HTTP APIs and REST APIs are different types of API Gateway APIs, so do confirm that you have configured your API correctly.
Coming to how the routing would work, as a sample, here's how the routes for the API look:
Request to GET https://xxxx.execute-api.xxxx.amazonaws.com/prod/test : Routed to $default path
Request to GET https://xxxx.execute-api.xxxx.amazonaws.com/prod/api/test : Routed to /api/{proxy+} path
Further, if you have a greedy path at ANY /{proxy+}, then as you mentioned, this greedy path will take priority over the $default route. However, this would not take precedence over the ANY /api route if the request matches to the route, for example: GET https://xxxx.execute-api.xxxx.amazonaws.com/prod/api : would be routed to /api path and not /{proxy+}
The routing priority is also explained here
After selecting a stage, API Gateway selects a route. API Gateway selects the route with the most-specific match, using the following priorities:
Full match for a route and method.
Match for a route and method with a greedy path variable ({proxy+}).
The $default route.
If no routes match a request, API Gateway returns {"message":"Not Found"} to the client.
EDIT:
To create the $default route, just specify the path as $default when creating the route

Redirect http:// requests to https:// on AWS API Gateway (using Custom Domains)

I'm using AWS API Gateway with a custom domain. When I try to access https://www.mydomain.com it works perfectly, but when i try http://www.mydomain.com it can't connect.
Is there a way to redirect the http -> https with the custom domain in API Gateway? If not, is there a way to get the http:// links to work just like the https:// links?
API Gateway doesn't directly support http without TLS, presumably as a security feature, as well as for some practical considerations.
There is not a particularly good way to do this for APIs in general, because redirection of a POST request from HTTP to HTTPS is actually a little bit pointless -- the data is has already been sent insecurely by the time the redirect is generated, unless the client has asked the server to inspect the request headers before the body is sent, with Expect: 100-continue.
You can create a CloudFront distribution, and configure it to redirect GET and HEAD requests from HTTP to HTTPS... but if you send a POST request to such a distribution, CloudFront doesn't redirect -- it just throws an error, since (as noted) such a redirection would be more harmful than helpful.
However... if GET is your application, then it's pretty straightforward: first, deploy your API with a Regional (not Edge-Optimized) API endpoint with a system-assigned hostname, not a custom domain.
Then, create a CloudFront distribution that uses this regional API endpoint as its origin server, and configure the CloudFront distribution's behavior to redirect HTTP to HTTPS. Associate your custom domain name with the CloudFront distribution, rather than with API Gateway directly.