Skip to content

wearetechnative/terraform-aws-html-form-action

Repository files navigation

TAHFA (Terraform AWS HTML Form Action)

TAHFA implements a simple form handler for plain html forms. Great for static websites. Sets up Lambda and API Gateway.

Features

  • Creates Lambda mailform POST endpoint
  • Optional Success URL redirect
  • Optional Failure URL redirect
  • Optional ALTCHA Spam Protection
  • Optional Mail reply template

How does it work

The TAHFA module creates an API Gateway POST resource and connects this to a lambda function. When a HTML form is submitted the API Gateway forwards the formdata to the lambda function and this sends the email.

Altcha Implementation

Since version 0.2 TAHFA features ALTCHA challege and validation funcions. ALTCHA is a free, open-source CAPTCHA alternative. ALTCHA uses a proof-of-work mechanism to protect your website, APIs, and online services from spam and unwanted content. ALTCHA is free, open-source, does not use cookies nor fingerprinting, does not track users, and is fully compliant with GDPR.

Requirements

  • A configured SES domain or SES emails available in the same account.

Example

Checkout the examples for complete implementations. The inline example below shows a simple usa case.

terraform

module "form_action_example_com" {
  source         = "TechNative-B-V/html-form-action/aws"

  name           = "example-com-form-action-handler"
  to_email       = "[email protected]"
  from_email     = "[email protected]"
}

output "form_action_example_com_url_for_form" {
  description = "Place this URL in your the action attribute of your form element."
  value = module.form_action_example_com.message_post_url
}

Simple html form

The form html looks like this.

<form action="https://XXXXXXXXXX.execute-api.eu-central-1.amazonaws.com/formpost/message" method="post">

  <!-- FORM CONFIGURATION -->
  <input type="hidden" name="_subject" value="Demo Form Submission">
  <input type="hidden" name="_success_url" value="http://example.com/form_success.html">

  <!-- FORM FIELDS -->
  <input placeholder="Full Name" type="text" name="full-name"><br>
  <input placeholder="Email" type="text" name="Email"><br>
  <textarea name="message" placeholder="Your message"></textarea><br>

  <input type="submit" value="send"></br>

</form>

Providers

Name Version
aws >= 4.0.0

Modules

Name Source Version
lambda_function_altchachallenge terraform-aws-modules/lambda/aws 3.3.1
lambda_function_formpost terraform-aws-modules/lambda/aws 3.3.1
resource_cors_altchachallenge mewa/apigateway-cors/aws 2.0.0
resource_cors_formpost mewa/apigateway-cors/aws 2.0.0

Resources

Name Type
aws_api_gateway_deployment.altcha_challenge resource
aws_api_gateway_deployment.formpost resource
aws_api_gateway_integration.message_altchachallenge resource
aws_api_gateway_integration.message_formpost resource
aws_api_gateway_method.message_altchachallenge resource
aws_api_gateway_method.message_formpost resource
aws_api_gateway_resource.message_altchachallenge resource
aws_api_gateway_resource.message_formpost resource
aws_api_gateway_rest_api.altcha_challenge resource
aws_api_gateway_rest_api.formpost resource
aws_lambda_permission.lambda_permission resource
aws_lambda_permission.lambda_permission_formpost resource

Inputs

Name Description Type Default Required
allowed_origin Which origin to allow submissions from. Use * when testing string "*" no
altcha_hmac_key HMAC Key to sign and validate Altcha Challenge string "change.me.now" no
from_email Receiving email address for forwarded messages, can also be configured in html form string "" no
name Name to use for function and api gateway string n/a yes
to_email 'From' email to use when forwarding a message, defaults to recipient email in the Lambda, can also be configured in html form string "" no
use_altcha Enable Altcha Spam protection bool n/a yes

Outputs

Name Description
message_altcha_challenge_url GET URL for Altcha Challenge requests
message_post_url POST URL for message requests