Cloud in Action: Learning Serverless Architectures

Cloud in Action: Learning Serverless Architectures


薛国锋     xueguofeng2011@gmail.com

 

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:

Untitled.png

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)


Untitled.png


Create and deploy the API – ‘XGFMicroservice’


Create the api name – XGFMicroservice:

Untitled.png


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

Untitled.png


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

Untitled.png


Untitled.png


Untitled.png


{

    "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


Untitled.png


Untitled.png

Create the role – ‘myLambdaRole’ 

Untitled.png

Create the Lambda function – ‘XGFReturnPhoneNumber’

Untitled.png

Untitled.png

// 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']]

'''

Untitled.png

 

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

Untitled.png

// 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())

相关文章
相关标签/搜索