AWS Lambda colleague name generator

Time passed since he started receiving daily *slav names upon arrival in the office… Why? Noone remembers exactly, but after a few days, it was obvious that the process needed to be automated.

Good friend of mine created music “artist” name generator and shared it with the world as a legendary Polumenta generator. He open-sourced it, so shameless code stealing was obviously the way to go.

Script was Python-ized with minor adjustment of adding another ponderized last character, before necessary “слав” ending, based on distribution of actual names of it’s kind. Base64 from indexed 1-bit palette image was supplied along – all ready for FaaS.

AWS Lambda (colleague name generator)

Creating a λ function is trivial – from given hello-world example, without initial trigger, apply code gently –

AWS Lambda colleague name generator

Based on above lambda_handler function name – and the fact we’re using Python – here is the overview of configuration options –

Runtime: Python 3.6
Handler: lambda_function.lambda_handler
Role: Choose an existing role
Existing role: service-role/polygon-shiftoslav
Description: Generate Shift-O-Slav name

Memory (MB): 128 MB
Timeout: 3s
DLQ Resource: None
VPC: no VPC
Enable active tracing: [ ]
KMS key: (default) aws/lambda

In order to verify that function returns correct JSON with name and UTF-8 chars, use empty curly brackets for Input test event and validate output –

{
  "name": "Гомеслав"
}

AWS API Gateway

In order to have an URL to invoke the function from, GET request method integration is needed. By default API Gateway returns JSON, so making it present HTML requires a bit of extra work.

Three out of four pillars of non-intuitive method execution setup (Method Re{quest,sponse}, Integration Re{quest,sponse}) need to be tweaked in order to achieve that.

AWS Lambda colleague name generator

Changes on the Integration Request are basic –

Integration type: Lambda Function
Use Lambda Proxy integration: [ ]
Invoke with caller credentials: [ ]

Body Mapping Templates:
  Request body passthrough:
    When no template matches the request Content-Type header

Integration Response, however, demands more alteration. Within HTTP 200 response set Content-Type and define body mapping so that function return is parsed as part of HTML –

Lambda Error Regex: default
Content handling: Passthrough

Header Mappings:
  Content-Type:
    'text/html; charset=utf-8'

Body Mapping Templates:
  application/json:
    #set($inputRoot = $input.path('$'))
    <!doctype html>
        <meta charset=utf-8>
        <head><title>ShiftOSlav</title></head>
        <body><center>
            <p><img src="data:image/png;base64, iVBORw0KGgoAAAAN [ ... ]
                         5wAAAABJRU5ErkJggg==" alt="Shift-O-Slav" />
            <p><h1>$inputRoot.name</h1>
            </center></body></html>

Content-Type is entered with single quotes and all JSON fields from lambda are accessed via inputRoot._

AWS Lambda colleague name generator

Last step before hitting Actions / Deploy API is adding “Content-Type” within Method Response and modifying response body for HTTP 200 –

Response Headers for 200:
  Name: Content-Type

Response Body for 200:
  Content type: application/json
  Models: Empty

Shift-O-Slav

Entry point for the function can now be found under Invoke URL on the Stages editor, after API has been succesfully deployed. All that’s left is to enjoy fresh FaaS Shift-O-Slav name generator.

AWS Lambda colleague name generator

razvijamo software

Programiramo, debug-ujemo, nerviramo se, borimo se i najčešće pokidamo.

Radimo za iGame, Unibet i Relax Gaming. Sve tri kompanije među najvećim su igračima u industriji igara na sreću! Ako si iskusan programer ili super-talentovan klinac i u fazonu da radiš sa nama - klik ovde.

2017-08-31T09:20:32+00:00