How to create invoke same lambda function for the two api-gateway

这一生的挚爱 提交于 2020-08-15 12:58:55

问题


I aws create a 2 api-gateway

  • First one

https://xx.xx-api.us-east-1.amazonaws.com/v1/uploadapi/?search=all

then my lambda function will invoke below

searchone = es.search(index="my-index", body={"query": {"match_all": {}}})
return searchone
  • Second One

https://xx.xx-api.us-east-1.amazonaws.com/v1/uploadapi/?search=matchphrase=name_computer

searchtwo = es.search(index="my-index", body={"query": {"match": {"name":"computer"}}})
return searchtwo

Basically need to create single lambda function

if api url is first one then return searchone if the api url is second one then return searchtwo

Disclaimer ? DO i need to create separate Lambda function for above two api's


回答1:


No, you do not need multiple lambda functions. Among the two url's that you have specified, what changes is only the query params.

...?search=all
...?search=matchphrase=name_computer

Search parameters are used for such conditions i.e. to notify the server if it needs to handle the request in some specific way. You can access them in the lambda function.

Before we move ahead, I would suggest you consider the following schema:

...?search=all --> ?search_type=match_all
...?search=matchphrase=name_computer -> ...?search_type=match_some&match_phrase=name_computer

Now, if you explore the event object that is passed to the lambda function, you will find these query parameters at event.requestContext.queryStringParameters. It would look something like this:

{
    <...>
    'resource': '/v1/uploadapi',
    'requestContext': {
    'queryStringParameters': {
        'search_type': 'match_some',
        'match_phrase': 'name_computer'
    },
    <...>
}

You can now use this to build logic on top of it.



来源:https://stackoverflow.com/questions/62799901/how-to-create-invoke-same-lambda-function-for-the-two-api-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!