DEVELOPER BLOG

HOME > DEVELOPER BLOG > 【Introduction to Terraform】Creating Amazon SES with Terraform - PrismScaler

【Introduction to Terraform】Creating Amazon SES with Terraform - PrismScaler

1. Introduction

Hello! We are a writer team from Definer Inc. In this issue, you are wondering how to create an Amazon SES with Terraform. Let's take a look at the actual screens and resources to explain in detail.

2. Purpose/Use Cases

In this article, we will utilize the Terraform technology for the purpose of "coding cloud resources". It is a collection of information and practices that can be used as a reference when you want to create an AWS SES with Terraform in the IT field.

3. Creating Amazon SES with Terraform

Amazon SES stands for Simple Email Service. It is quickly configured and supports several email use cases, including transactional, marketing, and high-volume email communications.   In this article, we will create an Amazon SES with Terraform.   First, create a Terraform file like this Change ${domain name} to any domain name.
provider "aws" {
  region     = "ap-northeast-1"
}

resource "aws_route53_zone" "primary" {
  name = ${domain name}
}

resource "aws_ses_domain_identity" "ses" {
  domain = ${domain name}
}

resource "aws_route53_record" "ses_record" {
  zone_id = "${aws_route53_zone.primary.zone_id}"
  name    = "_amazonses.${aws_route53_zone.primary.name}"
  type    = "TXT"
  ttl     = "600"
  records = [aws_ses_domain_identity.ses.verification_token]
}

resource "aws_ses_domain_dkim" "dkim" {
  domain = ${domain name}
}

resource "aws_route53_record" "dkim_record" {
  count   = 3
  zone_id = "${aws_route53_zone.primary.zone_id}"
  name    = "${element(aws_ses_domain_dkim.dkim.dkim_tokens, count.index)}._domainkey.${aws_route53_zone.primary.name}"
  type    = "CNAME"
  ttl     = "600"
  records = ["${element(aws_ses_domain_dkim.dkim.dkim_tokens, count.index)}.dkim.amazonses.com"]
}                
This Terraform file is used to create the resource.

After running Terraform for a while, the status of SES Identities will change from Pending to Verified.

4. Try sending emails with Amazon SES

Once the creation is complete, we will use Amazon SES to actually send the emails.   We will use Lambda to execute the API for sending emails. As a note on creating Lambda, please grant an IAM role with SES privileges. Copy and paste the following code to create the Lambda.
import boto3
from botocore.exceptions import ClientError
 
    SENDER = ${Sender's email address}
    RECEIVER = ${Sender's email address}
    AWS_REGION = "ap-northeast-1"
    SUBJECT = "Email Subject"
    # non HTML body of the email
    BODY_TEXT = (
                "Text of email"
                )
                
    # The HTML body of the email.
    BODY_HTML = """<html>
    <head></head>
    <body>
    <p>Text of email</p>
    </body>
    </html>
        """ 

    CHARSET = "UTF-8"
    client = boto3.client('ses',region_name=AWS_REGION)
    try:
        response = client.send_email(
            Destination={
                'ToAddresses': [
                    RECEIVER,
                ],
            },
            Message={
                'Body': {
                    'Html': {
                        'Charset': CHARSET,
                        'Data': BODY_HTML,
                    },
                    'Text': {
                        'Charset': CHARSET,
                        'Data': BODY_TEXT,
                    },
                },
                'Subject': {
                    'Charset': CHARSET,
                    'Data': SUBJECT,
                },
            },
            Source=SENDER,
        )
    except ClientError as e:
        print(e.response['Error']['Message'])
        return f'Failed with {e}'
    else:
        print("Email sent! Message ID:"),
        print(response['MessageId'])
        return "Email Send DONE"                
Kick Lambda, and we've got confirmation that emails are flying!

5. Cited/Referenced Articles

6. About the proprietary solution "PrismScaler"

・PrismScaler is a web service that enables the construction of multi-cloud infrastructures such as AWS, Azure, and GCP in just three steps, without requiring development and operation. ・PrismScaler is a web service that enables multi-cloud infrastructure construction such as AWS, Azure, GCP, etc. in just 3 steps without development and operation. ・The solution is designed for a wide range of usage scenarios such as cloud infrastructure construction/cloud migration, cloud maintenance and operation, and cost optimization, and can easily realize more than several hundred high-quality general-purpose cloud infrastructures by appropriately combining IaaS and PaaS.  

7. Contact us

This article provides useful introductory information free of charge. For consultation and inquiries, please contact "Definer Inc".

8. Regarding Definer

・Definer Inc. provides one-stop solutions from upstream to downstream of IT. ・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition/design development/implementation, and maintenance and operation. ・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition, design development, implementation, maintenance, and operation. ・PrismScaler is a high-quality, rapid, "auto-configuration," "auto-monitoring," "problem detection," and "configuration visualization" for multi-cloud/IT infrastructure such as AWS, Azure, and GCP.