send emails with serverless and tem
Jump toUpdate content

Sending emails with Transactional Email and Serverless using Terraform

Reviewed on 30 October 2023Published on 27 April 2023
  • transactional-email
  • tem
  • serverless
  • emails
  • terraform

In this tutorial, you will learn how to send emails with Transactional Email and Serverless Functions using a Terraform configuration file that builds your entire infrastructure such as a Serverless Function and a website with a registration page hosted on an Object Storage bucket. It also allows you to configure SPF, DKIM and MX records on your sending domain.

Security & Identity (IAM):

You may need certain IAM permissions to carry out some actions described on this page. This means:

  • you are the Owner of the Scaleway Organization in which the actions will be carried out, or
  • you are an IAM user of the Organization, with a policy granting you the necessary permission sets

Clone the GitHub repository

We have created a GitHub repository that contains all the code you need to deploy your infrastructure. Open a terminal and clone the repository to your local machine using the following command:

git clone https://github.com/scaleway/tutorial-tem-serverless-terraform

Files and directories

In the tutorial-tem-serverless-terraform directory, you will find three folders: infra, signup-handler and super-product.

  • The infra folder holds main.tf, the Terraform configuration file used to deploy your infrastructure.
  • The signup-handler folder contains the files that will configure your function. The main.go file is the entry point for your function.
  • The super-product folder holds an index.html.tftpl file which is a Terraform template. This file contains an HTML template for the registration page of the website we are creating. This template allows you to automatically create and configure the SPF, MX and DKIM records needed for your domain, as well as a Serverless Function.

If you want to use a specific function, you can replace ${DISPATCHER_ENDPOINT} (line 74) in the index.html.tftpl file with your function’s endpoint.

Build and deploy your infrastructure

  1. Open a terminal and go to the tutorial-tem-serverless-terraform directory using the following command:

    cd tutorial-tem-serverless-terraform/infra
  2. Run the following command to download the dependencies and initialize Terraform:

    terraform init
  3. Run the following command to deploy your infrastructure:

    terraform apply
  4. Type your domain name when prompted to enter a value, and press Enter. You are then asked if you want to perform these actions:

    Plan: 14 to add, 0 to change, 0 to destroy.
    Changes to Outputs:
    + handler_url = (known after apply)
    + url = (known after apply)
    Do you want to perform these actions?
    Terraform will perform the actions described above.
    Only 'yes' will be accepted to approve.
  5. Type “yes”, then Enter. Terraform will generate your resources. The following output displays once your resources are generated:

    Apply complete! Resources: 14 added, 0 changed, 0 destroyed.
    handler_url = "https://temdemodct73sjr-tem-demo-signup-handler.functions.fnc.fr-par.scw.cloud"
    url = "https://tem-demo-super-product.s3-website.fr-par.scw.cloud"

Sending emails

  1. Click the link that displays next to the url output. You are redirected to your newly-created website.

  2. Enter your username in the Username field.

  3. Enter your recipient’s email address in the Email address field. For the purposes of this tutorial, we recommend you enter your own email address.

  4. Click Sign in.

  5. Log in to your email address. Your email titled Welcome to my cool product! displays in your mailbox.