aws lambda使用
APIs, short for Application Programming Interfaces, are connection points that allow different (parts of) computer programs to exchange data. Using APIs for the delivery of a software service makes your code better organized and easily reusable.
API是应用程序编程接口的缩写,是使不同(部分)计算机程序可以交换数据的连接点。 使用API交付软件服务可以使您的代码更好地组织并易于重用。
In this article, I am going through the basic steps of creating an API using AWS Lambda and API Gateway.
在本文中,我将介绍使用AWS Lambda和API Gateway创建API的基本步骤。
The example is a very short code example, that will create a random password, based on a given length, a number of capitals, and a number of numerical characters. The other characters will be small letters. You can make the service behind your API as large and advanced as you want, ranging from examples like selling data to predicting using advanced Artificial Intelligence models.
该示例是一个非常简短的代码示例,它将基于给定的长度,大写字母和许多数字字符创建一个随机密码。 其他字符将是小写字母。 您可以根据需要使API背后的服务既大又高级,范围从出售数据到使用高级人工智能模型进行预测等示例。
Let’s get started:
让我们开始吧:
1.创建一个lambda函数 (1. Create a lambda function)
On your AWS Management Console go to Lambda:
在您的AWS管理控制台上,转到Lambda:
![Image for post](https://miro.medium.com/max/9999/1*VL0tSNvhZDYaky3hHAer_Q.png)
Create a new function:
创建一个新函数:
![Image for post](https://miro.medium.com/max/9999/1*MoxonBPiNnHI7z0r73kd1w.png)
Choose “author from scratch” and give your function a name. In this case, the api will generate secure passwords.
选择“从头开始”,然后为函数命名。 在这种情况下,api将生成安全密码。
![Image for post](https://miro.medium.com/max/9999/1*oCRxszZjCqJrYfMLbnCMow.png)
2.编写lambda函数 (2. Write the lambda function)
In your newly created lambda function, go to Function Code:
在您新创建的lambda函数中,转到函数代码:
![Image for post](https://miro.medium.com/max/9999/1*SzD_YKtPNLGhXEe91KWv3A.png)
And write your function:
并编写您的函数:
![Image for post](https://miro.medium.com/max/9999/1*7M8LGRUwkeoofl7kyPtTnA.png)
The code I use is the following:
我使用的代码如下:
3.配置测试 (3. Configure a test)
Go to the test menu and click on configure tests.
转到测试菜单,然后单击配置测试。
![Image for post](https://miro.medium.com/max/9999/1*YL8BoT6aZDWwCJAMGLCJfA.png)
Click on create new test event and specify the input for your test in the dictionary before clicking create:
单击创建新的测试事件,并在单击创建之前在字典中指定测试的输入:
![Image for post](https://miro.medium.com/max/9999/1*jCaUOS216y4Ycn99r0V3dw.png)
Test your Lambda function by clicking on Test:
通过单击测试来测试您的Lambda函数:
![Image for post](https://miro.medium.com/max/9999/1*Ko9cySsFgeKOERrenJC8ag.png)
You see that it sent back a password in the ‘body’ of the dictionary (or the json).
您会看到它在字典(或json)的“正文”中发回了密码。
4.构建API网关 (4. Build the API Gateway)
Go to API Gateway through the AWS Management Console. Click on REST API.
通过AWS管理控制台转到API网关。 单击REST API。
![Image for post](https://miro.medium.com/max/9999/1*-cxCox-gyxtQQ8oDuUR3rw.png)
Click on New API and choose a name:
单击新API,然后选择一个名称:
![Image for post](https://miro.medium.com/max/9999/1*RYB9MKeUtZtnwCVNaN0yCQ.png)
Create a POST method and choose the integration type “Lambda Function”. Then select the name of your Lambda Function.
创建一个POST方法并选择集成类型“ Lambda Function”。 然后选择Lambda函数的名称。
![Image for post](https://miro.medium.com/max/9999/1*ySvngHchgm3l2VcA1vPjiQ.png)
You should now see this:
您现在应该看到以下内容:
![Image for post](https://miro.medium.com/max/9999/1*fsLwdOsVfUHu7CHLCxi18w.png)
As a final step, deploy your API:
最后一步,部署您的API:
![Image for post](https://miro.medium.com/max/9999/1*MDwCCuCRTfxHs1oNEobmPQ.png)
You can name it “prod” for example:
您可以将其命名为“ prod ”,例如:
![Image for post](https://miro.medium.com/max/9999/1*iHPoF6ZL63nkI7squmI-Ow.png)
The API Gateway should now tell you the URL to find your API:
API网关现在应该告诉您找到您的API的URL:
![Image for post](https://miro.medium.com/max/9999/1*vfnMI_4FdagUMztvP-u-rg.png)
5.测试您的API是否真实 (5. Test Your API for real)
You can now use a Python Notebook, or anything of your liking, to test out your API.
现在,您可以使用Python Notebook或任何您喜欢的东西来测试API。
If you copy-paste this in a notebook, the API will send you a new password!
如果您将此内容复制粘贴到笔记本中,API将向您发送一个新密码!
aws lambda使用