1

I'm trying to build an API with AWS API Gateway and AWS Lambda that has one resource, but multiple actions that can be taken on that resource via POST, similar to how Google has built its Natural Language API, e.g:

POST https://language.googleapis.com/v1/documents:analyzeEntities
POST https://language.googleapis.com/v1/documents:analyzeEntitySentiment
POST https://language.googleapis.com/v1/documents:analyzeSentiment
...

I want to link each POST method to a different Lambda function. I've looked through the developer documentation but haven't found anything that addresses this type of use case. Does anyone know if AWS supports this and how to go about implementing it?

KeepingItClassy
  • 161
  • 1
  • 13

1 Answers1

1

It depends on how important it is for you to follow the scheme you describe in your question. The key problem is that API Gateway doesn't let you use a colon : in your resource name.


If it is not important to follow the scheme

Consider making your API scheme more RESTful.

Perhaps instead of performing an analyzeEntities action on documents, consider that your requests to analyze the documents are themselves resources which can be created...

e.g:

POST /documents/analyzeRequest[s]
(where body describes the type of request)

or

POST /documents/analyzeEntitiesRequest[s]

These would allow you to add GETs later on to list previous/current requests


If it is important to follow the scheme

So, specifying a : in your resource path results in the error:

Resource's path part only allow a-zA-Z0-9._- and curly braces at the beginning and the end.

The workaround to this is to create a new resource and check the Configure as proxy resource box. Creating a proxy resource allows you to defer routing decision making to a lambda function, for any path which matches the rule.

e.g. ANY /{proxy+} would match all requests.

Note: If you then added a separate resource GET /foo, your proxy resource would not handle GET /foo, since a more specific rule now exists.

So, with a proxy resource set up, you would need to write a lambda that invokes the appropriate lambda function, based on your routing rules.

Nicholas Sizer
  • 3,300
  • 3
  • 26
  • 29
  • Great answer, thank you! I've thought about the first solution, but that doesn't seem to be compliant with REST best practices since it uses verbs for resources. I'll try the proxy resource solution you outlined. – KeepingItClassy Apr 04 '18 at 00:51
  • 1
    In the first solution, "analyzeRequest" is a noun. It's a request to analyze. You could also think of it as adding a task to a processing queue. The server will get around to processing it, and then you can later GET that task by ID to determine its status/results. – Nicholas Sizer Apr 04 '18 at 01:02
  • 1
    Another option, if you read through this answer: https://stackoverflow.com/a/19650279/242311 , another option is to post your document[s] to something like /documents-being-analyzed. Just another way to conceptualise it. – Nicholas Sizer Apr 04 '18 at 01:12
  • @NicholasSizer could you help me with this use case https://stackoverflow.com/questions/62871941/can-we-have-multiple-http-method-in-single-resource-in-api-gateway-in-aws/62872076#62872076 – sumanth shetty Jul 14 '20 at 09:07