Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a html page from a AWS Python lambda function

I currently have a Python lambda function that is returning a JSON object. However rather then getting a white screen with JSON data I was wondering if there is a way to return a html webpage that returns instead of JSON? Currently the return data looks like

 return {
'statusCode': 200,
'headers': {},
'body': json.dumps({ 

                    'HOW-TO: ': prompt,    
                    'instanceId': instanceId,
                    'PUBLIC_IP_ADDRESS': publicIp,
                       'instanceRegion':Instance_region
})

But was curious if there is a way to return an HTML page instead?

like image 513
jelidens Avatar asked Apr 09 '26 02:04

jelidens


1 Answers

Yes you can, just define with AWS Python the right headers

"headers": {'Content-Type': 'text/html'}

as in the following example (please adjust the right Python identations)

def lambda_handler(event, context):

import json

longinformation = '''
<h1 style="color: #5e9ca0;">You can edit <span style="color: #2b2301;">this demo</span> text!</h1>
<h2 style="color: #2e6c80;">How to send HTML with AWS lambda in Python:</h2>
<p>Paste your documents in the visual editor on the left or your HTML code in the source editor in the right. <br />Edit any of the two areas and see the other changing in real time.&nbsp;</p>
'''

return {
    "statusCode": 200,
    "headers": {'Content-Type': 'text/html'},   #it really works by Hector Duran!
    "body": longinformation
}
like image 155
HECTOR DURAN LOPEZ VELARDE Avatar answered Apr 10 '26 15:04

HECTOR DURAN LOPEZ VELARDE