今天終於要使用AWS Lambda Function, 目的為了減輕Server負荷。
事不宜遲,去片。
先到AWS Lambda 開新Function(選擇你想使用的Language Node/Java/Python),本次用了Python進行CURL RETURN RESULT。
然後,進入Function 幫它插上SDK,這樣才能import request。
再提供API Gateway,使Function擁有URL被Call。
最後 Develop program,Test program,Deploy program。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
//Example import json from botocore.vendored import requests // Source from SDK def lambda_handler(event, context): // get requester IP ip = event['requestContext']['http']['sourceIp']; res1 = requests.get('https://xxxxx.com/api/findMyCountry/'+ip) // Start assumed result res1 = { countryCode:"[YOUR COUNTRY]", xxxxxxx: "xxxx", xxxxxx: "xxxx", xxxxx: "xxxx", } // End assumed result code = res1.json()['countryCode']; res2 = requests.get("https://xxx.com/api/findTotalUser") // Start assumed result res2 = 9999; // End assumed result total = res2; message = "" if total > 9000: message = "Many User" elif total < 1000: message = "Few User" if code == "XX": message = "You are not HK" status = 400; else: status = 200; return { "status":status, "message":message, "remark":"This is example" } |
完成!
***
現時(2020-10-29) AWS Lambda Function 可以提供 1000個concurrency limit.
如果你做緊個大Project既話,1000實在太少。
不過,你可以要求提升上限既。
只要去Support ticket with AWS 申請new support case.
選擇 “Service Limit Increase”…”Lambda as Limit type”… 填返D資料…最後等AWS回覆就搞掂。