[英]How to store and use HTML templates in serverless application on AWS Lambda (using AWS SAM)?
Instead of 代替
(A) following a more traditional route for running a web application on AWS (eg, using Ruby/Sinatra or Python/Flask on AWS EC2 or Beanstalk), or (A)遵循在AWS上运行Web应用程序的更传统路线(例如,在AWS EC2或Beanstalk上使用Ruby / Sinatra或Python / Flask),或者
(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), (C)在S3中创建静态HTML和JavaScript文件以及在AWS Lambda中创建动态API端点(将JSON数据发送到那些通过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. 该设置将产生无服务器的Web应用程序,其中AWS Lambda函数提供服务器端(具有讽刺意味的是,我没有丢失)生成的HTML输出。
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. 在AWS Lambda函数的Python代码中,可以包含HTML代码片段,在函数中修改(填充数据),并将HTML作为
text/html
输出返回给客户端。 The drawback of that approach is that the HTML template is then 'embedded' in the Python code, and not external in a separate file. 这种方法的缺点是HTML模板然后“嵌入”在Python代码中,而不是外部在单独的文件中。
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? Q1:如何在代码包中的某处引用HTML模板文件 - 模板应该是包的一部分 - 由Python函数读取,并通过模板中的变量替换生成HMTL页面?
Q2: How can I specify / include / link to a set of HTML template files in my project using AWS Serverless Application Model (AWS SAM)? Q2:如何使用AWS无服务器应用程序模型(AWS SAM)指定/包含/链接到项目中的一组HTML模板文件?
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. 创建引用处理程序和事件资源的YAML配置文件,并放入
deployment
文件夹。
For Templating, use "Mustashe for Python" pystashe . 对于模板,使用“Mustashe for Python” pystashe 。
Create the parameterised HTML Template within your Python project/Virtualenv: 在Python项目/ Virtualenv中创建参数化HTML模板:
<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: 创建
data
对象以填充参数化模板:
{ "name": "Tom Thumb", "phone": "0123456789" }
Load the template
from the location in the project 从项目中的位置加载
template
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: 将呈现的html返回给客户端:
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. 压缩python代码,site-packages和html文件并放入
deployment
文件夹。
From the deployment
folder, package the SAM Project, which prepares and uploads to S3: 从
deployment
文件夹中打包SAM项目,该项目准备并上载到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: 从
deployment
文件夹中,将SAM项目部署到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... :) 为了记录,我更喜欢选项C,使用NodeJS ...... :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.