Cloud in Action: Learning Serverless Architectures


薛国锋     [email protected]

 

Serverless architectures refer to applications that significantly depend on third-party services (BaaS - Backend as a Service), or move server side code from the long-running components to ephymeral function instances (FaaS - Function as a Service); both allows us to build and run applications and services without thinking about servers or the infrastructure. Building serverless applications means that the developers can focus on the core products instead of worrying about managing and operating the infrastructure, either in the cloud or on-premises. This reduced overhead lets developers reclaim time and energy that can be spent on developing great products.

 

Besides its positive aspects including reduced operational and development costs, easier opertioanl managerment and reduced environmental impact, Serverless architectures also come with some significant trade-offs, such as vendor control, vendor lock-in and security concerns etc. So it deserves being more careful when we take the plunge into Serverless systems now, especially in the FaaS realm.

 

Today we are going to experience the serverless architecutres with Amazon API Gateway, Lambda and DynamoDB, and develop a mini-app -  the distributed phonenumber query system. Below is the system architecture and design:

Cloud in Action: Learning Serverless Architectures_第1张图片

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale: https://aws.amazon.com/api-gateway/


AWS Lambda is an event-driven computing cloud service allowing developers to program functions on a pay-per-use basis without having to provision storage or compute resources to support them: https://aws.amazon.com/lambda/


Amazon DynamoDB is a fast and flexible NoSQL database service for all applications that need consistent, single-digit millisecond latency at any scale: https://aws.amazon.com/dynamodb/

 

-         Create the DynamoDB table and some items (name - number)

-         Create and deploy the API – ‘XGFMicroservice’

-         Create the role – ‘myLambdaRole’

-         Create the Lambda function – ‘XGFReturnPhoneNumber’

-         Test the the API – ‘XGFMicroservice’ 


Create the DynamoDB table and add items (name - number)


Cloud in Action: Learning Serverless Architectures_第2张图片


Create and deploy the API – ‘XGFMicroservice’


Create the api name – XGFMicroservice:

Cloud in Action: Learning Serverless Architectures_第3张图片


Create the resource and method of XGFMicroservice, we will only implement the ‘GET’ method:

Cloud in Action: Learning Serverless Architectures_第4张图片


Set the Lamdba Function - XGFReturnPhoneNumber as the API method execution:

Cloud in Action: Learning Serverless Architectures_第5张图片


Cloud in Action: Learning Serverless Architectures_第6张图片


Cloud in Action: Learning Serverless Architectures_第7张图片


{

    "inputname": "$input.params('name')"

}

 

Deploy the API – XGFMicroservice:

the Invoke URL: https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production.

https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production/api/v1/name/richard

https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production/api/v1/name/apple

https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production/api/v1/name/eric

https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production/api/v1/name/kevin


Cloud in Action: Learning Serverless Architectures_第8张图片


Cloud in Action: Learning Serverless Architectures_第9张图片

Create the role – ‘myLambdaRole’ 

Cloud in Action: Learning Serverless Architectures_第10张图片

Create the Lambda function – ‘XGFReturnPhoneNumber’

Cloud in Action: Learning Serverless Architectures_第11张图片

Cloud in Action: Learning Serverless Architectures_第12张图片

// write the Python code to implement the Lambda function – ‘XGFReturnPhoneNumber’

// lambda_function.py

from __future__ import print_function

import json

import boto3

 

print('Loading function……')

 

def lambda_handler(event, context):

 

    dynamodb = boto3.resource('dynamodb')

    table = dynamodb.Table('name_number')  // access the DynamoDB table – ‘name_number’

   

    temp = {}

    temp['name'] = event['inputname']  // { ‘name’, ‘richard’ …}

   

    try:

        response = table.get_item( Key=temp )  // get the item via the primary key – ‘name’ and its value

        result = (response['Item']['number'])

    except:

        result = '0000000000'  // if not found, return000000000

    return result

 

''' // adopt the dictionary list to replace DyanmoDB

    name_number = { 'richard':'1111111111', 'apple':'2222222222',

                    'eric':'3333333333', 'kevin':'4444444444'}

    return name_number[event['inputname']]

'''

Cloud in Action: Learning Serverless Architectures_第13张图片

 

Test the the API – ‘XGFMicroservice’

 

gset@ubuntu:~$ curl https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production/api/v1/name/richard

gset@ubuntu:~$ curl https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production/api/v1/name/apple

gset@ubuntu:~$ curl https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production/api/v1/name/eric

gset@ubuntu:~$ curl https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production/api/v1/name/kevin

gset@ubuntu:~$ curl https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production/api/v1/name/others

Cloud in Action: Learning Serverless Architectures_第14张图片

// test.py

import requests

 

import json

from pkg_resources.extern import names

 

header = {'content-type': 'application/json'}

 

names = ['richard','apple','eric','kevin','others']

 

for name in names:

    r = requests.get( url='https://52j710k7sc.execute-api.us-west-2.amazonaws.com/production/api/v1/name/'+name,

                                                                  headers=header) 

    print(r.status_code)

    print(r.headers['content-type'])

    print(r.url)

print(r.json())


Cloud in Action: Learning Serverless Architectures_第15张图片


Cloud in Action: Learning Serverless Architectures_第16张图片