
이전 실습 (썸네일로 변환하기) 의 연장선입니다.
만들었던 이전 실습(썸네일로 변환하기) 에 다음의 기능을 추가합니다.
s3 의 변경사항을 람다가 바로 받지 말고 SQS 받도록 변경
썸네일용 이미지의 링크를 이메일에 전달
대기열 생성 버튼 클릭

원본 버킷이 SQS에게 메세지를 줄 수 있도록 정책 변경하기

{
"Version": "2012-10-17",
 "Id": "example-ID",
 "Statement": [
   {
    "Sid": "example-statement-ID",
    "Effect": "Allow",
    "Principal": {"Service": "s3.amazonaws.com"},
    "Action": "SQS:SendMessage",
    "Resource": "arn:aws:sqs:ap-northeast-2:640668917513:test-que",
    "Condition": {
      "ArnLike": {
        "aws:SourceArn": "arn:aws:s3:::bk-pictrue"
      }
    }
  }
 ]
}Resource 에는 방금 만든 SQS 의 arn 정보를 넣고, aws:SourceArn 에는 원본 버킷 의 arn 을 넣는다.  
저장 버튼 클릭
속성 탭 이동이벤트 알림 생성 버튼 클릭  
  
편집 클릭  


정책 을 복사하여 넣는다.{
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "logs:PutLogEvents",
                    "logs:CreateLogGroup",
                    "logs:CreateLogStream"
                ],
                "Resource": "arn:aws:logs:*:*:*"
            },
            {
                "Sid": "AllAccess",
                "Action": "s3:*",
                "Effect": "Allow",
                "Resource": [
                    "*"
                ]
            },
            {
                "Sid": "SQSAllAccess",
                "Effect": "Allow",
                "Action": [
                    "sqs:*"
                ],
                "Resource": "*"
            }
        ]
 }트리거 추가 버튼 클릭  


주제 생성 버튼 클릭
주제 엑세스 정책 변경편집 으로 이동  
액세스 정책 편집기 이동arn:aws:sns:ap-northeast-2:640668917513:test-1 는 자신의 SNS arn 으로 바꿔준다.{
"Version": "2008-10-17",
"Id": "__default_policy_ID",
"Statement": [
 {
   "Sid": "__default_statement_ID",
   "Effect": "Allow",
   "Principal": {
     "AWS": "*"
   },
   "Action": [
     "SNS:GetTopicAttributes",
     "SNS:SetTopicAttributes",
     "SNS:AddPermission",
     "SNS:RemovePermission",
     "SNS:DeleteTopic",
     "SNS:Subscribe",
     "SNS:ListSubscriptionsByTopic",
     "SNS:Publish"
   ],
   "Resource": "arn:aws:sns:ap-northeast-2:640668917513:test-1",
   "Condition": {
     "StringEquals": {
       "AWS:SourceOwner": "640668917513"
     }
   }
 },
 {
   "Sid": "Allow-AWS-Service-to-publish-to-the-topic",
   "Effect": "Allow",
   "Principal": {
     "AWS": "*"
   },
   "Action": "SNS:Publish",
   "Resource": "arn:aws:sns:ap-northeast-2:640668917513:test-1"
 }
]
}이메일 구독 설정구독 생성 버튼 클릭  

Confirm subscription 클릭  

소스 코드 수정 후 재배포
app.js 에 넣는다.const aws = require('aws-sdk');
 const s3 = new aws.S3({ apiVersion: '2006-03-01' });
 const sharp = require('sharp');
 exports.lambdaHandler = async (event, context) => {
   try {
     event.Records[0].body = JSON.parse(event.Records[0].body);
     console.log("event: ", JSON.stringify(event));
     console.log("context: ", JSON.stringify(context));
     console.log("event.Records[0].body: ", event.Records[0].body);
     const s3Data = event.Records[0].body.Records[0].s3;
     const {awsRegion} = event.Records[0].body.Records[0];
     const bucket = s3Data.bucket.name;      // 원본 버킷 이름
     const key = s3Data.object.key;          // 원본 버킷 키
     console.log("bucket: ", bucket);
     console.log("key: ", key);
     const dstBucket = process.env.s3resized     // 썸네일 버킷 이름
     // 원본 버킷으로부터 파일 읽기
     const s3Object = await s3.getObject({
         Bucket: bucket,
         Key: key
     }).promise()
     console.log(`dstBucket: `, dstBucket);
     // 이미지 리사이즈, sharp 라이브러리가 필요합니다.
     const data = await sharp(s3Object.Body)
         .resize(200)
         .jpeg({ mozjpeg: true })
         .toBuffer()
     // 대상 버킷으로 파일 쓰기
     const result = await s3.putObject({
         Bucket: dstBucket,
         Key: key,                                       // 원본 버킷 키와 동일
         ContentType: 'image/jpeg',                      // 컨텐츠 타입
         Body: data,
         ACL: 'public-read'
     }).promise()
     snsArn = process.env.arnTopic
     const sns = new aws.SNS(awsRegion);
     const massage = await sns.publish({
       Message : "URL : "+`https://${process.env.s3resized}.s3.${awsRegion}.amazonaws.com/${key}`,
       Subject: "썸네일 도착",
       TopicArn : process.env.arnTopic
     }).promise()
     console.log(`massage :`, massage);
     return result;
   } catch (e) {
     console.log("ERROR: ", e);
     return "ERROR"
   }
 }

sam build; sam deploy;람다 함수의 환경변수 편집에 들어가서 SNS 환경 변수 설정
arnTopic : arn:aws:sns:ap-northeast-2:640668917513:test-1

추후 해볼 내용
S3의 Pre-signed URL 기능을 이용하여, 업로드 전용 URL을 획득하고, 이를 통해 이미지를 S3 업로드할 수 있게 만들어봅시다
참고자료:
3. 연습: 알림용 버킷 구성(SNS 주제 또는 SQS 대기열)
5. json strip
6. json viewer
7. How to Send an Email Notification Using AWS Lambda
8. 서버리스랜드
10. 깃허브