Skip to content

Commit

Permalink
feat(email-receiver): Lambda function receives SNS event (#262)
Browse files Browse the repository at this point in the history
The Lambda function now receives a SNS event containing the SES event.

Use the following code to get the SES event:

```ts
export async function handler(event: AWSLambda.SNSEvent): Promise<void> {
  const ses = JSON.parse(event.Records[0].Sns.Message) as AWSLambda.SESMessage;
}
```

This removes a intermediary Lambda function that had an EventBridge destination, simplifying the architecture.

BREAKING CHANGE: The Lambda function of an `EmailReceiver` now receives a SNS event.
  • Loading branch information
jogold authored Mar 15, 2024
1 parent eb73513 commit ea70d45
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 665 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .projen/files.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/email-receiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class MyStack extends Stack {
}
```

Your Lambda function conveniently receives a [`AWSLambda.SESMessage`](https://www.npmjs.com/package/@types/aws-lambda)
Your Lambda function receives a [`AWSLambda.SNSMessage`](https://www.npmjs.com/package/@types/aws-lambda)
event:

```ts
Expand All @@ -36,11 +36,13 @@ import { SESMessage } from 'cloudstructs';

const s3 = new S3({ apiVersion: '2006-03-01' });

export async function handler(event: AWSLambda.SESMessage): Promise<void> {
export async function handler(event: AWSLambda.SNSEvent): Promise<void> {
const ses = JSON.parse(event.Records[0].Sns.Message) as AWSLambda.SESMessage;

// Download email
const rawEmail = await s3.getObject({
Bucket: event.receipt.action.bucketName,
Key: event.receipt.action.objectKey,
Bucket: ses.receipt.action.bucketName,
Key: ses.receipt.action.objectKey,
}).promise();

// ... do something with email ...
Expand Down
11 changes: 1 addition & 10 deletions src/email-receiver/receiver.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Duration } from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as destinations from 'aws-cdk-lib/aws-lambda-destinations';
import * as logs from 'aws-cdk-lib/aws-logs';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as ses from 'aws-cdk-lib/aws-ses';
import * as actions from 'aws-cdk-lib/aws-ses-actions';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as subscriptions from 'aws-cdk-lib/aws-sns-subscriptions';
import { Construct } from 'constructs';
import { S3Function } from './s3-function';
import { WhitelistFunction } from './whitelist-function';

/**
Expand Down Expand Up @@ -97,13 +95,6 @@ export class EmailReceiver extends Construct {
topic,
}));

const s3Handler = new S3Function(this, 's3', {
logRetention: logs.RetentionDays.ONE_MONTH,
onSuccess: new destinations.LambdaDestination(props.function, {
responseOnly: true,
}),
});

topic.addSubscription(new subscriptions.LambdaSubscription(s3Handler)); // Notify
topic.addSubscription(new subscriptions.LambdaSubscription(props.function)); // Notify
}
}
26 changes: 0 additions & 26 deletions src/email-receiver/s3-function.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/email-receiver/s3.lambda.ts

This file was deleted.

Loading

0 comments on commit ea70d45

Please sign in to comment.