The creation of WebView based applications ApiGateway AWS and AWS Lambda
Over the past year of existence in the vast network has accumulated a large number of tutorials on creating a serverless API on the basis of the subject. In this article I want to tell you about another use case of the API Gateway, which in particular can be used as a backend to a WebView of the app.
Be careful, a lot of screenshots!
Disclaimer:
the
To begin with, we have the simplest data model:
the
As a data source I decided to use S3 for clarity. Create a JSON file with contents like:
the
As the rendering engine templates, I'll use Python itself. Templates are HTML files that look like:
the
After you create a data bucket fill everything in with the appropriate prefixes (directories):

the
In the AWS console create a Lambda function using blueprint hello-world-python.

Used IAM roles, you must first add the policy to access S3.
the
Go to the API console and API Gateway creates the following:



Note the mapping template — he describes the event given to the lambda. Next, we describe the mapping and content-type for the given API Gateway data. Deploym two stage — production and development, we prescribe each stage variable lamdbdaAlias and is about API Gateway forget.

the
Next, return to the created lambda and add the following code:
the
After that published the lambda and create two alias to it — production and development:

All we got quite a working draft:

Video:
Github: aws webview
Article based on information from habrahabr.ru
Be careful, a lot of screenshots!
Disclaimer:
This is only a concept, so you should not consider it as work use. At the end of the attached link to github, if You don't want to go through all steps manually, and I recorded the video, so do not be surprised that many of the moments I will miss.
the
data Preparation
To begin with, we have the simplest data model:
the
User:
id = 'string'
name = 'string'
As a data source I decided to use S3 for clarity. Create a JSON file with contents like:
the
{
"id":"1",
"name":"George"
}
As the rendering engine templates, I'll use Python itself. Templates are HTML files that look like:
the
base.tpl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Base view</title>
</head>
<body>
{body}
</body>
</html>
user.tpl
<a href="#">{id} <span class="label label-success">{name}</span></a><br>
After you create a data bucket fill everything in with the appropriate prefixes (directories):

the
creating the function in AWS Lambda
In the AWS console create a Lambda function using blueprint hello-world-python.

Used IAM roles, you must first add the policy to access S3.
the
Creating an API
Go to the API console and API Gateway creates the following:



Note the mapping template — he describes the event given to the lambda. Next, we describe the mapping and content-type for the given API Gateway data. Deploym two stage — production and development, we prescribe each stage variable lamdbdaAlias and is about API Gateway forget.

the
AWS Lambda
Next, return to the created lambda and add the following code:
the
from __future__ import print_function
import json
import boto3
s3_client = boto3.client("s3")
def get_template(filename):
response = s3_client.get_object(
Bucket='config-data-bucket',
Key='template/{}'.format(filename)
)
return response['Body'].read()
def get_objects_list():
return [json.loads(
s3_client.get_object(
Bucket='config-data-bucket', Key=item['Key']
)['Body'].read()
) for item in s3_client.list_objects_v2(
Bucket='config-data-bucket', Prefix='data'
)['Contents'] if item['Key'].endswith('.json')]
def get_object(id):
response = s3_client.get_object(
Bucket='config-data-bucket',
Key='data/{}.json'.format(id)
)
return json.load(response['Body'])
def lambda_handler(event, context):
print "Event received"
resource = event['path_params']['resource']
method = event['http_method']
id = event['path_params'].get('id')
if resource == 'user' and method=="GET":
base_template = get_template('base.tpl')
user_template = get_template('user.tpl')
if id:
user = get_object(id)
return base_template.format(
body=user_template.format(id=user['id'], name=user['name'])
)
else:
users = get_objects_list()
return base_template.format(
body="".join(
user_template.format(id=user['id'], name=user['name']) for user
in users)
)
After that published the lambda and create two alias to it — production and development:

All we got quite a working draft:

Video:
Github: aws webview
Comments
Post a Comment