serverless-starter-python
Advanced tools
| {"country": "Italy"} |
| """ | ||
| Serverless Module: Lambda Handler | ||
| - Your lambda functions should be a thin wrapper around your own separate | ||
| modules, to keep your code testable, reusable and AWS independent | ||
| """ | ||
| from __future__ import print_function | ||
| import json | ||
| import logging | ||
| import sys, os | ||
| sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../")) | ||
| import lib | ||
| log = logging.getLogger() | ||
| log.setLevel(logging.DEBUG) | ||
| def handler(event, context): | ||
| return lib.continent_by_country_name(event) |
| { | ||
| "name": "continent", | ||
| "runtime": "python2.7", | ||
| "description": "Serverless Lambda function for project: serverless-starter-python", | ||
| "customName": false, | ||
| "customRole": false, | ||
| "handler": "continent/handler.handler", | ||
| "timeout": 6, | ||
| "memorySize": 1024, | ||
| "authorizer": {}, | ||
| "custom": { | ||
| "excludePatterns": [] | ||
| }, | ||
| "endpoints": [ | ||
| { | ||
| "path": "continent", | ||
| "method": "GET", | ||
| "type": "AWS", | ||
| "authorizationType": "none", | ||
| "authorizerFunction": false, | ||
| "apiKeyRequired": false, | ||
| "requestParameters": {}, | ||
| "requestTemplates": "$${apiRequestTemplate}", | ||
| "responses": { | ||
| "400": { | ||
| "selectionPattern": "^\\[BadRequest\\].*", | ||
| "responseModels": "$${apiResponseModelsError}", | ||
| "responseTemplates": "$${apiResponseTemplateError}", | ||
| "statusCode": "400" | ||
| }, | ||
| "default": { | ||
| "statusCode": "200", | ||
| "responseParameters": {}, | ||
| "responseModels": { | ||
| "application/json;charset=UTF-8": "Empty" | ||
| }, | ||
| "responseTemplates": { | ||
| "application/json;charset=UTF-8": "" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ], | ||
| "events": [], | ||
| "environment": { | ||
| "SERVERLESS_PROJECT": "${project}", | ||
| "SERVERLESS_STAGE": "${stage}", | ||
| "SERVERLESS_REGION": "${region}" | ||
| }, | ||
| "vpc": { | ||
| "securityGroupIds": [], | ||
| "subnetIds": [] | ||
| } | ||
| } |
| { | ||
| "apiRequestTemplate": { | ||
| "application/json": { | ||
| "country": "$input.params('country')" | ||
| } | ||
| }, | ||
| "apiResponseModelsError": { | ||
| "application/json": "Error" | ||
| }, | ||
| "apiResponseTemplateError": { | ||
| "application/json": { | ||
| "message": "$input.path('$.errorMessage')" | ||
| } | ||
| } | ||
| } |
| """ Continent lib """ | ||
| from countrycode import countrycode | ||
| def _error(msg): | ||
| """ Utility to handle custom errors (see response mapping) """ | ||
| raise Exception("[BadRequest] %s" % msg) | ||
| def continent_by_country_name(event): | ||
| """ Get continent name (e.g. "Europe"), given a country name (e.g. "Italy") """ | ||
| country_name = event.get('country') | ||
| if not country_name: | ||
| return _error("Invalid event (required country)") | ||
| continent = countrycode(codes=[country_name], origin="country_name", target='continent') | ||
| if not continent: | ||
| return _error("Invalid country: %s" % country_name) | ||
| return { | ||
| "continent": next(iter(continent)), | ||
| } |
| """ Multi Lib """ | ||
| def multi_create(event): | ||
| """ Multi - Create """ | ||
| return { | ||
| "message": "Your Serverless function 'multi/create' ran successfully", | ||
| } | ||
| def multi_show(event): | ||
| """ Multi - Show """ | ||
| pid = event['pathId'] | ||
| msg = "Your Serverless function 'multi/show' ran successfully ith the following ID '%s'" % pid | ||
| return { | ||
| "message": msg, | ||
| } |
| """ Single Lib """ | ||
| def single_all(event): | ||
| """ Single - All """ | ||
| method = event.get('httpMethod', "?") | ||
| msg = "Your Serverless function ran successfully via the '%s' method" % method | ||
| return { | ||
| "message": msg, | ||
| } |
| countrycode |
Sorry, the diff of this file is not supported yet
+2
-2
| { | ||
| "name": "serverless-starter-python", | ||
| "version": "0.2.3", | ||
| "version": "0.2.4", | ||
| "description": "A bare bones Serverless Framework project with examples for common use cases in Python.", | ||
@@ -18,3 +18,3 @@ "author": { | ||
| }, | ||
| "homepage": "https://github.com/serverless/serverless-starter#readme", | ||
| "homepage": "https://github.com/alexcasalboni/serverless-starter-python#readme", | ||
| "scripts": {}, | ||
@@ -21,0 +21,0 @@ "_npmOperationalInternal": { |
+1
-0
@@ -35,3 +35,4 @@ #Serverless Starter | ||
| * **Single:** A single function that uses multiple endpoints. | ||
| * **Continent:** A real-world example with Python requirements and response mapping. | ||
| * **Templates:** Templates are used to reduce configuraton syntax | ||
| * **REST API Parameters:** The Multi/Show function endpoint gives an example of how to accept a path parameter |
+18
-19
| """ Lib """ | ||
| def single_all(event): | ||
| """ Single - All """ | ||
| method = event.get('httpMethod') or "?" | ||
| msg = "Your Serverless function ran successfully via the '%s' method" % method | ||
| return { | ||
| "message": msg, | ||
| } | ||
| LIBS_PATH = "../vendored" | ||
| def multi_create(event): | ||
| """ Multi - Create """ | ||
| return { | ||
| "message": "Your Serverless function 'multi/create' ran successfully", | ||
| } | ||
| # add vendored folder to sys path | ||
| import sys, os | ||
| here = os.path.dirname(os.path.realpath(__file__)) | ||
| sys.path.append(os.path.join(here, LIBS_PATH)) | ||
| def multi_show(event): | ||
| """ Multi - Show """ | ||
| pid = event['pathId'] | ||
| msg = "Your Serverless function 'multi/show' ran successfully ith the following ID '%s'" % pid | ||
| return { | ||
| "message": msg, | ||
| } | ||
| # Manually load OS libraries | ||
| # ref: https://serverlesscode.com/post/deploy-scikitlearn-on-lamba/ | ||
| import ctypes | ||
| for d, dirs, files in os.walk(LIBS_PATH): | ||
| for f in files: | ||
| if ".so" in f: | ||
| ctypes.cdll.LoadLibrary(os.path.join(d, f)) | ||
| # expose libraries in module scope | ||
| from single import single_all | ||
| from multi import multi_create, multi_show | ||
| from continent import continent_by_country_name |
@@ -11,2 +11,4 @@ """ | ||
| import logging | ||
| import sys, os | ||
| sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../")) | ||
| import lib | ||
@@ -13,0 +15,0 @@ |
@@ -11,2 +11,4 @@ """ | ||
| import logging | ||
| import sys, os | ||
| sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../")) | ||
| import lib | ||
@@ -13,0 +15,0 @@ |
@@ -11,2 +11,4 @@ """ | ||
| import logging | ||
| import sys, os | ||
| sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../")) | ||
| import lib | ||
@@ -13,0 +15,0 @@ |
15499
33.6%27
50%483
32.69%38
2.7%