AWS SAM介绍

Dec 22, 2019


即将换项目,据说新项目是servless架构。看有空提前准备点AWS Lambda知识

关于SAM的定义:

AWS Serverless Application Model (SAM) is an open-source framework for building serverless applications

什么是Serverless Application

还是从AWS的文档中来看serverless application的定义如下:

A serverless application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. Note that a serverless application is more than just a Lambda function—it can include additional resources such as APIs, databases, and event source mappings.

其实关于serverless,意即其不需要server的,不过需要多说一句的是其实不需要申请任何AWS服务器的。

什么是AWS Lambda

AWS文档中对AWS Lambda开门见山的介绍:

AWS Lambda is a compute service that lets you run code without provisioning or managing servers.

为什么要使用AWS Lambda呢?AWS 文档也是相当的直接:

You pay only for the compute time you consume - there is no charge when your code is not running.

看样子用AWS Lambda是不需要服务器的,也是比较省钱的。

什么时候使用AWS Lambda呢?

  • Run your code in response to events.

  • Run your code in response to HTTP requests using Amazon API Gateway

  • Invoke your code using API calls made using AWS SDKs

AWS文档也给出了一些events的场景:

  • Changes to data in an Amazon S3 bucket

  • Changes to data in an Amazon DynamoDB table

  • Run your code in response to HTTP requests using Amazon API Gateway

AWS SAM CLI

安装

SAM CLI依赖于docker,所以不管当前环境是Linux, Windows还是MacOS,首先要安装好docker。

笔者使用的是MacOS,且已经安装了brew神器,所以安装AWS CLI非常简单:

brew tap aws/tap
brew install aws-sam-cli

安装完成后,就可以直接使用sam命令了,如sam -version

如果你希望在Windows环境安装SAM CLI,请参考官方文档 Installing the AWS SAM CLI on Windows

如果你希望在Linux环境安装SAM CLI,请参考官方文档 Installing the AWS SAM CLI on Linux

创建项目

测试和调试

参考文章