2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

🌍 Terraform で AWS SES を設定するシンプルな手順

2
Last updated at Posted at 2025-02-09

📌 概要

Terraform を使って AWS Simple Email Service (SES) の設定をする方法を解説します

SES を使って安全にメールを送信するためのベストプラクティスとして、SPF・DKIM・DMARC の設定 も合わせて解説するよ💪

📌 事前に準備するもの

main.tf

main.tf の作成がまだの方は以下の記事を参考にファイルを作成してください。

route53のホストゾーン

route53のホストゾーンの作成がまだの方は以下の記事を参考にファイルを作成してください。

📌 ディレクトリ構成

terraform-project/
│── main.tf
│── modules
│   ├── route53
│   │   ├── main.tf
│   ├── ses
│   │   ├── main.tf

📌 ソースコード全体

maiin.tf

AWS のプロバイダーを設定し、SES の設定に必要なモジュールを読み込みます。

# AWS プロバイダーの設定
provider "aws" {
  region = "ap-northeast-1" # 東京リージョン
}

# Route 53 モジュールの読み込み
module "route53" {
  source = "./route53"
}

# SES(Simple Email Service)モジュールの読み込み
module "ses" {
  source         = "./ses"
  route53_zone_id = module.route53.route53_zone_id
}

route53/main.tf

Route 53 のホストゾーン を作成し、独自ドメインの DNS 設定を管理できるようにします。

# Route 53 のホストゾーンを作成
resource "aws_route53_zone" "zone" {
  name = "sandbox-terraform.com"
}

# 作成した Route 53 ホストゾーンの ID を出力
output "route53_zone_id" {
  value = aws_route53_zone.zone.zone_id
}

ses/main.tf

AWS Simple Email Service (SES) を使用して、独自ドメインの メール送信認証(DKIM / SPF / DMARC) を Terraform で自動設定します ✉️🚀

provider "aws" {
  region = "us-east-1" # AWS リージョンを指定
}

# SES ドメイン ID の作成
resource "aws_ses_domain_identity" "ses_domain" {
  domain = "example.com" # ここに設定するドメイン名を変更
}

# DKIM 設定
resource "aws_ses_domain_dkim" "ses_dkim" {
  domain = aws_ses_domain_identity.ses_domain.domain
}

# DKIM の CNAME レコードを Route 53 に登録
resource "aws_route53_record" "ses_dkim" {
  count   = 3
  zone_id = "Z123456ABCXYZ" # Route 53 のホストゾーン ID を指定
  name    = "${element(aws_ses_domain_dkim.ses_dkim.dkim_tokens, count.index)}._domainkey.example.com"
  type    = "CNAME"
  ttl     = "600"
  records = ["${element(aws_ses_domain_dkim.ses_dkim.dkim_tokens, count.index)}.dkim.amazonses.com"]
}

# SPF(TXT レコード)
resource "aws_route53_record" "ses_spf_txt" {
  zone_id = "Z123456ABCXYZ"
  name    = "example.com"
  type    = "TXT"
  ttl     = "600"
  records = ["v=spf1 include:amazonses.com ~all"]
}

# DMARC(TXT レコード)
resource "aws_route53_record" "ses_dmarc" {
  zone_id = "Z123456ABCXYZ"
  name    = "_dmarc.example.com"
  type    = "TXT"
  ttl     = "600"
  records = ["v=DMARC1; p=none; pct=100"]
}

📌 ses/main.tf の解説

SES のドメイン認証

SES でメールを送信するには、自分のドメインを AWS に登録する必要があります!

resource "aws_ses_domain_identity" "ses_domain" {
  domain = "sandbox-terraform.com"
}

✅ SES に 「このドメインを使ってメールを送ります!」と宣言
var.domain_name実際のドメイン名(例: example.com)に置き換える

DKIM(電子署名)を設定

DKIM(DomainKeys Identified Mail)は、送信メールの信頼性を高めるための署名機能 です

resource "aws_ses_domain_dkim" "ses_dkim" {
  domain = aws_ses_domain_identity.ses_domain.domain
}

DKIM を設定すると、受信側が「このメールは本物だ!」と判別しやすくなる
迷惑メール判定を回避しやすくなる

Route 53 に CNAME レコードを登録

resource "aws_route53_record" "ses_dkim" {
  count   = 3
  zone_id = var.route53_zone_id
  name    = "${element(aws_ses_domain_dkim.ses_dkim.dkim_tokens, count.index)}._domainkey.sandbox-terraform.com"
  type    = "CNAME"
  ttl     = "600"
  records = ["${element(aws_ses_domain_dkim.ses_dkim.dkim_tokens, count.index)}.dkim.amazonses.com"]
}

📧 SPF(送信許可リスト)を設定

SPF(Sender Policy Framework)は、なりすましメールを防ぐために「どのサーバーがこのドメインからメールを送れるか」を定める仕組み です

resource "aws_route53_record" "ses_spf_txt" {
  zone_id = var.route53_zone_id
  name    = "sandbox-terraform.com"
  type    = "TXT"
  ttl     = "600"
  records = ["v=spf1 include:amazonses.com ~all"]
}

v=spf1 include:amazonses.com ~allAWS SES からのメールだけ許可する設定
✅ SPF を設定すると、他のサーバーがこのドメインを使ってメールを送れなくなる!(なりすまし防止!)

DMARC(なりすまし対策)を設定

DMARC(Domain-based Message Authentication)は、SPF・DKIM に合格しなかったメールをどう処理するか決める仕組み です

resource "aws_route53_record" "ses_dmarc" {
  zone_id = var.route53_zone_id
  name    = "_dmarc.sandbox-terraform.com"
  type    = "TXT"
  ttl     = "600"
  records = ["v=DMARC1; p=none; pct=100"]
}

p=none → 何もしない(メールの処理に影響なし)
pct=100 → すべてのメールを DMARC でチェック

[参考リポジトリ]

📌 実行

準備ができたらコマンド実行しましょう!

  1. terraform init
  2. terraform plan
  3. terraform apply

Terraform関連のコマンドについては以下の記事を参考にしてください ☺️

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?