Skip to content

Instantly share code, notes, and snippets.

@sscovil
Last active November 28, 2022 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sscovil/f8f9a505e3663f9812f5cb36c941ff40 to your computer and use it in GitHub Desktop.
Save sscovil/f8f9a505e3663f9812f5cb36c941ff40 to your computer and use it in GitHub Desktop.
Terraform CDK Python example of AWS SES configuration
#!/usr/bin/env python
from cdktf import App, Fn, TerraformStack
from cdktf_cdktf_provider_aws.provider import AwsProvider
from cdktf_cdktf_provider_aws.route53_record import Route53Record
from cdktf_cdktf_provider_aws.route53_zone import Route53Zone
from cdktf_cdktf_provider_aws.ses_configuration_set import \
SesConfigurationSet, \
SesConfigurationSetDeliveryOptions
from cdktf_cdktf_provider_aws.ses_domain_dkim import SesDomainDkim
from cdktf_cdktf_provider_aws.ses_domain_identity import SesDomainIdentity
from cdktf_cdktf_provider_aws.ses_domain_mail_from import SesDomainMailFrom
from constructs import Construct
class MailerStack(TerraformStack):
def __init__(self, scope: Construct, ns: str, domain: str, region: str):
super().__init__(scope, ns)
AwsProvider(self, "aws", region=region)
zone = Route53Zone(self, "hosted_zone", name=domain)
default_configuration_set = SesConfigurationSet(
self,
"default_config_set",
delivery_options=SesConfigurationSetDeliveryOptions(
tls_policy="Require", # "Optional" or "Require"
),
name="default_config_set",
reputation_metrics_enabled=True,
sending_enabled=True,
)
identity = SesDomainIdentity(self, "domain_identity", domain=domain)
dkim = SesDomainDkim(self, "domain_dkim", domain=domain)
for i in range(3):
token = Fn.element(dkim.dkim_tokens, i)
Route53Record(
self,
f"domain_dkim_record_{i}",
name=f"{token}._domainkey",
records=[f"{token}.dkim.amazonses.com"],
ttl=600,
type="CNAME",
zone_id=zone.zone_id,
)
mailer = SesDomainMailFrom(
self,
"domain_mail_from",
depends_on=[identity, dkim],
domain=domain,
mail_from_domain=f"mailer.{domain}",
)
Route53Record(
self,
"ses_mailer_mx_record",
name=mailer.mail_from_domain,
records=[f"10 feedback-smtp.{region}.amazonses.com"],
ttl=600,
type="MX",
zone_id=zone.zone_id,
)
Route53Record(
self,
"ses_mailer_txt_record",
name=mailer.mail_from_domain,
records=["v=spf1 include:amazonses.com -all"],
ttl=600,
type="TXT",
zone_id=zone.zone_id,
)
app = App()
MailerStack(app, "example", domain="example.com", region="us-east-1")
app.synth()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment