2018年1月21日日曜日

AWS SAM Local を CentOS7 にインストール

AWS SAM Local (ベータ版)を CentOS7 にインストールして、Lambda の python サンプルで動作確認してみます。

1.Docker インストール


yum で docker をインストールします。
# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# yum makecache fast
# yum install docker-ce
# systemctl start docker
# systemctl enable docker

2.AWS SAM Local インストール


rpm パッケージをダウンロードします。
# curl -OL https://github.com/awslabs/aws-sam-local/releases/download/v0.2.4/sam_0.2.4_linux_amd64.rpm
rpm パッケージをインストールします。
# rpm -ivh sam_0.2.4_linux_amd64.rpm
バージョンを確認します。
# sam -version
sam version 0.2.4

3.動作確認


GitHUBからサンプルをダウンロードします。
# yum install git
# git clone https://github.com/awslabs/aws-sam-local.git
python の HelloWorld サンプルに移動します。
# cd aws-sam-local/samples/hello-world/python/
サンプルを実行します。
# sam local invoke "HelloWorldFunction" -e event.json
2018/01/21 20:35:50 Successfully parsed template.yaml
2018/01/21 20:35:50 Connected to Docker 1.35
2018/01/21 20:35:50 Runtime image missing, will pull....
2018/01/21 20:35:50 Fetching lambci/lambda:python3.6 image for python3.6 runtime...
python3.6: Pulling from lambci/lambda
f338a32fa56c: Pull complete
4926b20b634f: Pull complete
298eae5902d7: Pull complete
e58d162628c7: Pull complete
e02bc73f2e71: Pull complete
Digest: sha256:0682e157b34e18cf182b2aaffb501971c7a0c08c785f337629122b7de34e3945
Status: Downloaded newer image for lambci/lambda:python3.6
2018/01/21 20:39:17 Invoking main.handler (python3.6)
2018/01/21 20:39:18 WARNING: No AWS credentials found. Missing credentials may lead to slow startup times as detailed in https://github.com/awslabs/aws-sam-local/issues/134
2018/01/21 20:39:18 Mounting /root/workspace/aws-sam-local/samples/hello-world/python as /var/task:ro inside runtime container
START RequestId: 0c79a16a-82df-40c4-904d-2619f8836f39 Version: $LATEST
Loading function
value1 = value1
value2 = value2
value3 = value3
END RequestId: 0c79a16a-82df-40c4-904d-2619f8836f39
REPORT RequestId: 0c79a16a-82df-40c4-904d-2619f8836f39 Duration: 14 ms Billed Duration: 0 ms Memory Size: 0 MB Max Memory Used: 15 MB

"Hello world"


これで、Lambda関数をローカルで実行できるようになりました。