简体   繁体   English

如何在AWS Lambda上的无服务器应用程序中存储和使用HTML模板(使用AWS SAM)?

[英]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: 我想达成一个中间立场:

  • (B) create HTTP endpoints in AWS Lambda (eg, in Python) that read and use HTML templates for generating complete HTML responses to the client. (B)在AWS Lambda中创建HTTP端点(例如,在Python中),读取和使用HTML模板以生成对客户端的完整HTML响应。

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. 我不确定你从哪里开始,所以我将从头开始。

  1. Create the YAML Config file referencing the handlers and the Event Resources and put in a deployment folder. 创建引用处理程序和事件资源的YAML配置文件,并放入deployment文件夹。

  2. For Templating, use "Mustashe for Python" pystashe . 对于模板,使用“Mustashe for Python” pystashe

  3. 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> 
  4. Create the data object to populate the parameterised template: 创建data对象以填充参数化模板:

     { "name": "Tom Thumb", "phone": "0123456789" } 
  5. Load the template from the location in the project 从项目中的位置加载template

     template = file('%s/mypath/template.html'%py_root).read() 
  6. Render the page from the data object: 从数据对象渲染页面:

     myhtml = pystache.render(template, data) 
  7. Return the rendered html to the client: 将呈现的html返回给客户端:

     response = { "statusCode": 200, "body": myhtml, "headers": { 'Content-Type': 'text/html', } } 
  8. Zip the python code, site-packages, and html files and put in the deployment folder. 压缩python代码,site-packages和html文件并放入deployment文件夹。

  9. 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 
  10. 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.

相关问题 在 Jenkins 中使用 AWS SAM 部署 AWS Lambda - Deploying AWS Lambda using AWS SAM in Jenkins 如何在 S3 上存储大型 Python 依赖项(适用于无服务器的 AWS Lambda) - How to Store Large Python Dependencies on S3 (for AWS Lambda with Serverless) 使用无服务器框架为 AWS Lambda 构建和使用本地包 - Build and use local package for AWS Lambda using serverless framework 如何使用AWS Web API和Lambda验证无服务器Web请求? - How to authenticate serverless web request using AWS Web API and Lambda? 如何从 SAM 本地中的另一个 lambda 调用 AWS lambda? - How to invoke AWS lambda from another lambda within SAM local? 如何编写无服务器 AWS lambda function 将使用 wget 下载 linux 第三方应用程序,然后从该应用程序执行命令? - How to code a serverless AWS lambda function that will download a linux third party application using wget and then execute commands from that app? 使用 SAM 使用自定义 python 函数构建 AWS Lambda 层 - Building AWS Lambda layers with custom python functions using SAM 如何在 Lambda 中引用通过 SAM 创建的 AWS 资源? - How to reference an AWS resource created via SAM in a Lambda? 如何使用 AWS Lambda 层使用 Python? - How to use AWS Lambda layer using Python? 使用无服务器框架将 Python package 部署到 AWS lambda 时出错 - Error deploying Python package to AWS lambda using Serverless framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM