High-Concept GOT story plot generator written for Apex-Lambda, demoing the awesome piping of lambda functions in the Terminal and proposing a logging and monitoring strategy for serverless with ELK.
Ever wanted to have Game of Thrones story plots being randomly generated by a Lambda set of functions? No? Well, that's too bad... this compelling need actually comes from a little experiment trying to answer 2 questions:
- Is it possible to have a coherent strategy for logging and monitoring Lambda functions?
- Is it possible to chain Lambda functions in the Terminal to provide a UNIX-like piping?
Both answers turned out to be yes.
For this project to work you'll need to:
- have Apex installed on your local machine
- have an AWS account with full access to:
- IAM roles
- Lambda functions
- CloudWatch
- have GoLang installed on your local machine
- have AWS CLI tools installed on your local machine with valid
~/.aws/config
and~/.aws/credentials
files - have json - JSON love for your command line installed
Optionally, if you want to see your logs in ELK :
- have an AWS account with further full access to:
- AWS ElasticSearch
- have Sense Chrome extension installed
Git clone yada yada
From within your GOPATH
:
# create the
mkdir $GOPATH/src/github.com/edoardo849 && cd "$_"
# clone the repo
git clone https://github.com/edoardo849/lot
# enter the folder
cd lot
Initialise Apex
# set your default profile for AWS and AWS region
source ./config
# run the setup
make setup
# Project name
Project name: lot
# Project description
Project description: Lambda of Thrones
I always find useful to setup the aws profile in the project.json file that apex will generate:
file: ./project.json
{
"profile":"default",
}
you may still need to source the ./config file for every new bash session though: that's a known bug in apex.
As a shorthand I've set up a Makefile
that takes care of everything. If you want to tweak the configuration have a look at it
# run the tests
make test
# deploy the code on AWS Lambda
make deploy
# invoke the functions
make plot
Something like this should come out: hopefully it will make great sense... but most of the time is just silly.
In order to check out the lambda functions that were created, run:
aws lambda list-functions --profile=YOUR-PROFILE --region=YOUR-REGION
_ _ _ __ _ _
| | __ _ _ __ ___ | |__ __| | __ _ ___ / _| | |_| |__ _ __ ___ _ __ ___ ___
| | / _` | '_ ` _ \| '_ \ / _` |/ _` | / _ \| |_ | __| '_ \| '__/ _ \| '_ \ / _ \/ __|
| |__| (_| | | | | | | |_) | (_| | (_| | | (_) | _| | |_| | | | | | (_) | | | | __/\__ \
|_____\__,_|_| |_| |_|_.__/ \__,_|\__,_| \___/|_| \__|_| |_|_| \___/|_| |_|\___||___/
What if Jon Snow drank all the wine of Tywin Lannister after winter came in the Eyre because when dead men come hunting... you think it matters who sits on the iron throne?
So apart from the obvious value of having a random plot generator... this project demonstrate that it's possible to chain lambda functions together in the Terminal to achieve a UNIX-like piping of outputs.
All the code is actually in the ./src
folder. The Makefile is just replicating the code inside the ./functions
folder and replacing a variable to correctly name each function. Each function is called after one of the Five Ws:
- who
- what
- when
- where
- why
Now this is of course not optimal... but remember that we just want to chain lambda functions together and to obtain their different logs.
When make plot
is called we are actually invoking each lambda function individually passing around the resulting json
. The first seed is:
{ "value": "What if" }
In order to try it out, you can copy-paste in the Terminal:
echo '{ "value": "What if" }' | apex invoke who
or be more creative and:
echo '{ "value": "The best character is" }' | apex invoke who | apex invoke why
The complete code from the Makefile is:
echo '{ "value": "What if" }' | \
apex invoke who | \
apex invoke what | \
apex invoke who | \
apex invoke when | \
apex invoke where | \
apex invoke why
You can of course play around and replace the seed data from the ./data
folder into whatever you like to produce a valid random phrase generator.
You can download the slides from Google Slides.