@@ -56,7 +56,7 @@ export class StacksCloud extends Stack {
56
56
publicBucket : s3 . Bucket
57
57
privateBucket : s3 . Bucket
58
58
logBucket : s3 . Bucket | undefined
59
- emailBucket ? : s3 . Bucket
59
+ emailBucket : s3 . Bucket
60
60
fileSystem ?: efs . FileSystem | undefined
61
61
accessPoint ?: efs . AccessPoint | undefined
62
62
}
@@ -354,19 +354,14 @@ export class StacksCloud extends Stack {
354
354
autoDeleteObjects : true ,
355
355
} )
356
356
357
- let emailBucket : s3 . Bucket | undefined
358
- if ( isProductionDeployment ( ) ) {
359
- emailBucket = new s3 . Bucket ( this , 'EmailBucket' , {
360
- bucketName : `${ this . domain } -email` ,
361
- versioned : true ,
362
- removalPolicy : RemovalPolicy . DESTROY ,
363
- autoDeleteObjects : true ,
364
- } )
365
- }
357
+ const emailBucket : s3 . Bucket = new s3 . Bucket ( this , 'EmailBucket' , {
358
+ bucketName : `${ this . domain } -email` ,
359
+ versioned : true ,
360
+ removalPolicy : RemovalPolicy . DESTROY ,
361
+ autoDeleteObjects : true ,
362
+ } )
366
363
367
- // Create an S3 bucket for CloudFront access logs
368
364
let logBucket : s3 . Bucket | undefined
369
-
370
365
if ( config . cloud . cdn ?. enableLogging ) {
371
366
logBucket = new s3 . Bucket ( this , 'LogBucket' , {
372
367
bucketName : `${ this . domain } -logs-${ appEnv } ` ,
@@ -579,6 +574,61 @@ export class StacksCloud extends Stack {
579
574
recordName : `mail.${ this . domain } ` ,
580
575
values : [ 'v=spf1 include:amazonses.com ~all' ] ,
581
576
} )
577
+
578
+ const lambdaFunction = new lambda . Function ( this , 'SesForwarder' , {
579
+ description : 'The Stacks Email Forwarder' ,
580
+ runtime : lambda . Runtime . NODEJS_18_X ,
581
+ handler : 'index.handler' ,
582
+ code : lambda . Code . fromAsset ( path . join ( __dirname , '/email-forwarder.zip' ) ) ,
583
+ } )
584
+
585
+ const ruleSet = new ses . CfnReceiptRuleSet ( this , 'RuleSet' , {
586
+ ruleSetName : 'EmailForwardingRuleSet' ,
587
+ } )
588
+
589
+ new ses . CfnReceiptRule ( this , 'Rule' , {
590
+ rule : {
591
+ name : 'EmailForwardingRule' ,
592
+ recipients : [ 'chrisbreuer93@gmail.com' ] , // replace with your email addresses
593
+ actions : [
594
+ {
595
+ s3Action : {
596
+ bucketName : this . storage . emailBucket . bucketName ,
597
+ objectKeyPrefix : 'email' ,
598
+ } ,
599
+ } ,
600
+ {
601
+ lambdaAction : {
602
+ functionArn : lambdaFunction . functionArn ,
603
+ invocationType : 'Event' ,
604
+ } ,
605
+ } ,
606
+ ] ,
607
+ enabled : true ,
608
+ scanEnabled : true ,
609
+ } ,
610
+ ruleSetName : ruleSet . ruleSetName as string ,
611
+ } )
612
+
613
+ // Grant SES permission to write to the S3 bucket
614
+ this . storage . emailBucket . addToResourcePolicy ( new iam . PolicyStatement ( {
615
+ principals : [ new iam . ServicePrincipal ( 'ses.amazonaws.com' ) ] ,
616
+ actions : [ 's3:PutObject' ] ,
617
+ resources : [ this . storage . emailBucket . arnForObjects ( '*' ) ] ,
618
+ conditions : {
619
+ StringEquals : {
620
+ 'aws:Referer' : this . account ,
621
+ } ,
622
+ } ,
623
+ } ) )
624
+
625
+ // Grant the Lambda function permission to read from the S3 bucket
626
+ this . storage . emailBucket . grantRead ( lambdaFunction )
627
+
628
+ // Grant SES permission to invoke the Lambda function
629
+ lambdaFunction . addPermission ( 'InvokeBySES' , {
630
+ principal : new iam . ServicePrincipal ( 'ses.amazonaws.com' ) ,
631
+ } )
582
632
}
583
633
584
634
additionalBehaviors ( ) : Record < string , cloudfront . BehaviorOptions > {
0 commit comments