Skip to content

Commit 5cb123c

Browse files
committed
Allow email sending
1 parent 63746c7 commit 5cb123c

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

bin/aws-app.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ const env = {
1212

1313
const app = new cdk.App();
1414
const prefix = process.env.STACK_PREFIX ? `${process.env.STACK_PREFIX}-` : "";
15+
const domain = process.env.RECEIVING_EMAIL?.split("@")[1] || "";
1516

16-
new ReaderStack(app, `${prefix}Reader`, { env });
17+
new ReaderStack(app, `${prefix}Reader`, { domain, env });
1718

1819
if (process.env.SES_SKIP_DOMAIN_IDENTITY_CREATION !== "true") {
19-
new SesIdentityStack(app, `${prefix}Identity`, { env });
20+
new SesIdentityStack(app, `${prefix}Identity`, { domain, env });
2021
}

lib/stacks/reader-stack.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ import { getEnv } from "../utils/getEnv";
99

1010
const env = getEnv();
1111

12+
interface Props extends cdk.StackProps {
13+
domain: string;
14+
}
15+
1216
export class ReaderStack extends cdk.Stack {
1317
/**
1418
* Create AWS resources required for storing and taking action on emails received
1519
*/
16-
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
20+
constructor(scope: cdk.App, id: string, props: Props) {
1721
super(scope, id, props);
1822
const recipients = [env.RECEIVING_EMAIL];
1923

@@ -64,7 +68,14 @@ export class ReaderStack extends cdk.Stack {
6468
bucket,
6569
objectKeyPrefix: env.S3_EMAIL_OBJECT_PREFIX,
6670
});
71+
const domainIdentityArn = `arn:aws:ses:${this.region}:${this.account}:identity/${props.domain}`;
6772

6873
bucket.grantRead(lambdaS3Reader.lambda);
74+
lambdaS3Reader.lambda.addToRolePolicy(
75+
new cdk.aws_iam.PolicyStatement({
76+
actions: ["ses:SendEmail"],
77+
resources: [domainIdentityArn],
78+
})
79+
);
6980
}
7081
}

lib/stacks/ses-identity-stack.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ import { getEnv } from "../utils/getEnv";
99

1010
const env = getEnv();
1111

12+
interface Props extends cdk.StackProps {
13+
domain: string;
14+
}
15+
1216
export class SesIdentityStack extends cdk.Stack {
1317
/**
1418
* Create AWS SES domain identity so we can receive emails to the desired email address.
1519
* This domain will require verification using the DNS records output after deploying this stack.
1620
*/
17-
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
21+
constructor(scope: cdk.App, id: string, props: Props) {
1822
super(scope, id, props);
19-
const domain = env.RECEIVING_EMAIL.split("@")[1];
2023

2124
const domainIdentity = new ses.EmailIdentity(this, "Domain", {
22-
identity: ses.Identity.domain(domain),
25+
identity: ses.Identity.domain(props.domain),
2326
});
2427

2528
/**
@@ -35,7 +38,7 @@ Name: ${domainIdentity.dkimDnsTokenName3}\nValue: ${domainIdentity.dkimDnsTokenV
3538
new cdk.CfnOutput(this, "MX", {
3639
value: `\n
3740
MX record:\n
38-
Name: ${domain}\nValue: inbound-smtp.${env.CDK_DEPLOY_REGION}.amazonaws.com
41+
Name: ${props.domain}\nValue: inbound-smtp.${env.CDK_DEPLOY_REGION}.amazonaws.com
3942
Priority: 10\n`,
4043
});
4144
}

0 commit comments

Comments
 (0)