What Is the AWS Serverless Application Model (AWS SAM)
The AWS Serverless Application Model (AWS SAM) is an open-source framework that you can use to build serverless applications on AWS.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.
簡單來說,就是結合 Lambda function 跟事件來源、其他資源等 serverless 的組合技
AWS SAM template specification.
顧名思義,不解釋,自己看比較快:AWS Serverless Application Model Specification
下面提供兩個範例
example: s3 create object send event to lambda function
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: s3 create object send event to lambda function
Resources:
MyLambda:
Type: AWS::Serverless::Function
Properties:
Handler: my_lambda.my_handler
Timeout: 300
Runtime: python3.6
Role: arn:aws:iam:::role/LambdaRole
Events:
S3Event:
Type: S3
Properties:
Bucket: !Ref MyBucket
Events: s3:ObjectCreated:*
MyBucket:
Type: AWS::S3::Bucket
Properties:
BucketName:
example: cloudwatch schecduled event (每天凌晨兩點) trigger lambda function
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: cloudwatch schecduled event trigger lambda function
Resources:
MyLambda:
Type: AWS::Serverless::Function
Properties:
Handler: my_lambda.my_handler
Timeout: 300
Runtime: python3.6
Role: arn:aws:iam:::role/LambdaRole
Events:
MyEvent:
Type: Schedule
Properties:
Schedule: cron(0 18 * * ? *)
AWS SAM command line interface (AWS SAM CLI)
The AWS SAM CLI lets you locally build, test, and debug serverless applications that are defined by AWS SAM templates.
可以在本機執行 Lambda Function,並且如果用使用 event source 的話,可以產生測試用的 event 方便本機測試;另外,可以結合 CI 服務,執行 build, package, deploy,做到 CI/CD
Install
$ pip install awscli
$ pip install aws-sam-cli
How to use
validate
顧名思義,驗證 template 寫法是否正確
build
可以配合使用 --use-container 參數解決第三方程式庫引入問題
--use-container
If your functions depend on packages that have natively compiled dependencies, use this flag to build your function inside an AWS Lambda-like Docker container
local
example
$ sam local invoke -e event.json MyLambda
invoke
Invokes a local Lambda function once.
generate-event
產生測試用的 event 方便本機測試
You can use this command to generate sample payloads from different event sources such as S3,
API Gateway, and SNS. These payloads contain the information that the event sources send to your Lambda functions.
Generate the event that S3 sends to your Lambda function when a new object is uploaded
$ sam local generate-event s3 [put/delete]
You can even customize the event by adding parameter flags. To find which flags apply to your command,
run:
$ sam local generate-event s3 [put/delete] --help
Then you can add in those flags that you wish to customize using
$ sam local generate-event s3 [put/delete] --bucket --key
After you generate a sample event, you can use it to test your Lambda function locally
$ sam local generate-event s3 [put/delete] --bucket --key | sam local invoke
example: s3 create object event
{
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "ap-northeast-1",
"eventTime": "1970-01-01T00:00:00.000Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"responseElements": {
"x-amz-request-id": "EXAMPLE123456789",
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {
"name": "my_bucket",
"ownerIdentity": {
"principalId": "EXAMPLE"
},
"arn": "arn:aws:s3:::my_bucket"
},
"object": {
"key": "test.txt",
"size": 19,
"eTag": "8a6da8606b1e063921d61dbaf8f5b643",
"sequencer": "0A1B2C3D4E5F678901"
}
}
}
]
}
example: cloudwatch scheduled event
{
"id": "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c",
"detail-type": "Scheduled Event",
"source": "aws.events",
"account": "",
"time": "1970-01-01T00:00:00Z",
"region": "us-east-1",
"resources": [
"arn:aws:events:us-east-1:123456789012:rule/ExampleRule"
],
"detail": {}
}
package
Package an AWS SAM application. This is an alias for 'aws cloudformation package'.
deploy
Deploy an AWS SAM application. This is an alias for 'aws cloudformation deploy'.
沒有留言:
張貼留言