Share
Embed
Building a serverless application with AWS
How to build a simple image resizing service based on serverless architecture with AWS
mingrammer
September 27, 2017
Transcript
54 slides
-
S3 + Lambda + API Gatewayܳ ഝਊೠ ࢲߡܻझ ࢲ࠺झ ѐߊೞӝ
MinJae Kwon (@mingrammer) 2017.08.29 AWSKRUG Architecture (Make a serverless on-demand image resizing service) -
Name ӂ (MinJae Kwon) Nickname @mingrammer Email [email protected] Who Game
Server Engineer @ SundayToz Blog https://mingrammer.com Facebook https://facebook.com/mingrammer Github https://github.com/mingrammer Eng Blog https://medium.com/@mingrammer -
2. On-demand image resizing 4. Result & Review 3. Building
a service with AWS Contents 1. Serverless? -
Serverless? By Server Engineers Dist EC2 EC2 Cache Dist EC2
EC2 Cache Client Client API Code API Code By AWS -
Serverless? Dist EC2 EC2 Cache Dist EC2 EC2 Cache Client
Client API Code API Code By Server Engineers By AWS have to manage entire architectures and logics focus on logics -
Serverless? Amazon API Gateway AWS Lambda + Amazon S3 +
Amazon Kinesis Amazon DynamoDB Amazon SNS … … -
On-demand image resizing Resizing Server image resized User Service desired
parameters new image -
On-demand image resizing ܳ ܻ ࢤࢿ೧ف ঋইب ؽ নೠ ࢎૉ
ܳ ਃೡ ٸ ࢤࢿೡ ࣻ ܻ োਸ ݫੋ ۽җ ܻ࠙ೡ ࣻ ࣻ ܻ োী ٮܲ ஹೊ ࢿמ ਬزੋ झாੌ݂ -
On-demand image resizing ਃী ೠ ې ஏ য۰ ౠࣻ
ӝמਸ ਤೠ ৮ೠ ױੌ ੋझఢझ ࢎਊ ࠺ਊ ࠺ബਯۄ ౸ױ ࢲߡ ҙܻ ࠁױ ࢲ࠺झ ѐߊী݅ ೞҊ र -
Amazon S3 Amazon API Gateway AWS Lambda users resizing Architecture
On-demand image resizing -
Architecture with deploy process On-demand image resizing (advanced) Amazon S3
Amazon API Gateway AWS Lambda users resizing AWS IAM deploy Python (boto) -
Building a service with AWS AWS Lambda handler def lambda_handler(event,
context) Check existence of a resized image Get a resized image Put a resized image Resize an image Generate an thumbnail link Amazon S3 resizer code -
Building a service with AWS Amazon API Gateway AWS Lambda
handler def lambda_handler(event, context) Check existence of a resized image Get a resized image Put a resized image Resize an image Generate an thumbnail link Amazon S3 resizer code -
Building a service with AWS Set up IAM Lambda /
S3 / API Gatewayܳ ҙܻೡ ࣻ ח ুࣁझ ӂೠਸ ೡ -
Building a service with AWS Set up S3 bucket
҃۽ ࢤࢿ ҃۽ীࢲ ӏ mingrammer/{sub}/{category}/{filename} -
Building a service with AWS Set up S3 bucket S3
Bucket Policy ࢸ { "Version": "2008-10-17", "Id": "Policy1335892530063", "Statement": [ { "Sid": "Stmt1335892150622", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::386209384616:root" }, "Action": [ "s3:GetBucketAcl", "s3:GetBucketPolicy" ], "Resource": "arn:aws:s3:::mingrammer" }, { "Sid": "Stmt1335892526596", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::386209384616:root" }, "Action": “s3:PutObject", "Resource": "arn:aws:s3:::mingrammer/*" }, { "Sid": "PublicReadGetObject", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::mingrammer/*" } ] } -
Building a service with AWS Write Lambda handler function def
lambda_handler(event, context): sub = event.get('sub') category = event.get('category') filename = event.get('filename') width = event.get('width') # Check if the image url with given parameter exists # If the format of image is not supported, redirect to original image with Image(file=original_image['Body']) as img: # If width is not set, get width of original image # Check if the width is valid numeric value # If the thumbnail already exists, redirect to that # Image resizing ] event.get('field') ? -
Building a service with AWS Write Lambda handler function ../{sub}/{category}/{filename}?width=400
Amazon API Gateway AWS Lambda API Gateway Mapping Templateীࢲ Lambda۽ ۄఠܳ ֈѹષ -
Building a service with AWS Write Lambda handler function event.get('field')
Amazon API Gateway AWS Lambda Amazon S3 Valid? Format check Param check Amazon S3 Exists? Resize Save Amazon S3 original thumbnail Get object Get object Put object -
Building a service with AWS Upload Lambda handler function
ܻܳ ਤೠ wand ۄ࠳۞ܻ৬ ೩ٜ۞ ೣࣻо ನೣػ ੌਸ ೣԋ ୷ zip -R thumbnailer.zip wand/* generator.py -
Building a service with AWS (+) thumbnailerۄח IAM Role Lambdaীࢲ
S3ܳ ઁযೡ ࣻ ب۾ IAMীࢲ Policyܳ ୶о೧ঠೣ Setup Permissions -
Building a service with AWS Set up API Gateway with
Lambda /mingrammer/{sub}/{category}/{filename}ۄח ߡఉ ೞਤ ҃۽ ӏী ݏѱ ܻࣗझܳ ࢤࢿ -
Building a service with AWS Set up API Gateway with
Lambda /mingrammer/{sub}/{category}/{filename}ۄח ߡఉ ೞਤ ҃۽ ӏী ݏѱ ܻࣗझܳ ࢤࢿ ҃۽ ܳ Object ఃчਵ۽ ࢸ೧ S3۽ Ӕҗ زੌೠ Ӕ ߑधਸ ஂೞӝ ਤೣ ( {variable} ҃۽ ߸ࣻܳ ڷೣ ) -
Building a service with AWS Set up API Gateway with
Lambda {/filenamae} ҃۽ীࢲ ݫࢲ٘ ࢤࢿਸ ࢶఖೠ റ GET ݫࢲ٘ܳ ࢤࢿ -
Building a service with AWS Set up API Gateway with
Lambda {/filenamae} ҃۽ীࢲ ݫࢲ٘ ࢤࢿਸ ࢶఖೠ റ GET ݫࢲ٘ܳ ࢤࢿ /mingrammer/{sub}/{category}/{filename} ҃۽ী ೧ API ݫࢲ٘ܳ ೡ ࣻ -
Building a service with AWS Set up API Gateway with
Lambda ࢤࢿೠ GET ݫࢲ٘ী ખ ী ݅ٚ thumbnailۄח Lambda ೣࣻܳ োѾ -
Building a service with AWS Set up API Gateway with
Lambda /mingrammer/{sub}/{category}/{filename}ۄח API ূ٘ನੋী Lambda ೣࣻо ࢿҕਵ۽ োѾؽ -
Building a service with AWS Set up API Gateway with
Lambda ܻࢎਸ ਤ೧ [ݫࢲ٘ ਃ]ীࢲ widthۄח ۄఠܳ ߉ਸ ࣻ ب۾ URL (GET) ௪ܻ ۄఠ ୶о -
Building a service with AWS Set up API Gateway with
Lambda ઁ۽ زೞח ഛੋ ೞӝ ਤ೧ [పझ]ী ٜযо ೠ ҃۽ ߸ࣻ৬ ௪ܻ ۄఠܳ ֍Ҋ పझ None ?? -
Building a service with AWS Set up API Gateway with
Lambda ҃۽ ߸ࣻܳ ࢸ೮ীب Noneۄח ч ٜযоҊ API Gatewayীࢲ Lambda۽ context ৻ ୶оੋ ۄఠܳ ׳ೞӝ ਤ೧ࢶ Mapping Templatesܳ ࢸ೧ঠೣ Lambda۽ সਸ ਃೞӝ API Gatewayী ٜযয়Ѣա ઓೞח ߸ٜࣻਸ JSON ഋక۽ ݒೝೞৈ ׳ -
Building a service with AWS Set up API Gateway with
Lambda [ా ਃ]ী ٜযоݶ ࠄޙ ݒೝ ప࠶ۄח ೦ݾীࢲ ୶оೡ ࣻ -
Building a service with AWS Set up API Gateway with
Lambda sub, category, filename, width ۄఠܳ ߉ই৬ пп JSON ఃчਵ۽ ݒೝೣ -
Building a service with AWS Set up API Gateway with
Lambda ۄఠٜ ઁ۽ ׳غয ࢚ਵ۽ زೞৈ ߸ജػ URL ߈ജؽ -
Building a service with AWS Set up API Gateway with
Lambda S3ীࢲ ߸ജػ ܳ ഛੋೞৈ ઁ۽ ੜ زೣਸ ഛੋ -
Building a service with AWS Set up API Gateway with
Lambda పझܳ ֈয पઁ۽ ࢎਊೡ ࣻ ب۾ APIܳ ߓನ -
Building a service with AWS Set up API Gateway with
Lambda ݒ ࣚऔѱ ߓನо ৮ܐؽ -
Building a service with AWS Set up API Gateway with
Lambda ߓನػ APIо ੜ ز ೞա ઑӘ ࢚ೣ .. ࢚धਵ۽ח Location ࠁ न Redirectػ पઁ о ա৬ঠೣ (ਤ Response ഋध Defaultо ইפݴ Lambdaীࢲ ߸ജ դ റ, ਤ ഋక۽ Responseܳ ٜ݅য ղ۰ળѢ) -
Building a service with AWS Set up API Gateway with
Lambda [ݫࢲ٘ ]ীࢲ Body۽ ڄযח Locationਵ۽ Redirectionਸ ೞӝ ਤ೧ Locationҗ Status ؊ܳ ୶о -
Building a service with AWS Set up API Gateway with
Lambda ӒܻҊ [ా ]ীࢲ Locationҗ Status ؊ী Body۽ ڄযח чٜਸ ݒೝೣ -
Building a service with AWS Call the Lambda based API
APIܳ ߓನ ೠ റ ਗೞח widthчҗ ೣԋ APIܳ ഐ ೞݶ Body न Locationਵ۽ Redirectػ ܳ ߄۽ ࠅ ࣻ -
Result & Review ױ ࢲߡ ҙܻী ೠ Ҋ ਃহয
ੋ ۽ ࣇস ए ण ࠺ਊ ֫ ঋਵա হب ঋ ୡࠁ ੑীࢶ ҳഅ ৻ AWSী ೠ ӝࠄੋ ध ਃҳؽ ѐߊ/ߓನ/ࢸ ݒ рױೞҊ ࡅܴ -
хࢎפ MinJae Kwon (@mingrammer) 2017.09.27 AWSKRUG Architecture (Make a serverless
on-demand image resizing service)