Amazon Web Services introduced Serverless Application Model , or SAM, a couple of months ago. It defines simplified syntax for expressing serverless resources. SAM extends AWS CloudFormation to add support for API Gateway, AWS Lambda and Amazon DynamoDB.This blog will show how to create a simple microservice using SAM. Of course, we’ll use Couchbase instead of DynamoDB!

This blog will also use the basic concepts explained in Microservice using AWS API Gateway,AWS Lambda and Couchbase .SAM will show the ease with which the entire stack for microservice can bedeployed andmanaged.

As a refresher, here are key components in the architecture:


Miles to go ... - Arun Gupta: Microservice using AWS Serverless Application Mode ...
Client could be curl, AWS CLI/Console, Postman client or any other tool/API that can invoke a REST endpoint. AWS API Gateway is used to provision APIs. The top level resource is available at path /books .HTTP GET and POST methods arepublished for the resource. Each API triggers a Lambda function. Two Lambda functions are created, book-list function for listing all the books available and book-create function to create a new book. Couchbase is used as a persistence store in EC2. All theJSON documents are stored and retrieved from this database.

Other blogs on serverless:

Microservice using AWS API Gateway, AWS Lambda and Couchbase AWS IoT Button, Lambda and Couchbase Serverless FaaS with Lambda and Java

Let’s get started!

Serverless Application Model (SAM) Template

An AWS CloudFormation template with serverless resources conforming to the AWS SAM model is referred to as a SAM file or template. It is deployed as a CloudFormation stack.

Let’s take a look at our SAM template:

This template is available at github.com/arun-gupta/serverless/blob/master/aws/microservice/template.yml .

AWSTemplateFormatVersion : '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Microservice using API Gateway, Lambda and Couchbase Resources: MicroserviceGetAllGateway: Type: AWS::Serverless::Function Properties: Handler: org.sample.serverless.aws.couchbase.gateway.BucketGetAll Runtime: java8 CodeUri: s3://serverless-microservice/microservice-http-endpoint-1.0-SNAPSHOT.jar Timeout: 30 MemorySize: 1024 Environment: Variables: COUCHBASE_HOST: ec2-35-163-21-104.us-west-2.compute.amazonaws.com Role: arn:aws:iam::598307997273:role/microserviceRole Events: GetResource: Type: Api Properties: Path: /books Method: get MicroservicePostGateway: Type: AWS::Serverless::Function Properties: Handler: org.sample.serverless.aws.couchbase.gateway.BucketPost Runtime: java8 CodeUri: s3://serverless-microservice/microservice-http-endpoint-1.0-SNAPSHOT.jar Timeout: 30 MemorySize: 1024 Environment: Variables: COUCHBASE_HOST: ec2-35-163-21-104.us-west-2.compute.amazonaws.com Role: arn:aws:iam::598307997273:role/microserviceRole Events: GetResource: Type: Api Properties: Path: /books Method: post

SAM template Specification provide complete details about contents in the template. The key parts of the template are:

Defines two resources, both ofLambda Function type identified by AWS::Serverless::Function attribute. Name of the Lambda function is defined by Resources.<resource> . Class for each handler is defined by the value of Resources.<resource>.Properties.Handler attribute Java 8 runtime is used to run the Function defined by Resources.<resource>.Properties.Runtime attribute Code for the class is uploaded to an S3 bucket, in our case to s3://serverless-microservice/microservice-http-endpoint-1.0-SNAPSHOT.jar Resources.<resource>.Properties.Environment.Variables.COUCHBASE_HOST attribute value defines the host where Couchbase is running. This can be easily deployed on EC2 as explained at Setup Couchbase . Each Lambda function is triggered by an API. It is deployed using AWS API Gateway. The path is defined by Events.GetResource.Properties.Path . HTTP method is defined using Events.GetResource.Properties.Method attribute. JavaApplication

The Java application that contains the Lambda functions is at github.com/arun-gupta/serverless/tree/master/aws/microservice/microservice-http-endpoint .

Lambda function thatis triggered by HTTP GET method is shown:

public class BucketGetAllimplements RequestHandler<GatewayRequest, GatewayResponse> { @Override public GatewayResponsehandleRequest(GatewayRequestrequest, Contextcontext) { try { N1qlQueryquery = N1qlQuery .simple(select("*") .from(i(CouchbaseUtil.getBucketName())) .limit(10)); String result = CouchbaseUtil.getBucket().query(query).allRows().toString(); return new GatewayResponse(200, result, GatewayResponse.HEADERS_JSON); } catch (ConfigurationException e) { return new GatewayResponse(400, e.getMessage(), GatewayResponse.HEADERS_TEXT); } } }

A little bit of explanation:

Each Lambda function needs to implement the interface com.amazonaws.services.lambda.runtime.RequestHandler . API Gateway and Lambdaintegrationrequire a specific input format and output format . These formats aredefined as

本文数据库(综合)相关术语:系统安全软件

主题: JavaRESTUC
分页:12
转载请注明
本文标题:Miles to go ... - Arun Gupta: Microservice using AWS Serverless Application Mode ...
本站链接:http://www.codesec.net/view/520846.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(61)