0 0
Read Time:3 Minute, 43 Second

Develop microservices using the AWS CLI, Lambda Functions

AWS CLI Lambda Functions

   延续著 使用 AWS Console, AWS Lambda Functions 开发微服务 这篇文章, 我们将继续的探讨由 AWS CLI, Lambda Functions 来开发微服务。

   Continuing with the article Using the AWS Console, Lambda Functions develops microservices” we will continue to explore the development of microservices by the AWS CLI, Lambda Functions.

  • 微服务; BookMuseum (Microservices;  BookMuseum)

BookMuseum, 主要是当用户提供了所想要去参观的博物馆与参观的日期, BookMuseum 便会调用 https://xxx.execute-api.ap-northeast-1.amazonaws.com/Prod/museum 为用户完成参观博物馆的预订。

BookMuseum, mainly when the user provides the museum they want to visit and the date of the visit,  BookMuseum will call https://xxx.execute-api.ap-northeast-1.amazonaws.com/Prod/museum ; the user completes the reservation to visit the museum.

用户提供的信息以 JSON 的格式提交给 BookMuseum。如下的例子:

The user-supplied information submits to BookMuseumin JSON format. 

The following example :

{
  "buyer_id": "mariano",
  "museum_name": "tate gallery",
  "when":"2020-03-14"
}

BookMuseum 将回传 JSON 格式的信息; 确定用户已完成参观博物馆的预订。如下的例子:

BookMuseum will pass back information in JSON format; Make sure that the user has completed a reservation to visit the museum. The following example :

{
  "when":"2020-03-14",
  "reservation_id":"CCEBA",
  "name":"tate gallery"
}

BookMuseum 的 AWS Lambda Function 的代码如下:

The code for the Lambda Function of BookMuseum is as follows:

exports.handler = async (env) => {
    // hotel's booking process
    try {
        let response = await fetch('https://xxx.execute-api.ap-northeast-1.amazonaws.com/Prod/museum', {
            method: 'POST',
            body: JSON.stringify(env),
            headers: {'Content-Type': 'application/json'}
        })
        let body = await checkResponseStatus(response);
        return await body.json();
    } catch (err) {
        console.error(err.message);
    }
}

开发完成后, 将我们所开发的代码与第三方的库, 压缩成一个 zip 文件, 然后, 按照以下的步骤, 就能完成以AWS CLI 的方式部署、调用 BookMuseum 的 AWS Lambda Function。

After development is complete, we will compress the code we developed and the third-party library into a zip file. Then, following the steps below, we can complete the deployment and call BookMuseum’s Lambda Function in the AWS CLI.

  • 创建 IAM 的角色 (Create a role for IAM)

Trust-policy.json 中的 “Service”: “lambda.amazonaws.com” , 给予了AWS Lambda function 足够的权限能调用 AWS Security Token Service AssumeRole action, 而使得 AWS Lambda Function可以使用 IAM 的角色中所拥有的权限。

“Service”: “lambda.amazonaws.com” in Trust-policy.json, giving AWS Lambda function enough permissions to invoke THE AWS Security Token Service  AssumeRole action, which makes the AWS Lambda Function available to IAM Permissions that own in the role.

Trust-policy.json

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

创建 BookMuseumLambdaRole 角色

Create the BookMuseumLambdaRole role

aws iam create-role --role-name BookMuseumLambdaRole --assume-role-policy-document file://trust-policy.json
  • 赋予 IAM 角色的权限 (Permissions to give the IAM role)

赋予角色 BookMuseumLambdaRole 拥有 AWSLambdaBasicExecutionRole 的权限。

Give the role; BookMuseumLambdaRole; permission to AWSLambdaBasicExecutionRole.

aws iam attach-role-policy --role-name BookMuseumLambdaRole --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

AWSLambdaBasicExecutionRole 使得角色 BookMuseumLambdaRole 可以将日志写至 AWS CloudWatch。

AWSLambdaBasicExecutionRole enables the role; BookMuseumLambdaRole; to write logs to AWS CloudWatch.

  • 部署 zip 文件、创建 AWS Lambda function (Deploy the zip file and create the AWS Lambda function)

创建 AWS Lambda Function; BookMuseum; 并且使得 AWS Lambda Function; BookMuseum; 可以使用 IAM 角色; BookMuseumLambdaRole; 所拥有的权限。

Create an AWS Lambda Function; BookMuseum;  and makes the AWS Lambda Function; BookMuseum;  can use IAM roles; BookMuseumLambdaRole; permissions has.

aws lambda create-function --function-name BookMuseum --zip-file fileb://book-museum.zip --handler index.handler --runtime nodejs14.x --role arn:aws:iam::403000007300:role/BookMuseumLambdaRole --region=ap-northeast-1

arn:aws:iam::403000007300; AWS 的帐号 ID。

The account ID of AWS.

handler index.handler; index 指的是 AWS Lambda 的文件 (index.js), handler 指的是 AWS Lambda 的 event handler。

handler index.handler; Index refers to aws lambda files (index.js), and handler  refers to event handler of aws lambda. 

  • 调用 AWS Lambda function (Invoke  the AWS Lambda function)
aws lambda invoke --function-name BookMuseum --cli-binary-format raw-in-base64-out --payload file://BookMuseum-payload.json response.json --region=ap-northeast-1

BookMuseum-payload.json: 定义 event 的信息; 由用户所提供的信息以 JSON 的格式提交给 BookMuseum。

BookMuseum-payload.json: defines the event’s information; The information provided by the user submit to BookMuseumin JSON  format.

BookMuseum-payload.json

{
  "buyer_id": "mariano",
  "museum_name": "tate gallery",
  "when":"2020-03-14"
}

response.json: AWS Lambda function回传的JSON格式的信息; 确定用户已完成参观博物馆的预订。

response.json: JSON-formatted information passed back by AWS Lambda function; Make sure that the user has completed a reservation to visit the museum.

{
  "when":"2020-03-14",
  "reservation_id":"CCEBA",
  "name":"tate gallery"
}

 

 

完整的代码, 请参考:

For the complete code, please refer to :

https://github.com/KenFang/Book-Museum

About Post Author

方俊贤; Ken Fang

专利号: 201910652769.4; 一种深度学习的算法, 预测微服务持续发布、持续部署后对产品整体质量的影响, 获得国家知识财产局专利; 符合专利法实施细则第 44 条的规定。
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据