Instead of
(A) following a more traditional route for running a web application on AWS (e.g., using Ruby/Sinatra or Python/Flask on AWS EC2 or Beanstalk), or
(C) creating static HTML and JavaScript files in S3 and dynamic API endpoints in AWS Lambda (sending JSON data to those static web pages that use/interpret that data via JavaScript),
I want to reach a middle ground:
That setup will result in a serverless web application where the AWS Lambda functions deliver server-side (the irony is not lost on me) generated HTML output.
In Python code for AWS Lambda functions, it is possible to include HTML code snippets, modify that (populate with data) in the function, and return the HTML as text/html
output to the client. The drawback of that approach is that the HTML template is then 'embedded' in the Python code, and not external in a separate file.
Q1: How can I refer to a HTML template file somewhere in the code package - the template should be part of the package - have that read by the Python function, and generate the HMTL page by variable substitution in the template?
Q2: How can I specify / include / link to a set of HTML template files in my project using AWS Serverless Application Model (AWS SAM)?
Note that a serverless application is more than just a Lambda function—it can include additional resources such as APIs, databases, and event source mappings. You can use AWS SAM to define your serverless applications.
The declaration Transform: AWS::Serverless-2016-10-31 is required for AWS SAM template files. This declaration identifies an AWS CloudFormation template file as an AWS SAM template file. For more information about transforms, see Transform in the AWS CloudFormation User Guide.
I'm not sure where you are starting from here, so I'll start from the beginning.
Create the YAML Config file referencing the handlers and the Event Resources and put in a deployment
folder.
For Templating, use "Mustashe for Python" pystashe.
Create the parameterised HTML Template within your Python project/Virtualenv:
<html>
<head>
<title>Customer: {{name}}</title>
</head>
<body>
<div>
<h2>Customer Name: {{name}}</h2>
<h4>Phone Number: {{phone}}</h4>
</div>
</body>
</html>
Create the data
object to populate the parameterised template:
{
"name": "Tom Thumb",
"phone": "0123456789"
}
Load the template
from the location in the project
template = file('%s/mypath/template.html'%py_root).read()
Render the page from the data object:
myhtml = pystache.render(template, data)
Return the rendered html to the client:
response = {
"statusCode": 200,
"body": myhtml,
"headers": {
'Content-Type': 'text/html',
}
}
Zip the python code, site-packages, and html files and put in the deployment
folder.
From the deployment
folder, package the SAM Project, which prepares and uploads to S3:
aws cloudformation package --template-file myservice.yml --output-template-file deploy-myservice.yml --s3-bucket myserverless-deploy
From the deployment
folder, deploy the SAM Project to AWS:
aws cloudformation deploy --template-file deploy-myservice.yml --stack-name mycontext-myservice-dev --capabilities CAPABILITY_IAM
For the record, I prefer option C, with NodeJS... :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With