Skip to content

Commit abff678

Browse files
added domain verification
1 parent f572e07 commit abff678

File tree

5 files changed

+80
-47
lines changed

5 files changed

+80
-47
lines changed

main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ locals {
3030
for key, project_info in local.all_projects : key => project_info if(contains(project_info.channels, "email"))
3131
}
3232

33+
projects_need_domain_verification = {
34+
for key, project_info in local.projects_need_email : key => project_info if(project_info.verify_domain_identity)
35+
}
36+
3337
}

output.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
# outputs
22
output "domain_identities" {
33
value = aws_ses_domain_identity.domain
44
}

ses-identity.tf renamed to ses-identity-domain.tf

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ locals {
55

66
valid_domains = [for p in local.projects_need_email : split("@", p.from_email)[1]]
77
domain_identities = toset(distinct(local.valid_domains))
8+
9+
valid_domains_to_verify = [for p in local.projects_need_domain_verification : split("@", p.from_email)[1]]
10+
domains_to_verify = toset(distinct(local.valid_domains_to_verify))
811
}
912

1013
# create all Domain identities
@@ -14,56 +17,38 @@ resource "aws_ses_domain_identity" "domain" {
1417
domain = each.value
1518
}
1619

17-
# policy for Domain identities
18-
data "aws_iam_policy_document" "domain" {
19-
for_each = aws_ses_domain_identity.domain
20-
21-
statement {
22-
actions = ["ses:*"]
23-
resources = [aws_ses_domain_identity.domain[each.key].arn]
24-
25-
principals {
26-
type = "Service"
27-
identifiers = ["pinpoint.amazonaws.com"]
28-
}
29-
30-
condition {
31-
test = "StringEquals"
32-
values = [data.aws_caller_identity.current.account_id]
33-
variable = "aws:SourceAccount"
34-
}
20+
# retrieve Hosted Zone using Domain Name
21+
data "aws_route53_zone" "by_name" {
22+
for_each = local.domains_to_verify
3523

36-
condition {
37-
test = "StringLike"
38-
values = ["arn:aws:mobiletargeting:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:apps/*"]
39-
variable = "aws:SourceArn"
40-
}
41-
}
24+
name = each.key
4225
}
4326

44-
resource "aws_ses_identity_policy" "domain" {
45-
for_each = aws_ses_domain_identity.domain
27+
resource "aws_route53_record" "verification" {
28+
for_each = data.aws_route53_zone.by_name
4629

47-
identity = each.value.arn
48-
name = "${replace(replace(each.value.id, "@", "_"), ".", "_")}-identity-policy"
49-
policy = data.aws_iam_policy_document.domain[each.key].json
30+
zone_id = each.value.zone_id
31+
name = "_amazonses.${each.value.id}"
32+
type = "TXT"
33+
ttl = "600"
34+
# records = [each.value.verification_token]
35+
records = [aws_ses_domain_identity.domain[each.key].verification_token]
5036
}
5137

52-
# create all Email identities
53-
resource "aws_ses_email_identity" "email" {
54-
for_each = local.email_identities
38+
resource "aws_ses_domain_identity_verification" "example_verification" {
39+
for_each = data.aws_route53_zone.by_name
5540

56-
email = each.value
41+
domain = aws_ses_domain_identity.domain[each.key].id
42+
depends_on = [aws_route53_record.verification]
5743
}
5844

59-
# policy for Email identities
60-
data "aws_iam_policy_document" "email" {
61-
for_each = aws_ses_email_identity.email
45+
# policy for Domain identities
46+
data "aws_iam_policy_document" "domain" {
47+
for_each = aws_ses_domain_identity.domain
6248

6349
statement {
6450
actions = ["ses:*"]
65-
resources = [aws_ses_email_identity.email[each.key].arn]
66-
51+
resources = [aws_ses_domain_identity.domain[each.key].arn]
6752

6853
principals {
6954
type = "Service"
@@ -84,10 +69,10 @@ data "aws_iam_policy_document" "email" {
8469
}
8570
}
8671

87-
resource "aws_ses_identity_policy" "email" {
88-
for_each = aws_ses_email_identity.email
72+
resource "aws_ses_identity_policy" "domain" {
73+
for_each = aws_ses_domain_identity.domain
8974

9075
identity = each.value.arn
9176
name = "${replace(replace(each.value.id, "@", "_"), ".", "_")}-identity-policy"
92-
policy = data.aws_iam_policy_document.email[each.key].json
77+
policy = data.aws_iam_policy_document.domain[each.key].json
9378
}

ses-identity-email.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
# create all Email identities
3+
resource "aws_ses_email_identity" "email" {
4+
for_each = local.email_identities
5+
6+
email = each.value
7+
}
8+
9+
# policy for Email identities
10+
data "aws_iam_policy_document" "email" {
11+
for_each = aws_ses_email_identity.email
12+
13+
statement {
14+
actions = ["ses:*"]
15+
resources = [aws_ses_email_identity.email[each.key].arn]
16+
17+
18+
principals {
19+
type = "Service"
20+
identifiers = ["pinpoint.amazonaws.com"]
21+
}
22+
23+
condition {
24+
test = "StringEquals"
25+
values = [data.aws_caller_identity.current.account_id]
26+
variable = "aws:SourceAccount"
27+
}
28+
29+
condition {
30+
test = "StringLike"
31+
values = ["arn:aws:mobiletargeting:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:apps/*"]
32+
variable = "aws:SourceArn"
33+
}
34+
}
35+
}
36+
37+
resource "aws_ses_identity_policy" "email" {
38+
for_each = aws_ses_email_identity.email
39+
40+
identity = each.value.arn
41+
name = "${replace(replace(each.value.id, "@", "_"), ".", "_")}-identity-policy"
42+
policy = data.aws_iam_policy_document.email[each.key].json
43+
}

variables.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
variable "api_name" {
22
type = string
33
default = "messaging"
4-
description = "Name for your API. Default is Messaging"
4+
description = "Name for your API. Default value is 'Messaging'"
55
# validation {
66
# condition = (can(regex("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\\.)*([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])$", var.api_name))
77
# && !strcontains(var.api_name, "..")
@@ -21,10 +21,11 @@ variable "api_version" {
2121

2222
variable "projects" {
2323
type = map(object({
24-
channels = list(string)
25-
from_email = string
26-
to_emails = list(string)
27-
need_api_endpoint = bool
24+
channels = list(string)
25+
from_email = string
26+
to_emails = list(string)
27+
need_api_endpoint = bool
28+
verify_domain_identity = bool
2829
}))
2930
description = "List of Projects to build Messaging channels (Project details as Map(Object('Project-Name'={channels=['email','sms'], from_email='[email protected]', to_emails=['[email protected]','[email protected]'] need_api_endpoint=true}))"
3031
}

0 commit comments

Comments
 (0)