{"id":8518,"date":"2016-07-16T09:41:04","date_gmt":"2016-07-16T01:41:04","guid":{"rendered":"https:\/\/ihower.tw\/blog\/?p=8518"},"modified":"2016-07-16T09:44:18","modified_gmt":"2016-07-16T01:44:18","slug":"aws-user-group-%e8%ab%87-aws-%e5%ae%b9%e5%99%a8%e8%88%87-serverless-%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-%e5%bf%83%e5%be%97%e7%b4%80%e9%8c%84","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/8518-aws-user-group-%e8%ab%87-aws-%e5%ae%b9%e5%99%a8%e8%88%87-serverless-%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-%e5%bf%83%e5%be%97%e7%b4%80%e9%8c%84","title":{"rendered":"AWS User Group:  \u8ac7 AWS \u5bb9\u5668\u8207 Serverless \u6700\u4f73\u5be6\u8e10 \u5fc3\u5f97\u7b46\u8a18"},"content":{"rendered":"<p>\u4eca\u5e74 2016\/4\/19 \u53c3\u52a0 <a href=\"http:\/\/awsugtw.kktix.cc\/events\/10-tpe\">AWS User Group in Taiwan Meetup #10<\/a> \u7684\u5fc3\u5f97\u7d00\u9304\u3002\u8b1b\u8005 Pahud \u807d\u5f97\u51fa\u4f86\u5c0d AWS \u975e\u5e38\u71b1\u60c5\uff0c\u800c\u4e14\u975e\u5e38\u5be6\u52d9\u5730\u5206\u4eab\u4e86 <a href=\"https:\/\/aws.amazon.com\/tw\/ecs\/details\/\">AWS ECS<\/a> \u548c <a href=\"https:\/\/aws.amazon.com\/tw\/lambda\/details\/\">AWS Lambda<\/a> \u7684\u5404\u7a2e\u9069\u7528\u60c5\u5883\u3002<\/p>\n<ul>\n<li>old school \u4f5c\u6cd5: ELB\/ASG + 2 subsets + RDS mutil-AZ\n<ul>\n<li>challenges<\/li>\n<li>  ec2 instances \u592a\u82b1\u9322 for micro services (\u90fd\u8981 x2 \u624d\u80fdHA)<\/li>\n<li>  complexity in infrastructure (VPC, routing-table, NAT, NACL, security groups, ELB&#8230;etc)<\/li>\n<li>  complexity in A\/B testing and B\/G deployment<\/li>\n<li>  deploy<\/li>\n<\/ul>\n<\/li>\n<li>Want to have self-healing, auto-scaling, AZ-balancing, log consolidation, immutable and stateless architecture, cost optimisation and resource optimisation\n<ul>\n<li>\u4eca\u5929\u7684\u4e3b\u984c: AWS ECS<\/li>\n<\/ul>\n<\/li>\n<li>ECS Cluster best practices\n<ul>\n<li>\u7528 ec2 \u7d44\u6210\u4e00\u7fa4\u6a5f\u5668<\/li>\n<li>ASG on demand \u8a3b\u518a\u5230 cluster<\/li>\n<li>ASG spot instance \u4e5f\u8a3b\u518a\u5230 cluster<\/li>\n<li>cloudwatch \u7528\u4f86\u76e3\u63a7\u9019\u5169\u500b ASG<\/li>\n<\/ul>\n<\/li>\n<li>Auto scaling policy design\n<ul>\n<li>scale out spot on 30% ~ 60% (cpu usage?)<\/li>\n<li>scale out on-demand when 60%<\/li>\n<li>scale in on-demand when 60%<\/li>\n<li>scale in spot when &lt;= 30%<\/li>\n<li>with minimal 1 on-demand or RI (reserved instance)<\/li>\n<li>spot fleet \u529f\u80fd\u53ef\u4ee5\u4e00\u6b21\u6a19\u591a\u53f0 instances <\/li>\n<\/ul>\n<\/li>\n<li>\u4e0d\u8981\u7528 SSH \u9032\u53bb\u6a5f\u5668\u7ba1\u7406\uff0c\u8b93 ECS \u63a7\u7ba1<\/li>\n<li>ECS\n<ul>\n<li>\u8a2d\u5b9a web, app, worker \u9700\u8981\u5e7e\u53f0 instance\uff0c\u5c31\u6703\u81ea\u52d5\u958b instance\uff0c\u5982\u679c\u4e0d\u5920\u5c31\u6703\u958b\u65b0\u7684 (?)<\/li>\n<li>internal ELB \u529f\u80fd\u53ef\u4ee5\u5c0d\u5167\u505a balance\uff0c\u662f\u5c0d\u5167 microservice \u7684 end-point<\/li>\n<li>SQS \u4e5f\u53ef\u4ee5\u7528\uff0c\u5206\u914d\u5230 instance \u53bb\u57f7\u884c<\/li>\n<li>\u7528 cloudwatch \u76e3\u63a7\uff0c\u4e26\u5206\u4e0d\u540c\u7dad\u5ea6\uff0c\u4f8b\u5982\u5206 container<\/li>\n<li>deployment and rolling update with revisions<\/li>\n<li>consolidate logs: \u4e0d\u8981\u7528 Docker \u7684 logger driver\uff0c\u8981\u7528 AWS log\u3002container \u6307\u5b9a log file \u8def\u5f91\u5c31\u6703\u6536\u96c6\u5230 cloudwatch <\/li>\n<li>\u53ef\u4ee5\u63a8 docker image \u5230 ECR (\u8b93\u958b\u767c\u3001\u6e2c\u8a66\u3001Production \u90fd\u5f9e\u9019\u88e1\u62c9docker image)<\/li>\n<\/ul>\n<\/li>\n<li>\u5982\u679c microservice \u591a\u5230\u975e\u5e38\u591a\n<ul>\n<li>ELB -&gt; ECS \u4e0d\u80fd\u52d5\u614b mapping port \u7aef\u53e3(i.e. 80 port \u5230 container \u7684 80)<\/li>\n<li>ELB -&gt; ECS on random ports: workaround: \u88dd\u4e00\u500b golang \u7684\u670d\u52d9<\/li>\n<li><a href=\"http:\/\/www.slideshare.net\/JulienSIMON5\/amazon-ecs-january-2016\/02\" class=\"autohyperlink\">www.slideshare.net\/JulienSIMON5\/amazon-ecs-january-2016\/02<\/a><\/li>\n<li>Meteor Galaxy session-aware with random ports: \u591a\u4e00\u500b Proxy<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2 id=\"toc_1\">AWS lambda + AWS API Gateway<\/h2>\n<p>Cloud native \u958b\u767c\u3001event-driven cloud computing: \u5b8c\u5168\u6c92\u6709 server \u9700\u8981\u7ba1\u7406\uff01<\/p>\n<ul>\n<li>AWS API Gateway\n<ul>\n<li>\u63d0\u4f9b REST \u7aef\u9ede\uff0c\u53bb\u89f8\u767c lambda<\/li>\n<li>service proxy \u529f\u80fd\u7121\u9808\u5beb lambda\uff0c\u5c31\u53ef\u4ee5\u5b58\u53d6\u64cd\u4f5c AWS \u8cc7\u6e90(\u7528 IAM role)<\/li>\n<li>\u53ef\u4ee5\u8f49\u767c\u5230\u4f60\u81ea\u5df1\u7684 EC2<\/li>\n<li>\u8a2d\u5b9a API Gateway Cache (\u4f46\u4e0d\u4fbf\u5b9c)<\/li>\n<li>\u53ea\u80fd https<\/li>\n<\/ul>\n<\/li>\n<li>\u6848\u4f8b\n<ul>\n<li>S3 -&gt; \u7e2e\u5716<\/li>\n<li>device -&gt;    kinesis -&gt; \u89f8\u767c lambda -&gt; \u653e s3 \u6216 dynamodb<\/li>\n<li>dynamodb \u6bcf\u4e00\u7b46\u66f4\u65b0 (stream) -&gt; \u89f8\u767c lambda <\/li>\n<li>AWS CloudTrail \u53ef\u4ee5 log AWS API \u64cd\u4f5c log \u9032\u5230 s3 -&gt; lambda -&gt; \u89f8\u767c SNS \u901a\u77e5<\/li>\n<\/ul>\n<\/li>\n<li>\u624b\u6a5f\u548c\u700f\u89bd\u5668\u7684 AWS SDK \u5c31\u53ef\u4ee5\u89f8\u767c AWS Lambda (\u4e0d\u9700\u8981 REST API \u7aef\u9ede\u9032 ELB)\n<ul>\n<li>\u6709 sync \u4e5f\u6709 async \u547c\u53eb\u6cd5<\/li>\n<\/ul>\n<\/li>\n<li>API Gateway call flow \u6703\u5148\u7d93\u904e cloudfront (e.g. \u53f0\u5317)\n<ul>\n<li>\u56e0\u70ba\u53ef\u4ee5\u4fdd\u8b49\u6700\u4f73\u8def\u7531\u5230 API Gateway (e.g. \u6771\u4eac)<\/li>\n<\/ul>\n<\/li>\n<li>\u8a8d\u8b49\u53ef\u4ee5\u7528 HTTP header\uff0c\u52a0\u4e0a\u4e00\u500b\u81ea\u5df1\u5beb\u7684 lambda \u53bb\u6aa2\u67e5\u81ea\u5df1\u7684\u8a8d\u8b49\u7cfb\u7d71<\/li>\n<li>API Gateway \u6bd4 lambda \u8cb4\u3002lambda \u975e\u5e38\u4fbf\u5b9c\u3002<\/li>\n<li>cost comprising: requests \u8d85\u904e\u67d0\u500b\u7d1a\u5225\uff0c\u662f\u53ef\u80fd\u6bd4 ec2 \u9084\u8cb4\u3002\u4ed6\u7684 cost \u662f\u7dda\u6027\u7684\u3002<\/li>\n<li>Lambda limit\n<ul>\n<li>soft limit: concurrency is 100 (\u8981\u5beb\u4fe1\u7d66 AWS \u5982\u679c\u8981\u8abf\u9ad8)<\/li>\n<li>hard limit: 300s max duration per invocation<\/li>\n<li>VPC restriction (lambda \u53ef\u4ee5\u8a2a\u554f\u4f60\u5167\u7db2\u7684\u8cc7\u6e90)\n<ul>\n<li>private IP addresses (\u7528\u6389\u4e00\u500b private IP)<\/li>\n<li>ENIC limit (\u7528\u5230\u4e00\u5f35\u865b\u64ec\u7db2\u5361) 20*5=100 \u6709\u9650\u5236 (\u4e00\u500b VPC subset \u901a\u5e38\u53ea\u80fd\u958b 20 \u500b instances, ENIC \u6700\u591a\u958b\u4e94\u500d)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>API Gateway\n<ul>\n<li>500-1000 QPS per AWS Account<\/li>\n<li>5M requests\/month = $18.79<\/li>\n<li>100 QPS = $974.07 \/ month \u5c31\u8cb4\u4e86\uff0c\u81ea\u5df1\u958b ec2 \u5bebcode\u53ef\u80fd\u6bd4\u8f03\u5212\u7b97<\/li>\n<li>No async or parallel invocation(\u540c\u6642\u5e73\u884c\u547c\u53eb\u591a\u500b lambda) with Lambda<\/li>\n<\/ul>\n<\/li>\n<li>lambda \u7f3a\u9ede\n<ul>\n<li>SNS -&gt; Lambda \u662f push invocation \u6548\u80fd\u6975\u597d<\/li>\n<li>kinesis or dynamodb stream \u662f pull invocation\uff0c\u6548\u80fd\u6162\u8ddf\u4e0d\u4e0a\n<ul>\n<li>\u89e3\u6cd5: delegation\uff0c\u591a\u958b\u4e00\u500b\u904e\u6c34 lambda \u8abf\u9ad8 higher memory\uff0c\u6293\u5230\u8cc7\u6599\u518d\u53e6 innovate lambda \u8655\u7406<\/li>\n<\/ul>\n<\/li>\n<li>no connection pooling\n<ul>\n<li>\u5167\u90e8 lambda sandbox \u6a5f\u5668\u53ef\u80fd\u6703\u4fdd\u755920\u5206\u9418\u4f86 reuse<\/li>\n<li>\u56e0\u6b64\u9023\u7dda\u7684 code \u8981\u653e handler scope\uff0c\u4e0d\u80fd\u653e header scope\uff0c\u4e0d\u7136 reuse \u6642\u6703\u6c92\u6709\u88ab\u57f7\u884c\u5230<\/li>\n<\/ul>\n<\/li>\n<li>\u53ea\u80fd\u7528 cloudwatch \u505a debugging\uff0c\u5f88\u75db\u82e6\uff0c\u751a\u81f3\u6703 delay 30s \u624d\u51fa\u4f86<\/li>\n<li>deployment \u5de5\u5177\u9084\u4e0d\u6210\u719f, \u76ee\u524d\u5f88\u591a\u5728\u958b\u767c\u4e2d\uff0c\u4f8b\u5982 serverless<\/li>\n<li>lack of PHP, ruby and golang<\/li>\n<li>re-deploy the whole bundle \u5f88\u82e6\u3002\u53ea\u6539\u4e00\u9ede\u6771\u897f\u4e5f\u9700\u8981\u6574\u5305 deploy\uff0c\u5f88\u6162\n<ul>\n<li>workaround: \u5148\u50b3\u5230 s3\uff0c\u7136\u5f8c\u53e6\u5beb\u4e00\u500b lambda \u505a\u6253\u5305\uff0c\u90e8\u7f72\u4e3b lambda\u3002\u9019\u6a23\u5c31\u53ef\u4ee5\u53ea\u50b3\u90e8\u4efd\u7684\u66f4\u65b0 code \u5230 s3 \u5373\u53ef\u3002\u4e0d\u7528\u6574\u5305\u50b3\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2 id=\"toc_2\">ECS or Lambda?<\/h2>\n<ul>\n<li>\u7528 ECS\n<ul>\n<li>financial concern: \u5982\u679c 100QPS+\uff0c\u7528 ECS \u6bd4\u8f03\u5212\u7b97<\/li>\n<li>operation concern: long running process (300s+) or API service \u7528 ECS<\/li>\n<li>ECS for long process, lambda for short<\/li>\n<li>language concern<\/li>\n<li>performance concern: CPU \u7b49<\/li>\n<li>\u7279\u5225\u7684 protocol<\/li>\n<\/ul>\n<\/li>\n<li>Lambda + API gateway\n<ul>\n<li>small project, simple business logic<\/li>\n<li>focus on code only<\/li>\n<li>stateless<\/li>\n<li>quick micro service <\/li>\n<li>simple integrate with other AWS services<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Serverless \u61c9\u8a72\u6703\u662f\u63a5\u4e0b\u4f86\u5e7e\u5e74 DevOps \u7684\u71b1\u9580\u984c\u76ee\uff0c\u53ea\u662f\u6574\u5834\u807d\u4e0b\u4f86\uff0c\u96d6\u7136\u8b1b\u8005\u5f88\u71b1\u60c5\u63a8\u85a6\uff0c\u4f46\u6211\u9084\u662f\u89ba\u5f97\u4f3c\u4e4e\u9084\u4e0d\u5230\u9032\u5834\u7684\u6642\u6a5f\uff0c\u5751\u591a\u53c8\u6c92\u6709\u6bd4\u8f03\u4fbf\u5b9c\u554a&#8230; XD<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5e74 2016\/4\/19 \u53c3\u52a0 AWS User Group in Taiwan Meetup #10 \u7684\u5fc3\u5f97 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/8518-aws-user-group-%e8%ab%87-aws-%e5%ae%b9%e5%99%a8%e8%88%87-serverless-%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-%e5%bf%83%e5%be%97%e7%b4%80%e9%8c%84\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008AWS User Group:  \u8ac7 AWS \u5bb9\u5668\u8207 Serverless \u6700\u4f73\u5be6\u8e10 \u5fc3\u5f97\u7b46\u8a18\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[67],"tags":[],"class_list":["post-8518","post","type-post","status-publish","format-standard","hentry","category-aws","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-2do","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/8518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=8518"}],"version-history":[{"count":5,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/8518\/revisions"}],"predecessor-version":[{"id":8522,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/8518\/revisions\/8522"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=8518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=8518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=8518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}