Cloud in Action: Learning Serverless Architectures

网友投稿 244 2022-10-17

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:

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: 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: DynamoDB is a fast and flexible NoSQL database service for all applications that need consistent, single-digit millisecond latency at any scale: 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)

Create and deploy the API – ‘XGFMicroservice’

Create the api name – XGFMicroservice:

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

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

{

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

}

Deploy the API – XGFMicroservice:

the Invoke URL: the role – ‘myLambdaRole’

Create the Lambda function – ‘XGFReturnPhoneNumber’

// 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, return ‘000000000’

return result

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

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

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

return name_number[event['inputname']]

'''

Test the the API – ‘XGFMicroservice’

gset@ubuntu:~$ curl curl curl curl curl 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())

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:阿里云幸运券抽奖规则
下一篇:Java面向对象的封装你了解吗
相关文章

 发表评论

暂时没有评论,来抢沙发吧~