下記の環境で、SAMを用いてサーバレスアプリケーションの構築と実行を試してみます。

SAMとは?
SAM(Serverless Application Model) は、サーバーレスアプリケーション構築用のオープンソースフレームワークです。CLIによりSAM テンプレートで定義されたサーバレスアプリケーションの構築、テスト、デバッグをローカルで実行できます。
サーバレスアプリケーションの構築と実行
環境ディレクトリに移動
下記のディレクトリでアプリケーションを構築するとCLoud9のファイルツリーから操作することができます。
$ cd /home/ec2-user/environment/
SAMのバージョン確認
Cloud9のAmazon Linuxには最初からSAMがインストール済でした。
$ sam --version
SAM CLI, version 1.3.2
アプリケーションの構築
「java11」と「gradle」を選択しています。
$ sam init
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
Which runtime would you like to use?
1 - nodejs12.x
2 - python3.8
3 - ruby2.7
4 - go1.x
5 - java11
6 - dotnetcore3.1
7 - nodejs10.x
8 - python3.7
9 - python3.6
10 - python2.7
11 - ruby2.5
12 - java8.al2
13 - java8
14 - dotnetcore2.1
Runtime: 5
Which dependency manager would you like to use?
1 - maven
2 - gradle
Dependency manager: 2
Project name [sam-app]:
Cloning app templates from https://github.com/awslabs/aws-sam-cli-app-templates.git
AWS quick start application templates:
1 - Hello World Example: Gradle
2 - EventBridge Hello World: Gradle
3 - EventBridge App from scratch (100+ Event Schemas): Gradle
4 - Step Functions Sample App (Stock Trader): Gradle
Template selection: 1
-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: java11
Dependency Manager: gradle
Application Template: hello-world
Output Directory: .
Next steps can be found in the README file at ./sam-app/README.md
Code language: JavaScript (javascript)
アプリケーションディレクトリに移動
構築されたアプリケーションディレクトリに移動します。
$ cd sam-app/
ソースをビルド
先ほど選択した「gradle」でビルドされます。
$ sam build
Building function 'HelloWorldFunction'
Running JavaGradleWorkflow:GradleBuild
Running JavaGradleWorkflow:CopyArtifacts
Build Succeeded
Built Artifacts : .aws-sam/build
Built Template : .aws-sam/build/template.yaml
Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided
アプリケーションをローカル実行
無事にサンプルアプリケーションのLambdaが実行されました。
$ sam local invoke
Invoking helloworld.App::handleRequest (java11)
Image was not found.
Building image...............................................................................................................................
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-java11:rapid-1.3.2.
Mounting /home/ec2-user/environment/sam-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: xxxxxxxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx Version: $LATEST
END RequestId: xxxxxxxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx
REPORT RequestId: xxxxxxxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx Init Duration: 459.71 ms Duration: 1754.05 ms Billed Duration: 1800 ms Memory Size: 512 MB Max Memory Used: 78 MB
{"statusCode":200,"headers":{"X-Custom-Header":"application/json","Content-Type":"application/json"},"body":"{ \"message\": \"hello world\", \"location\": \"xxx.xxx.xxx.xxx\" }"}Code language: JavaScript (javascript)
最後に
SAMによるサーバレスアプリケーションの構築と実行の環境が整いました。ここからは実際に下記のようにCloud9でファイルを編集しながら開発を進めることになると思います!



