{Lambda}チュートリアル: Amazon S3 トリガーを使用してサムネイル画像を作成する

https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-s3-tutorial.html

-- 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

-- 1.2 jqインストール
sudo yum -y install jq

-- 1.3 nodejsインストール

curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
sudo yum install -y nodejs
npm -v

 

-- 2. S3 バケットを作成する

aws s3 mb s3://bucket123
aws s3 mb s3://bucket123-resized

aws s3 ls

-- 3. テストファイルアップロード


aws s3api put-object --bucket bucket123 --key file01.jpg --body file01.jpg --content-type image/jpeg

aws s3 ls s3://bucket123 --recursive
aws s3 ls s3://bucket123-resized --recursive


-- 4. ポリシーの作成 

vim policy01.json

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:CreateLogStream"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": "arn:aws:s3:::bucket123/*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject"
            ],
            "Resource": "arn:aws:s3:::bucket123-resized/*"
        }
    ]
}


aws iam create-policy \
--policy-name policy01 \
--policy-document file://policy01.json

 


-- 5. ロールの作成

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


-- 6. ポリシーをロールにアタッチ

aws iam attach-role-policy --policy-arn arn:aws:iam::999999999999:policy/policy01 --role-name role01

 


-- 7. Lambda関数作成

 


mkdir lambda-s3
cd lambda-s3

vim test.js

// dependencies
const AWS = require('aws-sdk');
const util = require('util');
const sharp = require('sharp');

// get reference to S3 client
const s3 = new AWS.S3();

exports.handler = async (event, context, callback) => {

  // Read options from the event parameter.
  console.log("Reading options from event:\n", util.inspect(event, {depth: 5}));
  const srcBucket = event.Records[0].s3.bucket.name;
  // Object key may have spaces or unicode non-ASCII characters.
  const srcKey    = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
  const dstBucket = srcBucket + "-resized";
  const dstKey    = "resized-" + srcKey;

  // Infer the image type from the file suffix.
  const typeMatch = srcKey.match(/\.([^.]*)$/);
  if (!typeMatch) {
      console.log("Could not determine the image type.");
      return;
  }

  // Check that the image type is supported
  const imageType = typeMatch[1].toLowerCase();
  if (imageType != "jpg" && imageType != "png") {
      console.log(`Unsupported image type: ${imageType}`);
      return;
  }

  // Download the image from the S3 source bucket.

  try {
      const params = {
          Bucket: srcBucket,
          Key: srcKey
      };
      var origimage = await s3.getObject(params).promise();

  } catch (error) {
      console.log(error);
      return;
  }

  // set thumbnail width. Resize will set the height automatically to maintain aspect ratio.
  const width  = 200;

  // Use the sharp module to resize the image and save in a buffer.
  try {
      var buffer = await sharp(origimage.Body).resize(width).toBuffer();

  } catch (error) {
      console.log(error);
      return;
  }

  // Upload the thumbnail image to the destination bucket
  try {
      const destparams = {
          Bucket: dstBucket,
          Key: dstKey,
          Body: buffer,
          ContentType: "image"
      };

      const putResult = await s3.putObject(destparams).promise();

  } catch (error) {
      console.log(error);
      return;
  }

  console.log('Successfully resized ' + srcBucket + '/' + srcKey +
      ' and uploaded to ' + dstBucket + '/' + dstKey);
};

 


npm install sharp
ls -l

zip -r ../test.zip .

cd ..


aws lambda create-function \
--function-name func01 \
--zip-file fileb://test.zip \
--handler test.handler \
--runtime nodejs14.x \
--timeout 30 \
--memory-size 1024 \
--role arn:aws:iam::999999999999:role/role01

 


aws lambda list-functions | grep func01

aws lambda get-function --function-name func01

 

-- 8. Lambda 関数をテストする

vim inputfile.txt
{
  "Records":[
    {
      "eventVersion":"2.0",
      "eventSource":"aws:s3",
      "awsRegion":"us-west-2",
      "eventTime":"1970-01-01T00:00:00.000Z",
      "eventName":"ObjectCreated:Put",
      "userIdentity":{
        "principalId":"AIDAJDPLRKLG7UEXAMPLE"
      },
      "requestParameters":{
        "sourceIPAddress":"127.0.0.1"
      },
      "responseElements":{
        "x-amz-request-id":"C3D13FE58DE4C810",
        "x-amz-id-2":"FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
      },
      "s3":{
        "s3SchemaVersion":"1.0",
        "configurationId":"testConfigRule",
        "bucket":{
          "name":"bucket123",
          "ownerIdentity":{
            "principalId":"A3NL1KOZZKExample"
          },
          "arn":"arn:aws:s3:::bucket123"
        },
        "object":{
          "key":"file01.jpg",
          "size":1024,
          "eTag":"d41d8cd98f00b204e9800998ecf8427e",
          "versionId":"096fKKXTRTtl3on89fVO.nfljtsv6qko"
        }
      }
    }
  ]
}

aws lambda invoke \
--function-name func01 \
--invocation-type RequestResponse \
--payload file://inputfile.txt \
outputfile.txt \
--cli-binary-format raw-in-base64-out

cat outputfile.txt

aws s3 ls s3://bucket123 --recursive
aws s3 ls s3://bucket123-resized --recursive


-- 9. Lambda関数に権限を追加する

aws lambda add-permission \
--function-name func01 \
--statement-id s3 \
--action lambda:InvokeFunction \
--principal s3.amazonaws.com \
--source-arn arn:aws:s3:::bucket123 \
--source-account 999999999999


aws lambda get-policy \
--function-name func01 | jq -r .Policy  | jq .


-- 10. イベント通知を作成

aws s3api get-bucket-notification-configuration \
--bucket bucket123

vim a.json
{
"LambdaFunctionConfigurations": [
    {
        "Id": "en01",
        "LambdaFunctionArn": "arn:aws:lambda:ap-northeast-1:999999999999:function:func01",
        "Events": [
            "s3:ObjectCreated:Put"
        ],
        "Filter": {
            "Key": {
                "FilterRules": [
                    {
                        "Name": "Prefix",
                        "Value": ""
                    },
                    {
                        "Name": "Suffix",
                        "Value": ""
                    }
                ]
            }
        }
    }
  ]
}


aws s3api put-bucket-notification-configuration \
--bucket bucket123 \
--notification-configuration file://a.json

-- 11. S3 トリガーでテストする

 

aws s3api put-object --bucket bucket123 --key file02.jpg --body file02.jpg --content-type image/jpeg

aws s3 ls s3://bucket123 --recursive
aws s3 ls s3://bucket123-resized --recursive


-- 12. クリーンアップ

-- 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::999999999999:policy/policy01

aws iam delete-role --role-name role01


-- ポリシーの削除
aws iam list-policies | grep policy01

aws iam delete-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01


-- S3バケットの削除
aws s3 ls
aws s3 rb s3://bucket123 --force
aws s3 rb s3://bucket123-resized --force