{Lambda}チュートリアル: Amazon Kinesis で AWS Lambda を使用する

https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-kinesis-example.html
https://qiita.com/fnaoto/items/0ef92551c8f19351a3a3


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

aws --version


-- 2. IAMロール作成
vim role01.json

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json


-- 3. ポリシーをロールにアタッチ
aws iam attach-role-policy \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaKinesisExecutionRole \
--role-name role01


-- 4. Lambda 関数を作成する

vim test.js

console.log('Loading function');

exports.handler = function(event, context) {
    //console.log(JSON.stringify(event, null, 2));
    event.Records.forEach(function(record) {
        // Kinesis data is base64 encoded so decode here
        var payload = Buffer.from(record.kinesis.data, 'base64').toString('ascii');
        console.log('Decoded payload:', payload);
    });
};

zip test.zip test.js


aws lambda create-function \
--function-name func01 \
--zip-file fileb://test.zip \
--handler test.handler \
--runtime nodejs12.x \
--role arn:aws:iam::999999999999:role/role01


aws lambda list-functions | grep func01
aws lambda get-function --function-name func01


-- 5. Lambda 関数をテストする
vim input.txt
{
    "Records": [
        {
            "kinesis": {
                "kinesisSchemaVersion": "1.0",
                "partitionKey": "1",
                "sequenceNumber": "49590338271490256608559692538361571095921575989136588898",
                "data": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==",
                "approximateArrivalTimestamp": 1545084650.987
            },
            "eventSource": "aws:kinesis",
            "eventVersion": "1.0",
            "eventID": "shardId-000000000006:49590338271490256608559692538361571095921575989136588898",
            "eventName": "aws:kinesis:record",
            "invokeIdentityArn": "arn:aws:iam::123456789012:role/lambda-kinesis-role",
            "awsRegion": "us-east-2",
            "eventSourceARN": "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream"
        }
    ]
}

aws lambda invoke \
--function-name func01 \
--payload file://input.txt \
output.txt \
--cli-binary-format raw-in-base64-out

cat output.txt


-- 6. Kinesis Stream を作成する

aws kinesis create-stream --stream-name kinesisstream01 --shard-count 1
aws kinesis describe-stream --stream-name kinesisstream01


-- 7. AWS Lambda でイベントソースを追加する

aws lambda create-event-source-mapping \
--function-name func01 \
--batch-size 1 \
--starting-position LATEST \
--event-source arn:aws:kinesis:ap-northeast-1:999999999999:stream/kinesisstream01

aws lambda list-event-source-mappings


-- 8. セットアップをテストする


aws kinesis put-record \
--stream-name kinesisstream01 \
--partition-key 1 \
--data "testtesttest" \
--cli-binary-format raw-in-base64-out


SHARD_ITERATOR=$(aws kinesis get-shard-iterator \
--shard-id shardId-000000000000 \
--shard-iterator-type TRIM_HORIZON \
--stream-name kinesisstream01 \
--query 'ShardIterator')

aws kinesis get-records --shard-iterator $SHARD_ITERATOR


入力したデータはBase64エンコーディングされている


-- 9. クリーンアップ

-- Lambdaイベントソースマッピング削除
aws lambda list-event-source-mappings \
--function-name func01

aws lambda delete-event-source-mapping \
--uuid dfa1f284-7395-4055-95c3-c2711ee2a094


-- Kinesis ストリーミングの削除
aws kinesis list-streams
aws kinesis delete-stream --stream-name kinesisstream01


-- Lambda関数の削除
aws lambda get-function --function-name func01
aws lambda delete-function --function-name func01


-- ロールの削除
aws iam list-roles | grep role01

aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaKinesisExecutionRole

aws iam delete-role --role-name role01