@vyriy/stack 0.5.6 → 0.6.6

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 CHANGED
@@ -35,6 +35,7 @@ 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.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
39
  - `deployment.createBucketDeployment(scope, id, props)` deploys files to S3 with a `512` MB default memory limit.
39
40
  - `deployment.createHtmlCacheControl()` creates cache-control headers for HTML files that should revalidate before reuse.
40
41
  - `deployment.createImmutableCacheControl(days?)` creates long-lived immutable cache-control headers.
@@ -150,6 +151,63 @@ stack(
150
151
  );
151
152
  ```
152
153
 
154
+ ## SES Email Receiving Example
155
+
156
+ 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.
157
+
158
+ 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.
159
+
160
+ ```ts
161
+ import { CfnOutput, Stack, type StackProps } from 'aws-cdk-lib';
162
+ import type { Construct } from 'constructs';
163
+
164
+ import { stack } from '@vyriy/cdk';
165
+ import { lambda, s3, ses } from '@vyriy/stack';
166
+ import { path } from '@vyriy/path';
167
+
168
+ stack(
169
+ class EmailStack extends Stack {
170
+ constructor(scope: Construct, id: string, props: StackProps & { env: { account: string; region: string } }) {
171
+ super(scope, id, props);
172
+
173
+ const domainName = 'site.com';
174
+ const recipient = `consulting@${domainName}`;
175
+ const processor = lambda.createLambda(this, 'EmailProcessor', {
176
+ code: lambda.Code.fromAsset(path('dist', 'email-processor')),
177
+ description: `Processes raw emails sent to ${recipient}`,
178
+ environment: {
179
+ RAW_EMAIL_PREFIX: 'incoming/',
180
+ TELEGRAM_BOT_TOKEN_PARAMETER_NAME: '/site/consulting-email/telegram/bot-token',
181
+ TELEGRAM_CHAT_ID_PARAMETER_NAME: '/site/consulting-email/telegram/chat-id',
182
+ },
183
+ handler: 'index.handler',
184
+ });
185
+ const emailBucket = s3.createBucket(this, 'RawEmailBucket');
186
+
187
+ const emailReceiving = ses.createEmailReceiving(this, 'ConsultingEmail', {
188
+ domainName,
189
+ emailBucket,
190
+ processor,
191
+ rawEmailPrefix: 'incoming/',
192
+ receiptRuleName: 'consulting',
193
+ receiptRuleSetName: 'site-consulting-email',
194
+ recipients: [recipient],
195
+ ssmParameterNames: [
196
+ '/site/consulting-email/telegram/bot-token',
197
+ '/site/consulting-email/telegram/chat-id',
198
+ ],
199
+ });
200
+
201
+ processor.addEnvironment('RAW_EMAIL_BUCKET_NAME', emailReceiving.emailBucket.bucketName);
202
+
203
+ new CfnOutput(this, 'RawEmailBucketName', { value: emailReceiving.emailBucket.bucketName });
204
+ new CfnOutput(this, 'ConsultingEmail', { value: recipient });
205
+ new CfnOutput(this, 'EmailReceiptRuleSetName', { value: 'site-consulting-email' });
206
+ }
207
+ },
208
+ );
209
+ ```
210
+
153
211
  ## API Gateway Lambda Example
154
212
 
155
213
  This example wires a Lambda-backed REST API to an API Gateway custom domain:
@@ -425,6 +483,12 @@ Read SSM parameter values and import parameters:
425
483
  ```ts
426
484
  import { ssm } from '@vyriy/stack';
427
485
 
486
+ const manualApiKey = ssm.createManualStringParameter(this, 'ManualApiKey', {
487
+ description: 'API key filled after first deploy',
488
+ initialValue: 'CHANGE_ME',
489
+ parameterName: `/${id}/api/key`,
490
+ });
491
+
428
492
  const apiBaseUrl = ssm.valueForStringParameter(this, `/${id}/api/base-url`);
429
493
 
430
494
  const importedApiKey = ssm.fromSecureStringParameterAttributes(this, 'ImportedApiKey', {
@@ -432,10 +496,14 @@ const importedApiKey = ssm.fromSecureStringParameterAttributes(this, 'ImportedAp
432
496
  version: 1,
433
497
  });
434
498
 
499
+ manualApiKey.grantRead(apiLambda);
435
500
  importedApiKey.grantRead(apiLambda);
436
501
  importedApiKey.grantRead(taskDefinition.taskRole);
437
502
  ```
438
503
 
504
+ `createManualStringParameter` writes the initial value only when CloudFormation creates the resource. Later stack updates do
505
+ not overwrite a manually edited value, and stack deletion removes the parameter.
506
+
439
507
  Pass SSM parameter values into Lambda and Fargate environment variables:
440
508
 
441
509
  ```ts
@@ -1,5 +1,5 @@
1
1
  import { Construct } from 'constructs';
2
- import { AddBehaviorOptions, Distribution, DistributionProps, Function as CloudFrontFunction, FunctionAssociation, FunctionProps, ViewerProtocolPolicy } from 'aws-cdk-lib/aws-cloudfront';
2
+ import { AddBehaviorOptions, Distribution, DistributionProps, Function as CloudFrontFunction, FunctionAssociation, FunctionProps, ViewerProtocolPolicy, DistributionAttributes } from 'aws-cdk-lib/aws-cloudfront';
3
3
  import { HttpOrigin } from 'aws-cdk-lib/aws-cloudfront-origins';
4
4
  import { Bucket } from 'aws-cdk-lib/aws-s3';
5
5
  export { FunctionEventType, OriginProtocolPolicy, ViewerProtocolPolicy } from 'aws-cdk-lib/aws-cloudfront';
@@ -12,6 +12,7 @@ export interface CreateIndexRewriteFunctionCodeProps {
12
12
  }
13
13
  export interface CreateFunctionAssociationsProps extends CreateIndexRewriteFunctionCodeProps, Omit<CreateCloudFrontFunctionProps, 'code'> {
14
14
  }
15
+ export declare const findDistribution: (scope: Construct, id: string, props: DistributionAttributes) => import("aws-cdk-lib/aws-cloudfront").IDistribution;
15
16
  export declare const createDistribution: (scope: Construct, id: string, props: DistributionProps) => Distribution;
16
17
  export declare const createCloudFrontFunction: (scope: Construct, id: string, props: CreateCloudFrontFunctionProps) => CloudFrontFunction;
17
18
  export declare const createIndexRewriteFunctionCode: ({ rootDomain, wwwDomain }: CreateIndexRewriteFunctionCodeProps) => string;
@@ -1,6 +1,7 @@
1
1
  import { Distribution, Function as CloudFrontFunction, FunctionCode, FunctionEventType, OriginProtocolPolicy, ViewerProtocolPolicy, } from 'aws-cdk-lib/aws-cloudfront';
2
2
  import { HttpOrigin, S3BucketOrigin } from 'aws-cdk-lib/aws-cloudfront-origins';
3
3
  export { FunctionEventType, OriginProtocolPolicy, ViewerProtocolPolicy } from 'aws-cdk-lib/aws-cloudfront';
4
+ export const findDistribution = (scope, id, props) => Distribution.fromDistributionAttributes(scope, id, props);
4
5
  export const createDistribution = (scope, id, props) => new Distribution(scope, id, props);
5
6
  export const createCloudFrontFunction = (scope, id, props) => new CloudFrontFunction(scope, id, {
6
7
  ...props,
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.5.6",
3
+ "version": "0.6.6",
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/s3/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Construct } from 'constructs';
2
2
  import { Bucket, BucketProps } from 'aws-cdk-lib/aws-s3';
3
- export { RedirectProtocol } from 'aws-cdk-lib/aws-s3';
3
+ export { RedirectProtocol, type BucketProps } from 'aws-cdk-lib/aws-s3';
4
+ export declare const findBucket: (scope: Construct, id: string, bucketName: string) => import("aws-cdk-lib/aws-s3").IBucket;
4
5
  export declare const createBucket: (scope: Construct, id: string, props?: BucketProps) => Bucket;
package/s3/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { RemovalPolicy } from 'aws-cdk-lib';
2
2
  import { BlockPublicAccess, Bucket, HttpMethods, BucketEncryption } from 'aws-cdk-lib/aws-s3';
3
3
  export { RedirectProtocol } from 'aws-cdk-lib/aws-s3';
4
+ export const findBucket = (scope, id, bucketName) => Bucket.fromBucketName(scope, id, bucketName);
4
5
  export const createBucket = (scope, id, props = {}) => {
5
6
  const bucketProps = {
6
7
  removalPolicy: RemovalPolicy.DESTROY,
package/ses/index.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { Construct } from 'constructs';
2
+ import { IFunction } from 'aws-cdk-lib/aws-lambda';
3
+ import { 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 CreateEmailReceivingProps = {
7
+ domainName: string;
8
+ emailBucket: IBucket;
9
+ receiptRuleName: string;
10
+ receiptRuleSetName: string;
11
+ recipients: string[];
12
+ processor: IFunction;
13
+ rawEmailPrefix?: string;
14
+ scanEnabled?: boolean;
15
+ ssmParameterNames?: string[];
16
+ tlsPolicy?: ReceiptRuleTlsPolicy;
17
+ };
18
+ export type CreateEmailReceivingResult = {
19
+ emailBucket: IBucket;
20
+ emailIdentity: CfnEmailIdentity;
21
+ receiptRule: CfnReceiptRule;
22
+ receiptRuleSet: CfnReceiptRuleSet;
23
+ };
24
+ export { CfnEmailIdentity, CfnReceiptRule, CfnReceiptRuleSet } from 'aws-cdk-lib/aws-ses';
25
+ export declare const createEmailReceiving: (scope: Construct, id: string, props: CreateEmailReceivingProps) => CreateEmailReceivingResult;
package/ses/index.js ADDED
@@ -0,0 +1,80 @@
1
+ import { ArnFormat, 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 { 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 createEmailReceiving = (scope, id, props) => {
8
+ const { domainName, emailBucket, processor, rawEmailPrefix = 'incoming/', receiptRuleName, receiptRuleSetName, recipients, scanEnabled = true, ssmParameterNames = [], tlsPolicy = 'Require', } = props;
9
+ const stack = Stack.of(scope);
10
+ const emailIdentity = new CfnEmailIdentity(scope, `${id}EmailIdentity`, {
11
+ dkimAttributes: {
12
+ signingEnabled: true,
13
+ },
14
+ emailIdentity: domainName,
15
+ });
16
+ const receiptRuleSet = new CfnReceiptRuleSet(scope, `${id}ReceiptRuleSet`, {
17
+ ruleSetName: receiptRuleSetName,
18
+ });
19
+ const receiptRuleArn = stack.formatArn({
20
+ arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
21
+ service: 'ses',
22
+ resource: 'receipt-rule-set',
23
+ resourceName: `${receiptRuleSetName}:receipt-rule/${receiptRuleName}`,
24
+ });
25
+ emailBucket.addToResourcePolicy(new PolicyStatement({
26
+ actions: ['s3:PutObject'],
27
+ conditions: {
28
+ StringEquals: {
29
+ 'AWS:SourceAccount': stack.account,
30
+ },
31
+ StringLike: {
32
+ 'AWS:SourceArn': receiptRuleArn,
33
+ },
34
+ },
35
+ effect: Effect.ALLOW,
36
+ principals: [new ServicePrincipal('ses.amazonaws.com')],
37
+ resources: [emailBucket.arnForObjects(`${rawEmailPrefix}*`)],
38
+ }));
39
+ emailBucket.grantRead(processor);
40
+ emailBucket.addEventNotification(EventType.OBJECT_CREATED, new LambdaDestination(processor), {
41
+ prefix: rawEmailPrefix,
42
+ });
43
+ if (ssmParameterNames.length > 0) {
44
+ processor.addToRolePolicy(new PolicyStatement({
45
+ actions: ['ssm:GetParameter'],
46
+ resources: ssmParameterNames.map((parameterName) => stack.formatArn({
47
+ arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
48
+ service: 'ssm',
49
+ resource: 'parameter',
50
+ resourceName: parameterName.replace(/^\//, ''),
51
+ })),
52
+ }));
53
+ }
54
+ const receiptRule = new CfnReceiptRule(scope, `${id}ReceiptRule`, {
55
+ rule: {
56
+ actions: [
57
+ {
58
+ s3Action: {
59
+ bucketName: emailBucket.bucketName,
60
+ objectKeyPrefix: rawEmailPrefix,
61
+ },
62
+ },
63
+ ],
64
+ enabled: true,
65
+ name: receiptRuleName,
66
+ recipients,
67
+ scanEnabled,
68
+ tlsPolicy,
69
+ },
70
+ ruleSetName: receiptRuleSetName,
71
+ });
72
+ receiptRule.node.addDependency(receiptRuleSet);
73
+ receiptRule.node.addDependency(emailIdentity);
74
+ return {
75
+ emailBucket,
76
+ emailIdentity,
77
+ receiptRule,
78
+ receiptRuleSet,
79
+ };
80
+ };
package/ssm/index.d.ts CHANGED
@@ -1,7 +1,18 @@
1
1
  import { Construct } from 'constructs';
2
- import { SecureStringParameterAttributes, StringParameterAttributes } from 'aws-cdk-lib/aws-ssm';
2
+ import { IStringParameter, ParameterTier, SecureStringParameterAttributes, StringParameterAttributes } from 'aws-cdk-lib/aws-ssm';
3
+ export interface ManualStringParameterProps {
4
+ allowedPattern?: string;
5
+ dataType?: string;
6
+ description?: string;
7
+ initialValue?: string;
8
+ parameterName: string;
9
+ simpleName?: boolean;
10
+ tier?: ParameterTier;
11
+ }
3
12
  export type { ParameterValueType } from 'aws-cdk-lib/aws-ssm';
4
- export declare const fromStringParameterName: (scope: Construct, id: string, stringParameterName: string) => import("aws-cdk-lib/aws-ssm").IStringParameter;
5
- export declare const fromStringParameterAttributes: (scope: Construct, id: string, attrs: StringParameterAttributes) => import("aws-cdk-lib/aws-ssm").IStringParameter;
6
- export declare const fromSecureStringParameterAttributes: (scope: Construct, id: string, attrs: SecureStringParameterAttributes) => import("aws-cdk-lib/aws-ssm").IStringParameter;
13
+ export { ParameterTier } from 'aws-cdk-lib/aws-ssm';
14
+ export declare const createManualStringParameter: (scope: Construct, id: string, props: ManualStringParameterProps) => IStringParameter;
15
+ export declare const fromStringParameterName: (scope: Construct, id: string, stringParameterName: string) => IStringParameter;
16
+ export declare const fromStringParameterAttributes: (scope: Construct, id: string, attrs: StringParameterAttributes) => IStringParameter;
17
+ export declare const fromSecureStringParameterAttributes: (scope: Construct, id: string, attrs: SecureStringParameterAttributes) => IStringParameter;
7
18
  export declare const valueForStringParameter: (scope: Construct, stringParameterName: string) => string;
package/ssm/index.js CHANGED
@@ -1,4 +1,55 @@
1
- import { StringParameter } from 'aws-cdk-lib/aws-ssm';
1
+ import { ArnFormat, Stack } from 'aws-cdk-lib';
2
+ import { AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId } from 'aws-cdk-lib/custom-resources';
3
+ import { StringParameter, } from 'aws-cdk-lib/aws-ssm';
4
+ export { ParameterTier } from 'aws-cdk-lib/aws-ssm';
5
+ const defaultManualParameterInitialValue = 'CHANGE_ME';
6
+ const parameterArn = (scope, parameterName) => Stack.of(scope).formatArn({
7
+ arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
8
+ service: 'ssm',
9
+ resource: 'parameter',
10
+ resourceName: parameterName.replace(/^\//, ''),
11
+ });
12
+ const definedParameterProps = (props) => ({
13
+ ...(props.allowedPattern === undefined ? {} : { AllowedPattern: props.allowedPattern }),
14
+ ...(props.dataType === undefined ? {} : { DataType: props.dataType }),
15
+ ...(props.description === undefined ? {} : { Description: props.description }),
16
+ ...(props.tier === undefined ? {} : { Tier: props.tier }),
17
+ });
18
+ export const createManualStringParameter = (scope, id, props) => {
19
+ const { initialValue = defaultManualParameterInitialValue, parameterName, simpleName } = props;
20
+ const resource = new AwsCustomResource(scope, `${id}Resource`, {
21
+ installLatestAwsSdk: false,
22
+ onCreate: {
23
+ action: 'putParameter',
24
+ parameters: {
25
+ ...definedParameterProps(props),
26
+ Name: parameterName,
27
+ Overwrite: false,
28
+ Type: 'String',
29
+ Value: initialValue,
30
+ },
31
+ physicalResourceId: PhysicalResourceId.of(parameterName),
32
+ service: 'SSM',
33
+ },
34
+ onDelete: {
35
+ action: 'deleteParameter',
36
+ ignoreErrorCodesMatching: 'ParameterNotFound',
37
+ parameters: {
38
+ Name: parameterName,
39
+ },
40
+ service: 'SSM',
41
+ },
42
+ policy: AwsCustomResourcePolicy.fromSdkCalls({
43
+ resources: [parameterArn(scope, parameterName)],
44
+ }),
45
+ });
46
+ const parameter = StringParameter.fromStringParameterAttributes(scope, id, {
47
+ parameterName,
48
+ simpleName,
49
+ });
50
+ parameter.node.addDependency(resource);
51
+ return parameter;
52
+ };
2
53
  export const fromStringParameterName = (scope, id, stringParameterName) => StringParameter.fromStringParameterName(scope, id, stringParameterName);
3
54
  export const fromStringParameterAttributes = (scope, id, attrs) => StringParameter.fromStringParameterAttributes(scope, id, attrs);
4
55
  export const fromSecureStringParameterAttributes = (scope, id, attrs) => StringParameter.fromSecureStringParameterAttributes(scope, id, attrs);