Skip to content

Commit 3790858

Browse files
jogoldrix0rrr
authored andcommitted
feat(ses): add constructs for email receiving (#1971)
Add constructs for email receiving.
1 parent a57c6d5 commit 3790858

15 files changed

+2424
-9
lines changed

packages/@aws-cdk/aws-ses/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,90 @@
11
## The CDK Construct Library for AWS Simple Email Service
22
This module is part of the [AWS Cloud Development Kit](https://github.com/awslabs/aws-cdk) project.
3+
4+
### Email receiving
5+
Create a receipt rule set with rules and actions:
6+
[example of setting up a receipt rule set](test/example.receiving.lit.ts)
7+
8+
Alternatively, rules can be added to a rule set:
9+
```ts
10+
const ruleSet = new ses.ReceiptRuleSet(this, 'RuleSet'):
11+
12+
const awsRule = ruleSet.addRule('Aws', {
13+
recipients: ['aws.com']
14+
});
15+
```
16+
17+
And actions to rules:
18+
```ts
19+
awsRule.addAction(
20+
new ses.ReceiptRuleSnsAction({
21+
topic
22+
});
23+
);
24+
```
25+
When using `addRule`, the new rule is added after the last added rule unless `after` is specified.
26+
27+
[More actions](test/integ.receipt.ts)
28+
29+
#### Drop spams
30+
A rule to drop spam can be added by setting `dropSpam` to `true`:
31+
32+
```ts
33+
new ses.ReceiptRuleSet(this, 'RuleSet', {
34+
dropSpam: true
35+
});
36+
```
37+
38+
This will add a rule at the top of the rule set with a Lambda action that stops processing messages that have at least one spam indicator. See [Lambda Function Examples](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-action-lambda-example-functions.html).
39+
40+
### Import and export receipt rule set and receipt rules
41+
Receipt rule sets and receipt rules can be exported:
42+
43+
```ts
44+
const ruleSet = new ReceiptRuleSet(this, 'RuleSet');
45+
const rule = ruleSet.addRule(this, 'Rule', {
46+
recipients: ['hello@mydomain.com']
47+
});
48+
49+
const ruleSetRef = ruleSet.export();
50+
const ruleRef = rule.export();
51+
```
52+
53+
And imported:
54+
```ts
55+
const importedRuleSet = ses.ReceiptRuleSet.import(this, 'ImportedRuleSet', ruleSetRef);
56+
57+
const importedRule = ses.ReceiptRule.import(this, 'ImportedRule', ruleRef);
58+
59+
const otherRule = ses.ReceiptRule.import(this, 'OtherRule', {
60+
name: 'other-rule'
61+
});
62+
63+
importedRuleSet.addRule('New', { // This rule is added after the imported rule
64+
after: importedRule,
65+
recipients: ['mydomain.com']
66+
});
67+
68+
importedRuleSet.addRule('Extra', { // Added after the 'New' rule
69+
recipients: ['extra.com']
70+
});
71+
```
72+
73+
### Receipt filter
74+
Create a receipt filter:
75+
```ts
76+
new ses.ReceiptFilter(this, 'Filter', {
77+
ip: '1.2.3.4/16' // Will be blocked
78+
})
79+
```
80+
81+
A white list filter is also available:
82+
```ts
83+
new ses.WhiteListReceiptFilter(this, 'WhiteList', {
84+
ips: [
85+
'10.0.0.0/16',
86+
'1.2.3.4/16',
87+
]
88+
});
89+
```
90+
This will first create a block all filter and then create allow filters for the listed ip addresses.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
export * from './receipt-rule-set';
2+
export * from './receipt-rule';
3+
export * from './receipt-rule-action';
4+
export * from './receipt-filter';
5+
16
// AWS::SES CloudFormation Resources:
27
export * from './ses.generated';
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import cdk = require('@aws-cdk/cdk');
2+
import { CfnReceiptFilter } from './ses.generated';
3+
4+
/**
5+
* The policy for the receipt filter.
6+
*/
7+
export enum ReceiptFilterPolicy {
8+
/**
9+
* Allow the ip address or range.
10+
*/
11+
Allow = 'Allow',
12+
13+
/**
14+
* Block the ip address or range.
15+
*/
16+
Block = 'Block'
17+
}
18+
19+
/**
20+
* Construction properties for a ReceiptFilter.
21+
*/
22+
export interface ReceiptFilterProps {
23+
/**
24+
* The name for the receipt filter.
25+
*
26+
* @default a CloudFormation generated name
27+
*/
28+
name?: string;
29+
30+
/**
31+
* The ip address or range to filter.
32+
*
33+
* @default 0.0.0.0/0
34+
*/
35+
ip?: string;
36+
37+
/**
38+
* The policy for the filter.
39+
*
40+
* @default Block
41+
*/
42+
policy?: ReceiptFilterPolicy;
43+
}
44+
45+
/**
46+
* A receipt filter. When instantiated without props, it creates a
47+
* block all receipt filter.
48+
*/
49+
export class ReceiptFilter extends cdk.Construct {
50+
constructor(scope: cdk.Construct, id: string, props?: ReceiptFilterProps) {
51+
super(scope, id);
52+
53+
new CfnReceiptFilter(this, 'Resource', {
54+
filter: {
55+
ipFilter: {
56+
cidr: (props && props.ip) || '0.0.0.0/0',
57+
policy: (props && props.policy) || ReceiptFilterPolicy.Block
58+
},
59+
name: props ? props.name : undefined
60+
}
61+
});
62+
}
63+
}
64+
65+
/**
66+
* Construction properties for a WhiteListReceiptFilter.
67+
*/
68+
export interface WhiteListReceiptFilterProps {
69+
/**
70+
* A list of ip addresses or ranges to white list.
71+
*/
72+
ips: string[];
73+
}
74+
75+
/**
76+
* A white list receipt filter.
77+
*/
78+
export class WhiteListReceiptFilter extends cdk.Construct {
79+
constructor(scope: cdk.Construct, id: string, props: WhiteListReceiptFilterProps) {
80+
super(scope, id);
81+
82+
new ReceiptFilter(this, 'BlockAll');
83+
84+
props.ips.forEach(ip => {
85+
new ReceiptFilter(this, `Allow${ip.replace(/[^\d]/g, '')}`, {
86+
ip,
87+
policy: ReceiptFilterPolicy.Allow
88+
});
89+
});
90+
}
91+
}

0 commit comments

Comments
 (0)