{"id":24732629,"url":"https://github.com/gonzalo123/random_serverless","last_synced_at":"2025-07-22T10:37:01.548Z","repository":{"id":37706193,"uuid":"188556347","full_name":"gonzalo123/random_serverless","owner":"gonzalo123","description":"Playing with lambda, serverless and Python","archived":false,"fork":false,"pushed_at":"2022-12-06T16:18:25.000Z","size":353,"stargazers_count":2,"open_issues_count":11,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-22T16:16:18.404Z","etag":null,"topics":["aws","flask","python","serverless","serverless-framework"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gonzalo123.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-25T11:11:22.000Z","updated_at":"2020-09-02T20:28:16.000Z","dependencies_parsed_at":"2023-01-24T08:31:30.057Z","dependency_job_id":null,"html_url":"https://github.com/gonzalo123/random_serverless","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gonzalo123/random_serverless","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalo123%2Frandom_serverless","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalo123%2Frandom_serverless/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalo123%2Frandom_serverless/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalo123%2Frandom_serverless/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gonzalo123","download_url":"https://codeload.github.com/gonzalo123/random_serverless/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalo123%2Frandom_serverless/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266476998,"owners_count":23935357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","flask","python","serverless","serverless-framework"],"created_at":"2025-01-27T17:53:11.946Z","updated_at":"2025-07-22T10:37:01.505Z","avatar_url":"https://github.com/gonzalo123.png","language":"Python","readme":"## Playing with lambda, serverless and Python\n\nCouple of weeks ago I attended to [serverless course](https://theserverlesscourse.com/). I've played with lambdas from time to time (basically when AWS forced me to use them) but without knowing exactly what I was doing. After this course I know how to work with the serverless framework and I understand better lambda world. Today I want to hack a little bit and create a simple Python service to obtain random numbers. Let's start\n\nWe don't need Flask to create lambdas but as I'm very comfortable with it so we'll use it here.\nBasically I follow the steps that I've read [here](https://medium.com/@Twistacz/flask-serverless-api-in-aws-lambda-the-easy-way-a445a8805028)\n\n```python\nfrom flask import Flask\n\napp = Flask(__name__)\n\n\n@app.route(\"/\", methods=[\"GET\"])\ndef hello():\n    return \"Hello from lambda\"\n\n\nif __name__ == '__main__':\n    app.run()\n```\n\nAnd serverless yaml to configure the service\n\n```yaml\nservice: random\n\nplugins:\n  - serverless-wsgi\n  - serverless-python-requirements\n  - serverless-pseudo-parameters\n\ncustom:\n  defaultRegion: eu-central-1\n  defaultStage: dev\n  wsgi:\n    app: app.app\n    packRequirements: false\n  pythonRequirements:\n    dockerizePip: non-linux\n\nprovider:\n  name: aws\n  runtime: python3.7\n  region: ${opt:region, self:custom.defaultRegion}\n  stage: ${opt:stage, self:custom.defaultStage}\n\nfunctions:\n  home:\n    handler: wsgi_handler.handler\n    events:\n      - http: GET /\n```\n\nWe're going to use serverless plugins. We need to install them:\n```\n\nnpx serverless plugin install -n serverless-wsgi\nnpx serverless plugin install -n serverless-python-requirements\nnpx serverless plugin install -n serverless-pseudo-parameters\n```\n\nAnd that's all. Our \"Hello world\" lambda service with Python and Flask is up and running.\nNow We're going to create a \"more complex\" service. We're going to return a random number with random.randint function. \nrandint requires two parameters: start, end. We're going to pass the end parameter to our service. The start value will be parameterized. I'll parameterize it only because I want to play with AWS's Parameter Store ([SSM](https://medium.com/@nqbao/how-to-use-aws-ssm-parameter-store-easily-in-python-94fda04fea84)). It's just an excuse.\n\nLet's start with the service:\n```python\nfrom random import randint\nfrom flask import Flask, jsonify\nimport boto3\nfrom ssm_parameter_store import SSMParameterStore\n\nimport os\nfrom dotenv import load_dotenv\n\ncurrent_dir = os.path.dirname(os.path.abspath(__file__))\nload_dotenv(dotenv_path=\"{}/.env\".format(current_dir))\n\napp = Flask(__name__)\n\napp.config.update(\n    STORE=SSMParameterStore(\n        prefix=\"{}/{}\".format(os.environ.get('ssm_prefix'), os.environ.get('stage')),\n        ssm_client=boto3.client('ssm', region_name=os.environ.get('region')),\n        ttl=int(os.environ.get('ssm_ttl'))\n    )\n)\n\n\n@app.route(\"/\", methods=[\"GET\"])\ndef hello():\n    return \"Hello from lambda\"\n\n\n@app.route(\"/random/\u003cint:to_int\u003e\", methods=[\"GET\"])\ndef get_random_quote(to_int):\n    from_int = app.config['STORE']['from_int']\n    return jsonify(randint(from_int, to_int))\n\n\nif __name__ == '__main__':\n    app.run()\n```\n\nNow the serverless configuration. I can use only one function, handling all routes and letting Flask do the job.\n```yaml\nfunctions:\n  app:\n    handler: wsgi_handler.handler\n    events:\n      - http: ANY /\n      - http: 'ANY {proxy+}'\n```\n\nBut in this example I want to create two different functions. Only for fun (and to use different role statements and different logs in cloudwatch).\n\n```yaml\nservice: random\n\nplugins:\n  - serverless-wsgi\n  - serverless-python-requirements\n  - serverless-pseudo-parameters\n  - serverless-iam-roles-per-function\n\ncustom:\n  defaultRegion: eu-central-1\n  defaultStage: dev\n  wsgi:\n    app: app.app\n    packRequirements: false\n  pythonRequirements:\n    dockerizePip: non-linux\n\nprovider:\n  name: aws\n  runtime: python3.7\n  region: ${opt:region, self:custom.defaultRegion}\n  stage: ${opt:stage, self:custom.defaultStage}\n  memorySize: 128\n  environment:\n    region: ${self:provider.region}\n    stage: ${self:provider.stage}\n\nfunctions:\n  app:\n    handler: wsgi_handler.handler\n    events:\n      - http: ANY /\n      - http: 'ANY {proxy+}'\n    iamRoleStatements:\n      - Effect: Allow\n        Action: ssm:DescribeParameters\n        Resource: arn:aws:ssm:${self:provider.region}:#{AWS::AccountId}:*\n      - Effect: Allow\n        Action: ssm:GetParameter\n        Resource: arn:aws:ssm:${self:provider.region}:#{AWS::AccountId}:parameter/random/*\n  home:\n    handler: wsgi_handler.handler\n    events:\n      - http: GET /\n```\n \nAnd that's all. \"npx serverless deploy\" and my random generator is running.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgonzalo123%2Frandom_serverless","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgonzalo123%2Frandom_serverless","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgonzalo123%2Frandom_serverless/lists"}