aws lambda使用_如何使用AWS Lambda和API网关在AWS中构建API

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

Create a new function:

创建一个新函数:

Image for post

Choose “author from scratch” and give your function a name. In this case, the api will generate secure passwords.

选择“从头开始”,然后为函数命名。 在这种情况下,api将生成安全密码。

Image for post

2.编写lambda函数 (2. Write the lambda function)

In your newly created lambda function, go to Function Code:

在您新创建的lambda函数中,转到函数代码:

Image for post

And write your function:

并编写您的函数:

Image for post

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

Click on create new test event and specify the input for your test in the dictionary before clicking create:

单击创建新的测试事件,并在单击创建之前在字典中指定测试的输入:

Image for post

Test your Lambda function by clicking on Test:

通过单击测试来测试您的Lambda函数:

Image for post

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

Click on New API and choose a name:

单击新API,然后选择一个名称:

Image for post

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

You should now see this:

您现在应该看到以下内容:

Image for post

As a final step, deploy your API:

最后一步,部署您的API:

Image for post

You can name it “prod” for example:

您可以将其命名为“ prod ”,例如:

Image for post

The API Gateway should now tell you the URL to find your API:

API网关现在应该告诉您找到您的API的URL:

Image for post

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将向您发送一个新密码!

翻译自: https://towardsdatascience.com/how-to-build-an-api-in-aws-using-aws-lambda-and-api-gateway-22a8dee7c8e1

aws lambda使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值