This demo uses Amazon Lambda and API Gateway to implement a serverless web service.
The repo contains two Lambda functions, fm_spekki_demo handles OAuth callback and saves user profile data into DB, while fm_spekki_db_count list all database content in table format.
This Lambda function performs those actions in order:
- Get OAuth callback params from API Gateway.
- Exchange
access_token
with passedcode
param. - Use
access_token
to get user profile. - Save user info into DynamoDB
- Create Lambda function in AWS console
- Select Python 2.7 as runtime.
- Make sure the role of this Lambda has permission to dynamodb.
- Enable API Gatway for this Lambda, default configuration is okay.
- Use default for other fields.
-
Configure API Gateway to pass through correct params
-
Get into API Gateway console
-
Find Method Request block in the GET action diagram
-
Add
code
andstate
to URL Query String Parameters section -
Back to diagram and get into Integration Request block
-
In Body Mapping Templates section, add new mapping template: * Content-Type:
application/json
* Put those code into template editor box:{ "state": "$input.params('state')", "code": "$input.params('code')" }
-
Select Deploy API from Actions drop down menu after changes saved
-
Clone code to local, and activate virtualenv
$ virtualenv venv -p `which python2.7`
$ . ./venv/bin/activate
$ pip install -r requirements.txt
- Prepare Google app
- Create web client in google developer console
- Copy conf.py.example to conf.py
- fill
client_id
,client_secret
andredirect_uri
-
Configure aws cli tool
-
Build and upload the deploy package by
make
command
This Lambda function gets all database record from related dynamo DB, and show it to user by HTML table.
This demo only contains one single file, you can copy-paste the content to Lambda function editor, or use the Makefile in the directory. The creatation of Lambda function is similar with previous app, except configuration for API Gateway.
Setup HTML Response in API Gateway
The response from Lambda function is a string, and the API Gateway needs to add a appropriate Content-Type header to help browser to parse it.
Browser the DB API's method execution diagram, click Integration Response block, then click status line, and find Body Mapping Templates. In the Content-Type section, add mapping template named text/html, then paste those conteint into the editor box:
#set($inputRoot = $input.path('$'))
$inputRoot.html_data .
The html_data
is the dict key returned from Lambda function.
After changes saved, click deploy API from action drop down menu.