2018年2月3日土曜日

AWS SAM Local でデプロイしたLambda(python) を実行する

前回の記事でデプロイした Lmbda 関数を実行します。

事前に、EC2インスタンスを起動しておきます。

1. テスト用のイベントを作成する


テスト用のイベントを作成します。



用意したEC2インスタンスのプライベートIPアドレスを指定します。



2.Lambda関数を実行する


上記で作成したイベントを指定して、[テスト]ボタンをクリックします。



関数の実行が成功すると下図のようにログが表示されます。



ログを見ると、下記のようなエラーメッセージがありました。

[ERROR] 2018-02-03T08:46:55.405Z c9897fd5-08be-11e8-8efc-358b494b1cbf Socket exception: Operation not permitted (1)

そこで、プログラムを修正しました。
下記のように記述していたところを
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())

下記のように修正しました。
c.set_missing_host_key_policy(paramiko.WarningPolicy())

AWS SAM Localで再度パッケージ化してデプロイしました。
[root@centos702 lambda1]# sam package --template-file template.yaml --s3-bucket blue21.dev.local --output-template-file /tmp/output-template.yaml
A newer version of the AWS SAM CLI is available!
Your version:   0.2.4
Latest version: 0.2.6
See https://github.com/awslabs/aws-sam-local for upgrade instructions

Uploading to 1a0a221d60d64481d730a3c59d8beb34  5730978 / 5730978.0  (100.00%)
Successfully packaged artifacts and wrote output template to file /tmp/output-template.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /tmp/output-template.yaml --stack-name <YOUR STACK NAME>
[root@centos702 lambda1]# aws cloudformation deploy --template-file /tmp/output-template.yaml --stack-name LambdaSSHFunction

Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - LambdaSSHFunction

もう一度実行すると、エラーが消えました。