Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (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:

  • (B) create HTTP endpoints in AWS Lambda (e.g., in Python) that read and use HTML templates for generating complete HTML responses to the client.

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)?

like image 819
Jochem Schulenklopper Avatar asked Oct 22 '18 08:10

Jochem Schulenklopper


People also ask

Which resources can be specified in the AWS serverless application model AWS Sam template?

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.

Which section of the AWS serverless application model template is required and identifies an AWS CloudFormation template file as an AWS Sam template file?

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.


1 Answers

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.

  2. For Templating, use "Mustashe for Python" pystashe.

  3. 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>
    
  4. Create the data object to populate the parameterised template:

    {
      "name": "Tom Thumb",
      "phone": "0123456789"
    }
    
  5. Load the template from the location in the project

    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:

    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.

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

like image 106
Matt D Avatar answered Oct 25 '22 21:10

Matt D