@vyriy/stack 0.5.6 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +11 -1
- package/ses/index.d.ts +28 -0
- package/ses/index.js +88 -0
package/README.md
CHANGED
|
@@ -35,6 +35,8 @@ The `aws-cdk-lib` package is listed because CDK apps and bin entrypoints use CDK
|
|
|
35
35
|
- `route53.createARecord(scope, id, props)` creates a Route 53 A record.
|
|
36
36
|
- `route53.createCloudFrontTarget(distribution)` creates a Route 53 alias target for CloudFront.
|
|
37
37
|
- `acm.createCertificate(scope, id, props)` creates an ACM certificate.
|
|
38
|
+
- `ses.createRawEmailBucket(scope, id, props?)` creates a secure retained S3 bucket for raw SES emails.
|
|
39
|
+
- `ses.createEmailReceiving(scope, id, props)` creates an SES domain identity, receipt rule set, receipt rule, raw email bucket permissions, S3-to-Lambda notification, and optional SSM parameter read access.
|
|
38
40
|
- `deployment.createBucketDeployment(scope, id, props)` deploys files to S3 with a `512` MB default memory limit.
|
|
39
41
|
- `deployment.createHtmlCacheControl()` creates cache-control headers for HTML files that should revalidate before reuse.
|
|
40
42
|
- `deployment.createImmutableCacheControl(days?)` creates long-lived immutable cache-control headers.
|
|
@@ -150,6 +152,61 @@ stack(
|
|
|
150
152
|
);
|
|
151
153
|
```
|
|
152
154
|
|
|
155
|
+
## SES Email Receiving Example
|
|
156
|
+
|
|
157
|
+
This example configures a domain to receive email through SES, store raw MIME messages in S3, and invoke a Lambda for application-specific processing such as sending a Telegram notification.
|
|
158
|
+
|
|
159
|
+
SES inbound receiving is region-limited, so deploy this in a region that supports SES email receiving and point your domain MX record to that regional SES inbound endpoint.
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
import { CfnOutput, Stack, type StackProps } from 'aws-cdk-lib';
|
|
163
|
+
import type { Construct } from 'constructs';
|
|
164
|
+
|
|
165
|
+
import { stack } from '@vyriy/cdk';
|
|
166
|
+
import { lambda, ses } from '@vyriy/stack';
|
|
167
|
+
import { path } from '@vyriy/path';
|
|
168
|
+
|
|
169
|
+
stack(
|
|
170
|
+
class EmailStack extends Stack {
|
|
171
|
+
constructor(scope: Construct, id: string, props: StackProps & { env: { account: string; region: string } }) {
|
|
172
|
+
super(scope, id, props);
|
|
173
|
+
|
|
174
|
+
const domainName = 'site.com';
|
|
175
|
+
const recipient = `consulting@${domainName}`;
|
|
176
|
+
const processor = lambda.createLambda(this, 'EmailProcessor', {
|
|
177
|
+
code: lambda.Code.fromAsset(path('dist', 'email-processor')),
|
|
178
|
+
description: `Processes raw emails sent to ${recipient}`,
|
|
179
|
+
environment: {
|
|
180
|
+
RAW_EMAIL_PREFIX: 'incoming/',
|
|
181
|
+
TELEGRAM_BOT_TOKEN_PARAMETER_NAME: '/site/consulting-email/telegram/bot-token',
|
|
182
|
+
TELEGRAM_CHAT_ID_PARAMETER_NAME: '/site/consulting-email/telegram/chat-id',
|
|
183
|
+
},
|
|
184
|
+
handler: 'index.handler',
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const emailReceiving = ses.createEmailReceiving(this, 'ConsultingEmail', {
|
|
188
|
+
domainName,
|
|
189
|
+
processor,
|
|
190
|
+
rawEmailPrefix: 'incoming/',
|
|
191
|
+
receiptRuleName: 'consulting',
|
|
192
|
+
receiptRuleSetName: 'site-consulting-email',
|
|
193
|
+
recipients: [recipient],
|
|
194
|
+
ssmParameterNames: [
|
|
195
|
+
'/site/consulting-email/telegram/bot-token',
|
|
196
|
+
'/site/consulting-email/telegram/chat-id',
|
|
197
|
+
],
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
processor.addEnvironment('RAW_EMAIL_BUCKET_NAME', emailReceiving.emailBucket.bucketName);
|
|
201
|
+
|
|
202
|
+
new CfnOutput(this, 'RawEmailBucketName', { value: emailReceiving.emailBucket.bucketName });
|
|
203
|
+
new CfnOutput(this, 'ConsultingEmail', { value: recipient });
|
|
204
|
+
new CfnOutput(this, 'EmailReceiptRuleSetName', { value: 'site-consulting-email' });
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
);
|
|
208
|
+
```
|
|
209
|
+
|
|
153
210
|
## API Gateway Lambda Example
|
|
154
211
|
|
|
155
212
|
This example wires a Lambda-backed REST API to an API Gateway custom domain:
|
package/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export * as ecs from './ecs/index.js';
|
|
|
9
9
|
export * as lambda from './lambda/index.js';
|
|
10
10
|
export * as route53 from './route53/index.js';
|
|
11
11
|
export * as s3 from './s3/index.js';
|
|
12
|
+
export * as ses from './ses/index.js';
|
|
12
13
|
export * as sns from './sns/index.js';
|
|
13
14
|
export * as sqs from './sqs/index.js';
|
|
14
15
|
export * as ssm from './ssm/index.js';
|
package/index.js
CHANGED
|
@@ -9,6 +9,7 @@ export * as ecs from './ecs/index.js';
|
|
|
9
9
|
export * as lambda from './lambda/index.js';
|
|
10
10
|
export * as route53 from './route53/index.js';
|
|
11
11
|
export * as s3 from './s3/index.js';
|
|
12
|
+
export * as ses from './ses/index.js';
|
|
12
13
|
export * as sns from './sns/index.js';
|
|
13
14
|
export * as sqs from './sqs/index.js';
|
|
14
15
|
export * as ssm from './ssm/index.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vyriy/stack",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "AWS CDK stack helpers for Vyriy infrastructure",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"dependencies": {
|
|
@@ -142,6 +142,16 @@
|
|
|
142
142
|
"import": "./s3/index.js",
|
|
143
143
|
"default": "./s3/index.js"
|
|
144
144
|
},
|
|
145
|
+
"./ses/index": {
|
|
146
|
+
"types": "./ses/index.d.ts",
|
|
147
|
+
"import": "./ses/index.js",
|
|
148
|
+
"default": "./ses/index.js"
|
|
149
|
+
},
|
|
150
|
+
"./ses/index.js": {
|
|
151
|
+
"types": "./ses/index.d.ts",
|
|
152
|
+
"import": "./ses/index.js",
|
|
153
|
+
"default": "./ses/index.js"
|
|
154
|
+
},
|
|
145
155
|
"./sns/index": {
|
|
146
156
|
"types": "./sns/index.d.ts",
|
|
147
157
|
"import": "./sns/index.js",
|
package/ses/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Construct } from 'constructs';
|
|
2
|
+
import { IFunction } from 'aws-cdk-lib/aws-lambda';
|
|
3
|
+
import { Bucket, BucketProps, IBucket } from 'aws-cdk-lib/aws-s3';
|
|
4
|
+
import { CfnEmailIdentity, CfnReceiptRule, CfnReceiptRuleSet } from 'aws-cdk-lib/aws-ses';
|
|
5
|
+
export type ReceiptRuleTlsPolicy = 'Optional' | 'Require';
|
|
6
|
+
export type CreateRawEmailBucketProps = BucketProps;
|
|
7
|
+
export type CreateEmailReceivingProps = {
|
|
8
|
+
domainName: string;
|
|
9
|
+
emailBucket?: IBucket;
|
|
10
|
+
emailBucketProps?: CreateRawEmailBucketProps;
|
|
11
|
+
receiptRuleName: string;
|
|
12
|
+
receiptRuleSetName: string;
|
|
13
|
+
recipients: string[];
|
|
14
|
+
processor: IFunction;
|
|
15
|
+
rawEmailPrefix?: string;
|
|
16
|
+
scanEnabled?: boolean;
|
|
17
|
+
ssmParameterNames?: string[];
|
|
18
|
+
tlsPolicy?: ReceiptRuleTlsPolicy;
|
|
19
|
+
};
|
|
20
|
+
export type CreateEmailReceivingResult = {
|
|
21
|
+
emailBucket: IBucket;
|
|
22
|
+
emailIdentity: CfnEmailIdentity;
|
|
23
|
+
receiptRule: CfnReceiptRule;
|
|
24
|
+
receiptRuleSet: CfnReceiptRuleSet;
|
|
25
|
+
};
|
|
26
|
+
export { CfnEmailIdentity, CfnReceiptRule, CfnReceiptRuleSet } from 'aws-cdk-lib/aws-ses';
|
|
27
|
+
export declare const createRawEmailBucket: (scope: Construct, id: string, props?: CreateRawEmailBucketProps) => Bucket;
|
|
28
|
+
export declare const createEmailReceiving: (scope: Construct, id: string, props: CreateEmailReceivingProps) => CreateEmailReceivingResult;
|
package/ses/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ArnFormat, RemovalPolicy, Stack } from 'aws-cdk-lib';
|
|
2
|
+
import { Effect, PolicyStatement, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
|
|
3
|
+
import { LambdaDestination } from 'aws-cdk-lib/aws-s3-notifications';
|
|
4
|
+
import { BlockPublicAccess, Bucket, BucketEncryption, EventType } from 'aws-cdk-lib/aws-s3';
|
|
5
|
+
import { CfnEmailIdentity, CfnReceiptRule, CfnReceiptRuleSet } from 'aws-cdk-lib/aws-ses';
|
|
6
|
+
export { CfnEmailIdentity, CfnReceiptRule, CfnReceiptRuleSet } from 'aws-cdk-lib/aws-ses';
|
|
7
|
+
export const createRawEmailBucket = (scope, id, props = {}) => new Bucket(scope, id, {
|
|
8
|
+
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
|
|
9
|
+
encryption: BucketEncryption.S3_MANAGED,
|
|
10
|
+
enforceSSL: true,
|
|
11
|
+
removalPolicy: RemovalPolicy.RETAIN,
|
|
12
|
+
versioned: true,
|
|
13
|
+
...props,
|
|
14
|
+
});
|
|
15
|
+
export const createEmailReceiving = (scope, id, props) => {
|
|
16
|
+
const { domainName, emailBucket = createRawEmailBucket(scope, `${id}RawEmailBucket`, props.emailBucketProps), processor, rawEmailPrefix = 'incoming/', receiptRuleName, receiptRuleSetName, recipients, scanEnabled = true, ssmParameterNames = [], tlsPolicy = 'Require', } = props;
|
|
17
|
+
const stack = Stack.of(scope);
|
|
18
|
+
const emailIdentity = new CfnEmailIdentity(scope, `${id}EmailIdentity`, {
|
|
19
|
+
dkimAttributes: {
|
|
20
|
+
signingEnabled: true,
|
|
21
|
+
},
|
|
22
|
+
emailIdentity: domainName,
|
|
23
|
+
});
|
|
24
|
+
const receiptRuleSet = new CfnReceiptRuleSet(scope, `${id}ReceiptRuleSet`, {
|
|
25
|
+
ruleSetName: receiptRuleSetName,
|
|
26
|
+
});
|
|
27
|
+
const receiptRuleArn = stack.formatArn({
|
|
28
|
+
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
|
|
29
|
+
service: 'ses',
|
|
30
|
+
resource: 'receipt-rule-set',
|
|
31
|
+
resourceName: `${receiptRuleSetName}:receipt-rule/${receiptRuleName}`,
|
|
32
|
+
});
|
|
33
|
+
emailBucket.addToResourcePolicy(new PolicyStatement({
|
|
34
|
+
actions: ['s3:PutObject'],
|
|
35
|
+
conditions: {
|
|
36
|
+
StringEquals: {
|
|
37
|
+
'AWS:SourceAccount': stack.account,
|
|
38
|
+
},
|
|
39
|
+
StringLike: {
|
|
40
|
+
'AWS:SourceArn': receiptRuleArn,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
effect: Effect.ALLOW,
|
|
44
|
+
principals: [new ServicePrincipal('ses.amazonaws.com')],
|
|
45
|
+
resources: [emailBucket.arnForObjects(`${rawEmailPrefix}*`)],
|
|
46
|
+
}));
|
|
47
|
+
emailBucket.grantRead(processor);
|
|
48
|
+
emailBucket.addEventNotification(EventType.OBJECT_CREATED, new LambdaDestination(processor), {
|
|
49
|
+
prefix: rawEmailPrefix,
|
|
50
|
+
});
|
|
51
|
+
if (ssmParameterNames.length > 0) {
|
|
52
|
+
processor.addToRolePolicy(new PolicyStatement({
|
|
53
|
+
actions: ['ssm:GetParameter'],
|
|
54
|
+
resources: ssmParameterNames.map((parameterName) => stack.formatArn({
|
|
55
|
+
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
|
|
56
|
+
service: 'ssm',
|
|
57
|
+
resource: 'parameter',
|
|
58
|
+
resourceName: parameterName.replace(/^\//, ''),
|
|
59
|
+
})),
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
const receiptRule = new CfnReceiptRule(scope, `${id}ReceiptRule`, {
|
|
63
|
+
rule: {
|
|
64
|
+
actions: [
|
|
65
|
+
{
|
|
66
|
+
s3Action: {
|
|
67
|
+
bucketName: emailBucket.bucketName,
|
|
68
|
+
objectKeyPrefix: rawEmailPrefix,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
enabled: true,
|
|
73
|
+
name: receiptRuleName,
|
|
74
|
+
recipients,
|
|
75
|
+
scanEnabled,
|
|
76
|
+
tlsPolicy,
|
|
77
|
+
},
|
|
78
|
+
ruleSetName: receiptRuleSetName,
|
|
79
|
+
});
|
|
80
|
+
receiptRule.node.addDependency(receiptRuleSet);
|
|
81
|
+
receiptRule.node.addDependency(emailIdentity);
|
|
82
|
+
return {
|
|
83
|
+
emailBucket,
|
|
84
|
+
emailIdentity,
|
|
85
|
+
receiptRule,
|
|
86
|
+
receiptRuleSet,
|
|
87
|
+
};
|
|
88
|
+
};
|