- Create a new lambda with the following settings:
- Function name - HTMX-example-lambda
- Runtime - Python 3.9
- Architecture - x86_64
- Under the
Code
tab, locate thelambda_function.py
file and edit it to be as follows:
import base64
from urllib.parse import parse_qs
def lambda_handler(event, context):
body_dec = base64.b64decode(event.get("body","")).decode('UTF-8')
parsed_body = parse_qs(body_dec)
name = parsed_body.get('name')
message = parsed_body.get('message')
return {
'headers': {'content-type': 'text/html'},
'statusCode': 200,
'body': f'''
<h2 class="text-center text-xl my-5">Hello {name} from a lambda!</h2>
<h3 class="text-center text-xl my-5">Your message: {message}</h3>
'''
}
- Click the ‘Deploy’ button
- Under the
Configuration
tab selectFunction URL
from the left navigation panel - Then click the
Create function URL
button - Create a Function URL with the following properties:
- Auth type -
NONE
- Configure cross-origin resource sharing (CORS) - Enabled
- Allow origin -
*
(allow all) - Add the following values under “Expose headers”
- access-control-allow-origin
- access-control-allow-methods
- access-control-allow-headers
- Add the following values under “Allow headers”
- hx-current-url
- hx-request
- Select the following values under “Allow methods”
- POST
- Click “Save"
- In the main menu, look for the
Function URL
and open it in a new tab, you should see:
Hello None from a lambda!
Your message: None
- Edit the
index.html
file and set theform.hx-post
attribute to your lambda’s URL - Drag
index.html
to your browser - Enter values into the
Name
andMessage
fields and click “Submit” - Observe that the form is replaced by:
Hello <your name> from a lambda!
Your message: <your message>