计算服务的演进
- AWS公有云基础设施
- 容器Containers
- Serverless(物理基础设施交由AWS进行托管,只需要关注代码逻辑)
- 资源预配置和使用率
- 可靠性和容错
- 扩展性
- 运维和管理
事件驱动 Event Driven
- 事件驱动 -> 持续自动扩展(λ)-> 按使用量付费(代码执行时间,使用力度,etc.)
- 代码只在需要的时候执行,并自动缩放
Lambda使用场景
-
S3 + λ = Dynamic data ingestion
图片压缩,视频转码,文建索引,日志处理,内容校验,筛选聚合 -
Kinesis + λ = Live stream process
应用程序活动审计,订单事务处理,点击流分析,IoT设备响应,遥测和计量 -
SNS + λ = Smart IT, Custom message
自动IT警报响应、自定义行为、IT审计、定义广播消息、消息推送、邮件推送 -
Direct call + λ = Serverless backend
微服务架构,移动端后端架构,IoT后端架构 -
DynamoDB + λ = Database triggers
数据校验,数据筛选,Live 通知 -
Alexa + λ = Voice triggers
在云端构建自定义自动语音响应系统
Lambda编程模型
exports.awsHandler = fuction(event, context, callback){
// console.log('Received event:', JSON.stringify(event, null, 2));
console.log('value1 = ', event.key1);
console.log('remaining time = ', context.getRemainingTimeInMillis());
console.log('functionName = ', context.functionName);
console.log('AWSrequestID = ', context.awsRequestId);
console.log('logGroupName = ', context.logGroupName);
console.log('logStreamName = ', context.logStreamName);
console.log('clientContext = ', context.clientContext);
if(typeof context.identity !== 'undefined'){
console.log('Cognity identity ID = ', context.identity.cognitoIdentityId);
}
context.succeed(event.key1);
}
// @awsHandler: 假设将代码保存为helloworld.js,helloworld.awsHandler则是处理程序
// @event: 将事件数据传递到处理程序
// @context: 提供运行时信息,与lambda执行环境作为交互,服务将此对象作为第二个参数传递给lambda函数的处理程序
// @callback: 回调函数是可选的,取决于是否希望将信息返回到调用方
AWS Lambda并发问题
对于Kinesis或者DynamoDB
- Lambda execute your function concurrently for each shard on the stream
- 如果流有100个活动分区,则最多会有100个Lambda函数并发调用运行
- Shards: One Lambda function concurrently invoked per Kinesis shard
- Increasing shards will cause more lambda function invoked concurrently
- Each individual shard follows ordered processing
Scale Kinesis by adding shards, Lambda will scale automatically.
流量激增问题
AWS Lambda将根据增加的流量动态扩展容量,具体取决于AWS账户的并发执行限制。为了处理突增流量,Lambda将立即根据预定量增加并发执行函数
- 正常并发数
- 流量突增,一次性加到3000
- 在3000之上,以500为单位继续扩展,直到满足负载或者达到安全限制
API Gateway
- 全托管、可扩展的RESTful API 网关服务
- 基于全球边缘站点快速分发内容
- 自带抗DDoS共计,包括伪造请求(7层)和SYN攻击(3层)
- 自定义多个API阶段(eg. 开发,测试,生产)
- 为所有API调用提供缓存层(caching layer)
- 自动使用Swagger模式做API接口管理
简介
-
定义和托管API
管理部署以及多版本和多环境 -
管控网络流量
DDoS防御以及自动管控流量代理到应用后端 -
利用AWS认证
利用Identity and Access Manager(IAM)控制访问云端资源
使用Swagger的好处
- API的定义将会透明保存在代码的Repository中
- API的定义可以用于结合其他工具生成文档源于和其他团队协同工作
- API可以用过命令行工作导入并轻松部署到API Gateway中托管并开始提供服务
./aws-api-import.sh --create /path/to/swagger.yaml