试试能在本地环境中运行的AWS SAM本地版本(aws-sam-local)

安装

可以通过npm进行安装

$ npm install -g aws-sam-local

生成项目

在创建一个适当的目录后,可以参考SAM官方项目的示例,逐步创建index.js和template.yml。

AWSTemplateFormatVersion : '2017-09-20'

Description: A hello world application.

Resources:
  HelloWorld:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs6.10
'use strict';
console.log('Loading function');

exports.handler = (event, context, callback) => {
    callback(null, 'Hello World!');
};

执行

如果能够创建index.js和template.yml文件,我将尝试运行它们。

在使用之前,需要提前运行Docker。

$ echo '{}' | sam local invoke HelloWorld
2017/09/20 18:39:12 Successfully parsed template.yaml
2017/09/20 18:39:12 Connected to Docker 1.30
2017/09/20 18:39:12 Fetching lambci/lambda:nodejs6.10 image for nodejs6.10 runtime...
nodejs6.10: Pulling from lambci/lambda
Digest: sha256:b9a57b98dcfe226cac3fb0b8329594eefb62ba7089fa27cf8ac968e5736cc04a
Status: Image is up to date for lambci/lambda:nodejs6.10
2017/09/20 18:39:15 Reading invoke payload from stdin (you can also pass it from file with --event)
2017/09/20 18:39:15 Invoking index.handler (nodejs6.10)
START RequestId: feb31e2a-3748-1152-e1c1-85725b4021a7 Version: $LATEST
2017-09-20T09:39:16.722Z    feb31e2a-3748-1152-e1c1-85725b4021a7    Loading function
END RequestId: feb31e2a-3748-1152-e1c1-85725b4021a7
REPORT RequestId: feb31e2a-3748-1152-e1c1-85725b4021a7  Duration: 9.66 ms   Billed Duration: 0 ms   Memory Size: 0 MB   Max Memory Used: 29 MB

"Hello World!"


第一次执行时,会先进行Docker镜像的下载,然后再执行。