POST JSON对象到aws lambda [英] POST JSON object to aws lambda

查看:1017
本文介绍了POST JSON对象到aws lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过aws API网关将json对象发布到aws lambda函数?

How can I post a json object to aws lambda function through aws API gateway ?

ps-我的目标是在python中编写lambda函数然后发布它到了SQS。

p.s.- My goal is to write the lambda function in python and then post it to aws SQS.

提前致谢。

推荐答案

我想过出来。现在,我有一个API Gateway acceptiong客户端发布了指定格式的JSON数据,然后将其传递给AWS-Lambda函数,该函数将数据转储到AWS-SQS中。这些步骤将在下面详细说明 -

I figured it out. Now I have a API Gateway acceptiong client posted JSON data of a specified format and then passing it to a AWS-Lambda function which, dumps the data into a AWS-SQS. The steps are explained below in details-

第1步 -

创建一个任何支持的语言中的lambda函数(我使用过Python 3.6)。下面是一个示例代码。

Create a lambda function in any supported languages (I have used Python 3.6). Here is a sample code.

import boto3  
import json

def lambda_handler(event, context):

    sqs = boto3.resource('sqs')

    queue = sqs.get_queue_by_name(QueueName='userData')

    response = queue.send_message(MessageBody=json.dumps(event))

    return {
                "status":"0",
                "message":"",
                "pubId":event["ClientID"],
                "routetitle":event["routeTitle"]
            }

注意:我已经导入了aws上下文中可用的json和boto3库,无需再添加任何文件。另请注意,除了名称之外,我没有指定SQS的任何详细信息,因为我的Lambda函数和SQS都位于同一AWS区域。我将整个事件变量转储到SQS,因为它只包含发布的JSON数据。

Note: I have imported both json and boto3 library which are available in aws context no need to add any more file. Also see that I have not specified any details for SQS other than the name because both of my Lambda function and SQS are in same AWS region. I am dumping the whole "event" variable to SQS as this only contains the posted JSON data.

第2步 -

现在在AWS控制台中转到API Gateway,然后创建一个新的API Gateway,然后在资源下创建一个POST操作。

Now in the AWS console goto "API Gateway" and then create a new API Gateway and then create a "POST" action under resources.

请查看截图

现在,在帖子操作下,点击整合请求。现在添加一个正文模板,就像下面给出的例子一样 -

Now, under the post action, click on "Integration request". Now add a body template to it like the example given below-

{
  "userMobile" : "$input.params('userMobile')",
  "ClientID" : "$input.params('ClientID')",
  "routeTitle" : "$input.params('routeTitle')"
}

另外,请确保您的API的集成类型为Lambda我们在STEP-1中创建的Lambda函数连接到API。

Also, make sure that you have the "Integration Type" of your API as "Lambda" and the Lambda function we created in STEP-1 is connected to the API.

现在,我们差不多完成了。现在,我们所要做的就是为我们创建的API创建一个舞台并部署API。 ***

Now, we are almost done. now all we have to do is create a stage for the API that we have created and deploy the API. ***

请注意部署后API的HTTP URL。

Please note the HTTP URL of the API after deployment.

第3步

现在转到简单排队服务(SQS),然后创建一个简单的SQS并保留所有默认参数。确保队列名称与您在Lambda函数中提供的名称相匹配,并且您的Lambda函数和SQS都位于同一AWS区域。

Now go to the "Simple Queuing Service (SQS)" and then create a simple SQS with keeping all the default parameters. Make sure the queue name is matching with the one you have provided in your Lambda function and both your Lambda function and your SQS are in same AWS region.

现在,您可以将POST JSON数据以相同的格式发送到您的API,您的Lambda函数会将其转储到SQS队列,您可以在那里查看数据。

Now, you can POST JSON data in the same format to your API and your Lambda function will dump it to the SQS queue, where you can go and view the data.

您还可以测试API使用Fidler等工具。

You can also test the API using tools like Fidler.

***确保每次更改API时重新部署API。

*** make sure to redeploy the API for every time you make a change to it.

这篇关于POST JSON对象到aws lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆