2018年2月11日日曜日

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


下記の記事の続きです。
  1. AWS SAM Local を CentOS7 にインストール
  2. SSHでシェルを実行するLambda(python) 関数をAWS SAM Local でテストする 
  3. AWS SAM Local で Lambda(python) をデプロイする 
  4. AWS SAM Local でデプロイしたLambda(python) を実行する
  5. lambda(python)でEC2を検索して、別のlambda(python)関数を実行する - 1/2

前回、TriggerFunction を AWS Sam Local で動作確認しました。
今回は、TriggerFunction を AWSにデプロイし、スケジュールイベント(5分ごと)で実行してみます。

1.TriggerFunction用のIAMロール用意


TriggetFunction 用のロールを用意します。


インラインポリシーで、lambda関数を実行する権限を付けてます。
lambda_invoke の定義は以下のとおり。
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "lambda:InvokeFunction",
            "Resource": "arn:aws:lambda:*:*:function:*"
        }
    ]
}

2. TriggerFunction のデプロイ


S3上にパッケージを作成します。
[root@centos702 lambda2]# 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 878fce0d1586a726423c4c8f2b7efd8c  1535 / 1535.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>

CloudFormation を使用してデプロイします。
[root@centos702 lambda2]# aws cloudformation deploy --template-file /tmp/output-template.yaml --stack-name LambdaTriggerFunction

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

デプロイが成功すると、AWSコンソールのLambda画面で下図のように TriggerFunction が表示されます。





登録したスケジュールを見てみます。
[CloudWatchEvent]をクリックして、下にスクロールします。

下図のように、登録したスケジュールが表示されます。
この例では、5分ごとに TriggerFunction が実行されます。


3. 実行結果の確認


デプロイ後、しばらく待って、ログを見てみます。
ロググループを確認します。
[root@centos702 lambda1]# aws logs describe-log-groups --output text --query "logGroups[*].logGroupName"
/aws/lambda/SSHFunction /aws/lambda/TriggerFunction

TriggerFunction のログを見ます。
[root@centos702 lambda1]#  aws logs describe-log-streams --log-group-name /aws/lambda/TriggerFunction --output=text --query "logStreams[*].logStreamName"
2018/02/11/[$LATEST]75d34dba53084a94993fa09e00905cee
[root@centos702 lambda1]# aws logs get-log-events --log-group-name /aws/lambda/TriggerFunction --log-stream-name '2018/02/11/[$LATEST]75d34dba53084a94993fa09e00905cee' --output=text --query "events[*].message"
START RequestId: 9ff3b640-0ec7-11e8-8609-198ad9decfe5 Version: $LATEST
        Invoking worker_function on 10.0.10.65
        {u'Payload': <botocore.response.StreamingBody object at 0x7f67497f8bd0>, 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 202, 'RequestId': 'a0996bfe-0ec7-11e8-9ac3-31707f309fc8', 'HTTPHeaders': {'x-amzn-requestid': 'a0996bfe-0ec7-11e8-9ac3-31707f309fc8', 'content-length': '0', 'x-amzn-trace-id': 'root=1-5a7f96cf-5c0d200803432cde6ca9c811;sampled=0', 'x-amzn-remapped-content-length': '0', 'connection': 'keep-alive', 'date': 'Sun, 11 Feb 2018 01:05:19 GMT'}}, u'StatusCode': 202}
        END RequestId: 9ff3b640-0ec7-11e8-8609-198ad9decfe5
        REPORT RequestId: 9ff3b640-0ec7-11e8-8609-198ad9decfe5  Duration: 1072.07 ms    Billed Duration: 1100 ms        Memory Size: 128 MB   Max Memory Used: 59 MB

SSHFunctionのログを見てみます。
[root@centos702 lambda1]#  aws logs describe-log-streams --log-group-name /aws/lambda/SSHFunction --output=text --query "logStreams[*].logStreamName"
2018/02/11/[$LATEST]78278dfd8e7e4a9fb55f5134cd9f4029
[root@centos702 lambda1]# aws logs get-log-events --log-group-name /aws/lambda/SSHFunction --log-stream-name '2018/02/11/[$LATEST]78278dfd8e7e4a9fb55f5134cd9f4029' --output=text --query "events[*].message"
START RequestId: a0996bfe-0ec7-11e8-9ac3-31707f309fc8 Version: $LATEST
        [ERROR] 2018-02-11T01:05:19.699Z        a0996bfe-0ec7-11e8-9ac3-31707f309fc8    Socket exception: Operation not permitted (1)
        Connecting to 10.0.10.65
        Connected to 10.0.10.65
        Executing /usr/bin/aws s3 cp s3://blue21.dev.local/HelloWorld.sh /tmp/HelloWorld.sh --region us-east-1
download: s3://blue21.dev.local/HelloWorld.sh to ../../tmp/HelloWorld.shng


Executing /usr/bin/chmod 700 /tmp/HelloWorld.sh


Executing /tmp/HelloWorld.sh
        Sun Feb 11 01:05:20 UTC 2018
        ip-10-0-10-65.ec2.internal
        Hello


Executing /bin/rm /tmp/HelloWorld.sh
        END RequestId: a0996bfe-0ec7-11e8-9ac3-31707f309fc8
        REPORT RequestId: a0996bfe-0ec7-11e8-9ac3-31707f309fc8  Duration: 1570.62 ms    Billed Duration: 1600 ms        Memory Size: 128 MB   Max Memory Used: 67 MB



[root@centos702 lambda1]#