Lambda based on Golang with AWS Simple Email Service (SES) and Terraform (IaC)

- 6 min read

Deploying a Go-based AWS Lambda Function for Seamless Email Delivery with AWS SES

Explore deploying a Go-based AWS Lambda function for email automation with SES, streamlined through Terraform. A paradigm shift in efficient cloud communication awaits.

Navigating the seas of cloud-based email communication can be daunting for developers. AWS Simple Email Service (SES) stands as a beacon for developers, offering a powerful platform to steer through the storm of outbound communications with confidence. This guide aims to chart a course through deploying a Go AWS Lambda function to handle email delivery via AWS SES, thereby setting a new course for simplified process management and enhanced manageability.

Introduction to AWS Lambda and SES for Go Developers

AWS Lambda is a serverless computing service that eliminates the need to manage servers, providing a scalable environment that only bills for the compute time consumed. When AWS Lambda is combined with AWS SES for email delivery, it forms an automated system that can handle the complex task of sending emails without the need for maintaining physical servers. For developers using Go, this integration means leveraging the language’s efficiency and straightforward syntax to manage email communications effectively.

Prerequisites

  • An active AWS account with the necessary permissions
  • A basic grasp of AWS services including Lambda, SES, and IAM
  • AWS Credentials with necessary permissions for Lambda and SES

Configure AWS credentials: Next, you need to configure your AWS credentials. You can do this through a shared credentials file or by setting environment variables. For the shared credentials file, run aws configure and enter the AWS_ACCESS_KEY_ID and the AWS_SECRET_ACCESS_KEY along with the AWS_REGION (make sure to have the AWS CLI installed beforehand). If you don't have the AWS CLI installed, you can alternatively create a file named ~/.aws/credentials with the following content:

[default] aws_access_key_id = YOUR_ACCESS_KEY aws_secret_access_key = YOUR_SECRET_KEY aws_region = YOUR_AWS_REGION

Replace YOUR_ACCESS_KEY and YOUR_SECRET_KEY with your actual AWS access key and secret key. If you prefer to use environment variables, you can set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in your shell.

Configuring AWS SES with Terraform

We'll use Terraform to define our infrastructure. This includes setting up SES domain identities and email sending policies. Here’s how the Terraform configurations will look:

# main.tf provider "aws" { region = var.aws_region } terraform { required_version = ">= 1.6.2" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.23.1" } } } variable "aws_region" { type = string default = "eu-central-1" } variable "verified_email" { type = string } resource "aws_ses_email_identity" "semplates_email_identity" { email = var.verified_email } resource "aws_ses_template" "semplates_demo_template" { name = "SEMPLATES_DEMO_TEMPLATE" subject = "Semplates Test Lambda Function" html = file("${path.module}/static/demo_template.html") }

The lambda.tf contains all policies for the lambda to be allowed to send a templated email as well as the lambda module itself:

# lambda.tf data "aws_iam_policy_document" "ses_send_templated_email_policy" { statement { effect = "Allow" actions = [ "ses:SendTemplatedEmail" ] resources = [ aws_ses_email_identity.semplates_email_identity.arn, aws_ses_template.semplates_demo_template.arn ] } statement { effect = "Allow" actions = [ "ses:GetTemplate", ] resources = [aws_ses_template.semplates_demo_template.arn] } } resource "aws_iam_policy" "ses_send_templated_email" { name = "SESSendTemplatedEmailPolicy" description = "Policy to allow SES sendTemplatedEmail only." policy = data.aws_iam_policy_document.ses_send_templated_email_policy.json } module "lambda_function" { source = "terraform-aws-modules/lambda/aws" function_name = "GoSesEmailSender" source_path = "${path.module}/../lambdas/go/" handler = "main" runtime = "go1.x" # ... (rest of the configuration) }

Adding the Go Code for the Lambda Function

Below you can find the source tree of our project. The binary file is built using the following command within the go directory:

docker run -e GOOS=linux -e GOARCH=amd64 -v "${PWD}:/app" -w /app golang:1.21.3 go build -ldflags="-s -w" -o bin/aws-lambda-go

In total, your folder structure should then look like this:

├── lambdas │ └── go │ ├── bin │ │ └── aws-lambda-go # Go binaray │ ├── main.go # Go script for Lambda function │ ├── go.sum │ ├── go.mod ├── terraform │ ├── lambda.tf # Terraform configuration for Lambda │ ├── ses.tf # Terraform configuration for SES │ ├── variables.tf # Terraform variable definitions │ ├── versions.tf # Terraform provider and version specifications │ └── static │ └── demo_template.html # HTML template for SES

Deploying the Lambda Function locally

To deploy the Go-based Lambda function you need to change directory to terraform. Create a new file called terraform.tfvars and add the following contents:

verified_email="jondoe@example.io" aws_region = "eu-central-1"

Then run terraform apply. You will be asked to review the plan and input yes if everything looks correct.

Testing Your Deployment

After deploying, you can send a test email by invoking the Lambda function with a test event. If everything is configured correctly, the Lambda function will send an email using the SES template and you will receive a successful response. Here are the steps to achieve this:

Step 1: After email verification, you will need to sign in to the AWS Management Console and check your AWS Lambda Functions. Select the one named PythonSesEmailSender. 

Step 2: Select the test tab and enter the following Event JSON:

{ "receiver_email": "<receiver-email>", "sender_email": "<sender-email>", "template_name": "SEMPLATES_DEMO_TEMPLATE", "placeholders": { "FIRST_NAME": "Jonathan", "LAST_NAME": "Doe" } }

Step 3: Hit the test button. The result should show:

{ "statusCode": 200, "body": { "message": "Email sent successfully!" } }

Destroy the setup

After you have tested everything successfully and want to remove the resources you have spun up, you can run terraform destroy. Again, you will need to confirm the destruction. After that, your account should be in the same state as before the apply.

Embracing Semplates for Template Management

Terraform provides a strong starting point for configuring AWS SES templates, but when it comes to their ongoing management, Semplates shines by offering a more dynamic and interactive approach. Relying solely on Terraform for template updates may introduce delays as modifications are dependent on the deployment cycle. This is where Semplates steps in to smooth out the workflow.

Semplates redefines template management by fostering a more cooperative environment. It empowers teams across the board, from design to marketing, allowing for direct engagement with the email template lifecycle.

Key Benefits of Integrating Semplates

  • Collaborative Dynamics: Semplates opens the door for multidisciplinary teams to participate actively in the creation and refining of email templates.
  • Boosted Efficiency: It liberates developers from the routine of template tweaking, reallocating their focus to more critical development endeavors.
  • User-Friendly Interface: The service boasts a graphical user interface, eliminating the complexity of the AWS CLI and script-based management.
  • On-the-Fly Email Testing: It provides facilities to conduct email tests right from the interface, thereby enhancing the testing workflow.

Transforming the Workflow Paradigm

The adoption of Semplates marks a departure from the traditional developer-driven approach, ushering in a more inclusive and efficient model. This modern methodology not only amplifies teamwork but also diminishes the resource expenditure tied to managing email templates, carving out a new era of workflow optimization.

Conclusion

Deploying a Go-based Lambda function for SES with Terraform provides a foundation for robust email communication. While Terraform handles the deployment efficiently, Semplates reshapes the management of SES templates, leading to a more collaborative and optimized workflow.

For a hands-on experience, clone the repository, set up your environment, and witness the smooth operation of deploying and managing your email functions with Go and AWS SES. Check the updated README in the GitHub repository for detailed instructions.

If you have any questions, please reach out to jonathan@semplates.io. We are happy to help you with your setup.

Create Great Email Templates on Amazon SES. Use Semplates.

Our discover plan is free forever. No credit card required.
Need more functionalities? You can upgrade anytime.

🍪

Our cookie policy

We use cookie technology to analyse the website traffic and page usage, improve the navigation experience and support our marketing efforts. If you accept, you grant us permission to store cookies on your device for those purposes only.
Please read our Data Privacy Policy for more information.

Accept all

Only necessary