aws云服务器怎么使用

Contrary to its name, serverless architecture does have servers. However, developers do not need to know anything about them — managing and provisioning servers are the responsibilities of the serverless platform host (in this case, AWS). Developers only need to handle writing code and building their application. Another benefit of serverless is that developers will only be charged for the resources they use.

与它的名字相反,无服务器架构确实具有服务器。 但是,开发人员无需了解任何信息-管理和配置服务器是无服务器平台主机(在本例中为AWS)的职责。 开发人员只需要处理编写代码和构建他们的应用程序。 无服务器的另一个好处是,开发人员将只为他们使用的资源付费。

In this article, we will be using the following three services from AWS to implement a serverless back-end: Amazon API Gateway, AWS Lambda, and Amazon DynamoDB.

在本文中,我们将使用AWS的以下三种服务来实现无服务器后端: Amazon API Gateway , AWS Lambda和Amazon DynamoDB 。

When a client makes a HTTP request to a public API endpoint provided by API Gateway, a Lambda function will be invoked. The Lambda function manipulates the database provided by DynamoDB and then sends the response back to the client.

当客户端向API网关提供的公共API端点发出HTTP请求时,将调用Lambda函数。 Lambda函数处理DynamoDB提供的数据库,然后将响应发送回客户端。

Want to read this story later? Save it in Journal.

想稍后再读这个故事吗? 将其保存在 日记本中 。

(Prerequisites)

  • AWS account with IAM (Identity and Access Management) user set up
  • Node.js

We’ll be building a back-end that will handle requests from the client to get information from a database of ramen restaurants in New York City.

我们将构建一个后端,该后端将处理来自客户端的请求,以从纽约市的拉面餐厅数据库中获取信息。

We will write a Lambda function that will be invoked every time a user requests a restaurant. The function will get the restaurant from the DynamoDB table and respond to the front-end of the application with the details of the specified restaurant. The Lambda function will be invoked from the browser via API Gateway.

我们将编写一个Lambda函数,该函数将在每次用户请求餐馆时调用。 该功能将从DynamoDB表获取餐厅,并使用指定餐厅的详细信息响应应用程序的前端。 Lambda函数将通过API网关从浏览器中调用。

(Creating a DynamoDB Table)

Before we can implement the Lambda function, we will need to create a table in DynamoDB. In your AWS console, navigate to DynamoDB and click the Create table button to begin.

在实现Lambda函数之前,我们需要在DynamoDB中创建一个表。 在您的AWS控制台中,导航到DynamoDB并单击Create table按钮开始。

1. Let’s name the table “RamenRestaurants”.

1.让我们将表命名为“ RamenRestaurants ”。

2. Give the table a primary key (or partition key) of “RestaurantId” with a type of “String”. The primary key is a unique identifier for each item in the table. It is used for querying and accessing the data in the table. We will be querying the table via the RestaurantId later on when we write the lambda function.

2.为表赋予主键(或分区键)“ RestaurantId ”,类型为“字符串”。 主键是表中每个项目的唯一标识符。 它用于查询和访问表中的数据。 我们稍后将在编写lambda函数时通过RestaurantId查询表。

3. Use default settings should already be checked off by default.

3. 默认情况下,应已选中“ 使用默认设置” 。

4. Hit Create.

4.点击创建 。

亚马逊云创建MySQL 亚马逊怎么用云服务器_AWS

5. Congrats! You’ve just created your first DynamoDB table. The next screen will show you an overview of your new table.

5.恭喜! 您刚刚创建了第一个DynamoDB表。 下一个屏幕将向您显示新表的概述。

6. To add items to your new table, click on the Items tab and then hit the Create item button. Provide ‘1’ as the RestaurantId. To add attributes to the item, hit the ‘+’ button and choose Append and choose a type accordingly. I am adding the following attributes and information as my first item but feel free to create yours however you’d like.

6.要将项目添加到新表中,请单击“ 项目”选项卡,然后单击“ 创建项目”按钮。 提供“ 1”作为RestaurantId。 要将属性添加到项目,请点击“ +”按钮,然后选择“ 添加”并相应地选择一种类型。 我将以下属性和信息添加为我的第一项,但是可以根据需要随意创建。

亚马逊云创建MySQL 亚马逊怎么用云服务器_java_02

7. Let’s add a few more restaurants into our database.

7.让我们在数据库中添加更多的餐馆。

亚马逊云创建MySQL 亚马逊怎么用云服务器_亚马逊云创建MySQL_03

(Creating a Lambda Function)

Now that we have our DynamoDB table, we can create a Lambda function to handle an HTTP request. Navigate to the AWS Lambda console.

现在我们有了DynamoDB表,我们可以创建一个Lambda函数来处理HTTP请求。 导航到AWS Lambda控制台。

1. Click the Create function button,

1.单击创建功能按钮,

2. The Author from scratch option is the default choice. We will keep that option selected.

2.“ 从头开始创建”选项是默认选项。 我们将选择该选项。

3. We’ll name the function ‘ramen_read’ and select Node.js for the Runtime language. (AWS Lambda supports other runtimes but for this article, we will be be using Node.)

3.我们将函数命名为“ ramen_read”,然后选择Node.js作为运行时语言。 (AWS Lambda支持其他运行时,但在本文中,我们将使用Node。)

4. Next, click on the Create function button and on the next screen, you’ll see the details of your new lambda function.

4.接下来,单击“ 创建功能”按钮,然后在下一个屏幕上,您将看到新的lambda函数的详细信息。

5. If you scroll down a bit, you’ll see the Function code section, with some sample code. You can write functions right in the console editor, which works well for simple functions. Before we get into actually writing it, let’s go over some syntax of a typical lambda function.

5.如果向下滚动一点,您将看到“ 功能代码”部分以及一些示例代码。 您可以直接在控制台编辑器中编写函数,该函数非常适合简单的函数。 在开始实际编写它之前,让我们看一下典型的lambda函数的一些语法。

Every lambda function has a handler, which is a method that processes events.

每个lambda函数都有一个处理程序,该处理程序是一种处理事件的方法。

亚马逊云创建MySQL 亚马逊怎么用云服务器_java_04

As shown above, the handler method takes in three arguments: event, context, callback. The event object contains information from the invoker, which passes on information in the form of a JSON-formatted string. The context object contains information about the function itself, the invocation request, and its runtime environment. The callback argument is a function that takes in two arguments: an error, and a response. The callback function returns either the error or the response to the invoker.

如上所示,处理程序方法采用三个参数: event , context , callback 。 event对象包含来自调用程序的信息,该调用程序以JSON格式的字符串形式传递信息。 context对象包含有关函数本身,调用请求及其运行时环境的信息。 callback参数是一个带有两个参数的函数:错误和响应。 回调函数将错误或响应返回给调用者。

6. Now, let’s start writing our “ramen_read” function! As mentioned before, you can write functions directly in the AWS Lambda console code editor. However, we will be writing our function in locally. Feel free to use your favorite IDE. This allows us to be able to create a git repository for our work.

6.现在,让我们开始编写我们的“ ramen_read”函数! 如前所述,您可以直接在AWS Lambda控制台代码编辑器中编写函数。 但是,我们将在本地编写函数。 随意使用您喜欢的IDE。 这使我们能够为我们的工作创建一个git存储库。

In your project folder, let’s create a new directory called “ramen_read.” Within that directory, run npm init and create a package.json file.

在您的项目文件夹中,创建一个名为“ ramen_read”的新目录。 在该目录中,运行npm init并创建一个package.json文件。

Create a new file called index.js and paste the below code within that file. Don’t forget to npm install aws-sdk!

创建一个名为index.js的新文件,并将以下代码粘贴到该文件中。 不要忘记npm install aws-sdk !

To access the DynamoDB table, we are using the AWS Document Client. Theparamsobject is a JSON object containing the parameters needed to query the table. Then, we call the query method on the document client.

要访问DynamoDB表,我们正在使用AWS Document Client。 params对象是一个JSON对象,其中包含查询表所需的参数。 然后,我们在文档客户端上调用query方法。

Next, compress all the contents of the “ramen_read” folder into a zip file. On the Function code section of the AWS Lambda console, there is an Actions button on the top-right corner. Click on it and select Upload a .zip file to upload the zip file to AWS.

接下来,将“ ramen_read”文件夹的所有内容压缩到一个zip文件中。 在AWS Lambda控制台的“ 功能代码”部分,右上角有一个“ 操作”按钮。 单击它,然后选择上载.zip文件以将zip文件上载到AWS。

亚马逊云创建MySQL 亚马逊怎么用云服务器_AWS_05

Note: Another way to upload your zip file is by using AWS CLI. This would involve installing AWS-CLI, configuring user permissions, and running a few commands to upload the zip file from your terminal.

注意:另一种上传zip文件的方法是使用AWS CLI。 这将涉及安装AWS-CLI,配置用户权限以及运行一些命令以从终端上载zip文件。

7. Before we can test the lambda in the console, we need to configure permissions. Otherwise, if you try to invoke the function, you will receive an Invoke Error: ”errorType”: “AccessDeniedException”.

7.在控制台中测试lambda之前,我们需要配置权限。 否则,如果您尝试调用该函数,则会收到一个Invoke Error: ”errorType”: “AccessDeniedException” 。

On the lambda function console, click on Permissions, and then click on the Role name.

在lambda功能控制台上,单击“ 权限” ,然后单击“ 角色名称” 。

亚马逊云创建MySQL 亚马逊怎么用云服务器_亚马逊云创建MySQL_06

A new tab will open to the IAM console and show you a summary of the role and permissions policies attached to the “ramen_read” lambda. Click on the blue Attach policies button. On the next page, search “DynamoDB” and select the AmazonDynamoDBReadOnlyAccess policy. For our example, we only need read only access. However, for other functions you may write in the future, you may need full access. Choose the one that best matches your needs. Click on the Attach policy button.

一个新选项卡将打开到IAM控制台,并向您显示“ ramen_read” lambda附带的角色和权限策略的摘要。 单击蓝色的附加策略按钮。 在下一页上,搜索“ DynamoDB”,然后选择AmazonDynamoDBReadOnlyAccess策略。 对于我们的示例,我们仅需要只读访问权限。 但是,对于将来可能要编写的其他功能,可能需要完全访问权限。 选择最符合您需求的产品。 单击附加策略按钮。

亚马逊云创建MySQL 亚马逊怎么用云服务器_亚马逊云创建MySQL_07

8. Now we can test our lambda in the console! Go back to the AWS Lambda console for the “ramen_read” function. Click on the Test button at the top of the page.

8.现在我们可以在控制台中测试lambda了! 返回AWS Lambda控制台以使用“ ramen_read”功能。 单击页面顶部的“ 测试”按钮。

亚马逊云创建MySQL 亚马逊怎么用云服务器_API_08

A window will pop up to configure test events. We will provide a RestaurantId of 1 in JSON format. Name your event and click Create.

将弹出一个窗口来配置测试事件。 我们将以JSON格式提供1的RestaurantId。 为您的活动命名,然后单击创建。

亚马逊云创建MySQL 亚马逊怎么用云服务器_API_09

Click on the Test button again and you should see a successful execution result on the console, along with the response returned by the function execution. In this case, it is the information we provided in our database for Nakamura, which has a restaurant id of 1.

再次单击“ 测试”按钮,您应该在控制台上看到成功的执行结果,以及函数执行返回的响应。 在这种情况下,这就是我们在数据库中为Nakamura提供的信息,该餐馆的ID为1。

亚马逊云创建MySQL 亚马逊怎么用云服务器_亚马逊云创建MySQL_10

(Creating a REST API with API Gateway)

Now that we have a lambda function, we will use API Gateway to get a public API endpoint that that will trigger the function. Navigate to the API Gateway console.

现在我们有了lambda函数,我们将使用API网关来获取将触发该函数的公共API端点。 导航到API Gateway控制台。

1. Click on Create API.

1.单击创建API 。

2. Choose REST API on the next screen and click Build.

2.在下一个屏幕上选择REST API ,然后单击Build 。

3. We will name the API “RamenRestaurantsAPI” as well and Create API.

3.我们也将API命名为“ RamenRestaurantsAPI”并创建API 。

亚马逊云创建MySQL 亚马逊怎么用云服务器_API_11

4. On the next screen, click on Action and select Create Resource. The Resource Name and Resource Path will be “restaurant.”

4.在下一个屏幕上,单击操作,然后选择创建资源 。 资源名称和资源路径将为“餐厅”。

亚马逊云创建MySQL 亚马逊怎么用云服务器_AWS_12

亚马逊云创建MySQL 亚马逊怎么用云服务器_亚马逊云创建MySQL_13

5. Back on the RamenRestaurantsAPI screen, click the /restaurant resource you just made and then click Actions again and Create Resource. This time, the Resource Name will “id” and the Resource Path is “{id}”. This is how we will pass in the id of the restaurant in our query; for example, a GET request to /restaurant/id.

5.返回RamenRestaurantsAPI屏幕,单击您刚刚创建的/ restaurant资源,然后再次单击Actions和Create Resource。 这次, 资源名称将为“ id”, 资源路径为“ {id}”。 这就是我们在查询中传递餐厅ID的方式; 例如,对/ restaurant / id的GET请求。

亚马逊云创建MySQL 亚马逊怎么用云服务器_python_14

6. Now we will Create Method. Select GET as your method and hit the checkmark.

6.现在,我们将创建方法 。 选择GET作为您的方法,然后打勾。

亚马逊云创建MySQL 亚马逊怎么用云服务器_AWS_15

亚马逊云创建MySQL 亚马逊怎么用云服务器_亚马逊云创建MySQL_16

7. The Setup for the method will pop up on the right.

7.方法的设置将在右侧弹出。

The Integration type is Lambda Function.

积分类型为Lambda函数。

Choose your Lambda Region. You can find it in the navigation bar in the top-right corner next to Support. In my case, my region is “us-east-1” and in the navigation bar, it says N. Virginia. The region is just a geographic location where Amazon has data centers. Usually, your region is the one you are physically located closest to.

选择您的Lambda地区 。 您可以在“ 支持”旁边右上角的导航栏中找到它。 以我为例,我所在的地区是“ us-east-1”,在导航栏中显示为N. Virginia。 该地区只是亚马逊拥有数据中心的地理位置。 通常,您所在的地区是您物理上最靠近的地区。

Lastly, fill in the name of the Lambda Function and click Save.

最后,填写Lambda函数的名称,然后单击保存 。

亚马逊云创建MySQL 亚马逊怎么用云服务器_AWS_17

8. Click OK when the next window pops up confirming permissions.

8.在弹出下一个窗口确认权限时,单击“ 确定” 。

9. Now you will see the /restaurant — GET — Method Execution. We will need to create a mapping template for the Integration Request.

9.现在,您将看到/ restaurant — GET —方法执行 。 我们将需要为Integration Request创建一个映射模板。

亚马逊云创建MySQL 亚马逊怎么用云服务器_python_18

Mapping templates are how API Gateway transforms a request before it is sent to the back-end. After you click on Integration Request, you will see the last option on the screen for Mapping Templates.

映射模板是API Gateway在将请求发送到后端之前如何对其进行转换。 单击“ 集成请求”后 ,您将在“ 映射模板 ”屏幕上看到最后一个选项。

Select “When there are no templates defined” for Request body passthrough. Click Add mapping template and input “application/json” in Content-Type.

为请求正文传递选择“当没有定义模板时”。 单击添加映射模板,然后在Content-Type中输入“ application / json”。

Underneath that box, a text editor will appear. The JSON for the mapping template is: {“RestaurantId”: “$input.params(‘id’)”}

在该框下面,将显示一个文本编辑器。 映射模板的JSON是: {“RestaurantId”: “$input.params('id')”}

Don’t forget to hit Save!

别忘了点击保存 !

亚马逊云创建MySQL 亚马逊怎么用云服务器_AWS_19

10. Go back to the Actions button at the top of the page and select Deploy API. On the window that pops up, create and name a deployment stage. We will just call it “prod” and then hit Deploy.

10.返回页面顶部的“ 操作”按钮,然后选择“ 部署API” 。 在弹出的窗口中,创建并命名部署阶段。 我们将其称为“ prod”,然后单击Deploy 。

11. After you hit deploy, you will be directed to the Stages page. Under the stages on the left-hand side, select the GET method that we created.

11.单击“部署”后,您将被定向到“ 阶段”页面。 在左侧的阶段下,选择我们创建的GET方法。

The invoke URL for the method is at the top of the screen.

该方法的调用URL在屏幕顶部。

亚马逊云创建MySQL 亚马逊怎么用云服务器_python_20

12. Input the invoke URL into your browser address bar and replace {id} with “1.” You will see the below response!

12.在浏览器地址栏中输入调用URL,并将{id}替换为“ 1”。 您将看到以下响应!

亚马逊云创建MySQL 亚马逊怎么用云服务器_java_21

Great! Your back-end is all set!

大! 您的后端已准备就绪!

Follow these steps to create other lambda functions, or new methods within your API as needed for your application. Make sure to re-deploy the API whenever you make changes.

请按照以下步骤在应用程序中根据需要在API中创建其他lambda函数或新方法。 进行更改后,请确保重新部署API。

📝 Save this story in Journal.

story将这个故事保存在Journal中 。

👩💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.

every💻每个星期天的早晨,您都可以在收件箱中等待本周最受关注的Tech故事。 阅读Tech Newsletter中的“值得注意” 。

翻译自: https://blog.usejournal.com/building-a-serverless-back-end-with-aws-5bb3642a3f4

aws云服务器怎么使用