@mavogel/awscdk-rootmail 0.0.17 → 0.0.18
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/.jsii +50 -16
- package/API.md +95 -2
- package/README.md +69 -2
- package/docs/img/cloudformation-tpl-min.png +0 -0
- package/integ-tests/functions/custom-ses-receive-function.ts +92 -0
- package/integ-tests/integ.rootmail-custom-santa.ts +143 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/RootmailIntegTestStack.assets.json +162 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/RootmailIntegTestStack.template.json +4541 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/SetupTestIntegStackDefaultTestDeployAssert6EB6FF77.assets.json +32 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/SetupTestIntegStackDefaultTestDeployAssert6EB6FF77.template.json +249 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.0649b5bfaa322d6bd173d2daef3177389cf0778b556b300ac6bf451704cc3a49/index.js +37981 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.157ba1713f7224463b621261792d18081588ff306a9bc3f75e6c468fabbe1a60/index.js +106 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.4c079063901e6478dbb5ace19e3123641ebf0da2ab648ecf52079c5bec154196/index.js +215 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.53e8368959effe94a6e51bef9c7c5bf0eaf5c4d64eece81632ca62aa34517978.bundle/index.js +31981 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.5cc92ed0cea39e2b8de2dbc527dfb5980a3af9564bd1084d840b9787c7d0467e/index.d.ts +14 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.5cc92ed0cea39e2b8de2dbc527dfb5980a3af9564bd1084d840b9787c7d0467e/index.js +1 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.78b64aaf9aebb4eacbdd42197be10e9d5894360a642f7dcf79aeb84bb3552377/index.js +61 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.7e8a681794eff5fda1841caa360113df5488ecf944055a69b8f1e5a687e269f5/index.js +83 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.9572b451243318765beb576ae7528096b3879783dbaab795c02c01d7f4e83438/index.js +47 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.b8448324ef2c7d5bfa362722df2d397079d5aabaae2749f09180ac5bedfa5c89/index.js +105 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.d2b520d86fd9d964c83dc0d5ce2e4456dbde5fa7ae0c791a71865cbdfe2fb855/index.js +85 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f2d30cfc360482320a52a4fcde8a70f3569df79ab30be24650fda58eb60052cf/cfn-response.js +1 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f2d30cfc360482320a52a4fcde8a70f3569df79ab30be24650fda58eb60052cf/consts.js +1 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f2d30cfc360482320a52a4fcde8a70f3569df79ab30be24650fda58eb60052cf/framework.js +3 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f2d30cfc360482320a52a4fcde8a70f3569df79ab30be24650fda58eb60052cf/outbound.js +1 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f2d30cfc360482320a52a4fcde8a70f3569df79ab30be24650fda58eb60052cf/util.js +1 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f9b17c134d24cf60e1d26c033b5f4f247b759149619ac0aac341b950a10ccf14/index.js +72 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/integ.json +27 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/manifest.json +742 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/tree.json +5893 -0
- package/integ-tests/integ.rootmail.ts.snapshot/SetupTestIntegStackDefaultTestDeployAssert6EB6FF77.assets.json +2 -2
- package/integ-tests/integ.rootmail.ts.snapshot/SetupTestIntegStackDefaultTestDeployAssert6EB6FF77.template.json +3 -3
- package/integ-tests/integ.rootmail.ts.snapshot/manifest.json +1 -1
- package/lib/index.js +2 -1
- package/lib/rootmail.d.ts +5 -1
- package/lib/rootmail.js +3 -2
- package/lib/ses-receive.d.ts +5 -1
- package/lib/ses-receive.js +36 -14
- package/package.json +2 -2
package/.jsii
CHANGED
|
@@ -3518,7 +3518,7 @@
|
|
|
3518
3518
|
},
|
|
3519
3519
|
"name": "@mavogel/awscdk-rootmail",
|
|
3520
3520
|
"readme": {
|
|
3521
|
-
"markdown": "# awscdk-rootmail\n\nA single rootmail box for all your AWS accounts. The cdk implementation and **adaption** of the [superwerker](https://superwerker.cloud/) rootmail feature. See [here](docs/adrs/rootmail.md) for a detailed Architectural Decision Record ([ADR](https://adr.github.io/))\n\n- [TL;DR](#tldr)\n- [Usage](#usage)\n - [Dependencies](#dependencies)\n - [Deploy](#deploy)\n - [CDK](#cdk)\n - [Cloudformation](#cloudformation)\n- [Solution design: Version 1 - Domain in the same AWS account](#solution-design-version-1---domain-in-the-same-aws-account)\n- [Solution design: Version 2 - external DNS provider](#solution-design-version-2---external-dns-provider)\n- [Uninstall](#uninstall)\n- [Known issues](#known-issues)\n- [Related projects](#related-projects)\n\n## TL;DR\nEach AWS account needs one unique email address (the so-called \"AWS account root user email address\").\n\nAccess to these email addresses must be adequately secured since they provide privileged access to AWS accounts, such as account deletion procedures.\n\nThis is why you only need 1 mailing list for the AWS Management (formerly *root*) account,\nwe recommend the following pattern `aws-roots+<uuid>@mycompany.test`\n\n> [!NOTE]\n> Maximum **64** characters are allowed for the whole address.\n\nAnd as you own the domain `mycompany.test` you can add a subdomain, e.g. `aws`, for which all EMails will then be received with this solution within this particular AWS Management account.\n\n## Usage\n### Dependencies\nAdministrative access to an AWS account and the following tools:\n```sh\nbrew install aws-cli node@18 esbuild\n```\n\n<details>\n <summary>... or a longer listing</summary>\n\n- Access to a development environment. It is recommended to use AWS Cloud9 to avoid having to set up the tools needed to deploy the solution. See [Getting started with AWS Cloud9](https://aws.amazon.com/cloud9/getting-started/).\n- Using Cloud9, all of the following have already been configured for you. If you choose not to use Cloud9, you will need to install the following.\n - AWS CLI. See [Installing, updating and uninstalling the AWS CLI version 2](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)\n - Set up the AWS CLI with IAM access credentials. See [Configuring the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)\n - Node.js version 10.13.0 or later\n - AWS CDK version 2.90.0 or later. For installation instructions see [Getting Started with the AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html#getting_started_install)\n - Docker version 20.10.x or later **OR** [esbuild](https://esbuild.github.io/)\n</details>\n\n### Deploy\nYou can chose via embedding the construct in your cdk-app or use is directly via Cloudformation.\n#### CDK\n1. To start a new project we recommend using [projen](https://projen.io/).\n 1. Create a new projen project\n ```sh\n npx projen new awscdk-app-ts\n ```\n 2. Add `@mavogel/awscdk-rootmail` as a dependency to your project in the `.projenrc.ts` file\n 3. Run `npm run projen` to install it\n2. In you `main.ts` file add the following\n```ts\nimport {\n App,\n Stack,\n aws_route53 as r53\n} from 'aws-cdk-lib';\nimport { Rootmail, SESReceiveStack } from '@mavogel/awscdk-rootmail';\nimport { Construct } from 'constructs';\n\nexport class MyStack extends Stack {\n constructor(scope: Construct, id: string, props: StackProps = {}) {\n super(scope, id, props);\n\n const domain = 'mycompany.test'\n\n const hostedZone = r53.HostedZone.fromLookup(this, 'rootmail-parent-hosted-zone', {\n domainName: domain,\n });\n\n const rootmail = new Rootmail(this, 'rootmail-stack', {\n // 1. a domain you own, registered via Route53 in the SAME account\n domain: domain;\n // 2. so the subdomain will be aws.mycompany.test and\n // wired / delegated automatically\n autowireDNSParentHostedZoneID: hostedZone.hostedZoneId,\n env: {\n // 3. or any other region SES is available\n region: 'eu-west-1',\n },\n });\n }\n}\n```\n2. run on your commandline\n```sh\nnpm run deploy\n```\n1. No need to do anything, the NS records are **automatically** propagated as the parent Hosted Zone is in the same account!\n2. The `hosted-zone-dkim-propagation-provider.is-complete-handler` Lambda function checks every 10 seconds if the DNS for the subdomain is propagated. Details are in the Cloudwatch log group.\n\n#### Cloudformation\nor use it directly a Cloudformation template `yaml` from the URL [here](https://mvc-test4-bucket-eu-west-1.s3.eu-west-1.amazonaws.com/rootmail/0.0.13-DEVELOPMENT/rootmailStack.template.yaml).\n\n\n<details>\n <summary>... click to expand</summary>\n\nand fill out the parameters\n\n\n</details>\n\n## Solution design: Version 1 - Domain in the same AWS account\n\n\n1. You own a domain, e.g., `mycompany.test`, registered via `Route53` in the **same** AWS account.\n2. The stack creates a `Route53` public Hosted Zone for the subdomain, e.g., `aws.mycompany.test`. It also automatically adds the `TXT` and `CNAME` records for verifying the domain towards SES **and** adds the NS server entries automatically to the main domain `mycompany.test`.\n3. When the subdomain `aws.mycompany.test` is successfully propagated, the stack creates a verified Domain in AWS SES and adds a recipient rule for `root@aws.mycompany.test`. On a successful propagation you will get a mail as follows to the root Email address of the account you are installing the stack 👇\n\n4. Now, any mail going to `root+<any-string>@aws.mycompany.test` will be processed by OpsSanta 🎅🏽 Lambda function and also stored in the rootmail S3 bucket 🪣.\n5. The OpsSanta function verifies the verdicts (DKIM etc.) of the sender, also skips AWS Account welcome EMails, and processes all other EMails. If it is a password reset link EMail it stores the link in the parameter store (and does *not* create an OpsItem for now). For all other mails, which are not skipped an OpsItem is created to have a central location for all items.\n> [!NOTE]\n> You can also connect your Jira to the OpsCenter.\n6. The bucket where all mail to `root@aws.mycompany.test` are stored.\n7. The [SSM parameter store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html) for the password reset links.\n\n8. The OpsItem which is created. It is open and shall be further processed either in the OpsCenter or any other issue tracker.\n\n\n> [!NOTE]\n> SES support alias, so mail to `root+random-string@aws.mycompany.test` will also be catched and forwarded.\n\n## Solution design: Version 2 - external DNS provider\n<details>\n <summary>open for details</summary>\n\n\n\n```ts\nconst rootmail = new Rootmail(this, 'rootmail-stack', {\n // 1. a domain you own, registered via Route53 in the same account\n domain: 'mycompany.test';\n // 2. '' is the default, so you can also remove it\n // autowireDNSParentHostedZoneID: '',\n env: {\n // 3. or any other region SES is available\n region: 'eu-west-1',\n },\n});\n```\n\n1. You own a domain, e.g., `mycompany.test`. It can be at any registrar such as `godaddy`, also `Route53` itself in another AWS account.\n2. The stack creates a `Route53` public Hosted Zone for the subdomain, e.g., `aws.mycompany.test`. It also automatically adds the TXT and CNAME records (for DKIM etc.) for verifying the domain towards SES. **NOTE:** You must now add the NS server entries into the Domain provider which owns the main domain `mycompany.test`.\n3. items 3-7 are the same as in `v1`\n\n\n> [!NOTE]\n> After running `npm run deploy` you need to do the following steps manually:\n\n1. watch out for the hosted zone `aws.mycompany.test` to be created\n By default you have **2 hours** to wire the DNS!\n2. Then create the NS record in your domain `mycompany.test` for the subdomain `aws.mycompany.test`. Here for Route53 in AWS:\n\n3. You can test it yourself via\n```sh\ndig +short NS 8.8.8.8 aws.mycompany.test\n# should return something like\nns-1111.your-dns-provider-10.org.\nns-2222.your-dns-provider-21.co.uk.\nns-33.your-dns-provider-04.com.\nns-444.your-dns-provider-12.net.\n```\nand also by sending an EMail, e.g. from Gmail to `root@aws.mycompany.test`\n\n</details>\n\n## Uninstall\n1. Delete the stack, the custom resources will delete most resources\n2. And by design you need to manually delete the S3 Bucket containing the mails. This is to prevent accidental deletion of the mails. You can find the bucket name in the stack output.\n\n3. Furthermore, the cloudwatch log groups are not deleted, since they might contain valuable information. You can delete them manually.\n\n## Known issues\n- [jsii/2071](https://github.com/aws/jsii/issues/2071): so adding `compilerOptions.\"esModuleInterop\": true,` in `tsconfig.json` is not possible. See aws-cdk usage with[typescript](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/#Usage_with_TypeScript). So we needed to change import from `import AWS from 'aws-sdk';` -> `import * as AWS from 'aws-sdk';` to be able to compile.\n\n## Related projects\n- [aws-account-factory-email](https://github.com/aws-samples/aws-account-factory-email): a similar approach with SES, however you need to manually configure it upfront and also it about delivering root mails for a specific account to a specific mailing list and mainly decouples the real email address from the one of the AWS account. The main difference is that we do not *hide* or decouple the email address, but more make those as unique and unguessable/bruteforable as possible (with `uuids`)."
|
|
3521
|
+
"markdown": "# awscdk-rootmail\n\nA single rootmail box for all your AWS accounts. The cdk implementation and **adaption** of the [superwerker](https://superwerker.cloud/) rootmail feature. See [here](docs/adrs/rootmail.md) for a detailed Architectural Decision Record ([ADR](https://adr.github.io/))\n\n- [TL;DR](#tldr)\n- [Usage](#usage)\n - [Dependencies](#dependencies)\n - [Deploy](#deploy)\n - [CDK](#cdk)\n - [Cloudformation](#cloudformation)\n- [Solution design: Version 1 - Domain in the same AWS account](#solution-design-version-1---domain-in-the-same-aws-account)\n- [Solution design: Version 2 - external DNS provider](#solution-design-version-2---external-dns-provider)\n- [Uninstall](#uninstall)\n- [Known issues](#known-issues)\n- [Related projects](#related-projects)\n\n## TL;DR\nEach AWS account needs one unique email address (the so-called \"AWS account root user email address\").\n\nAccess to these email addresses must be adequately secured since they provide privileged access to AWS accounts, such as account deletion procedures.\n\nThis is why you only need 1 mailing list for the AWS Management (formerly *root*) account,\nwe recommend the following pattern `aws-roots+<uuid>@mycompany.test`\n\n> [!NOTE]\n> Maximum **64** characters are allowed for the whole address.\n\nAnd as you own the domain `mycompany.test` you can add a subdomain, e.g. `aws`, for which all EMails will then be received with this solution within this particular AWS Management account.\n\n## Usage\n### Dependencies\nAdministrative access to an AWS account and the following tools:\n```sh\nbrew install aws-cli node@18 esbuild\n```\n\n<details>\n <summary>... or a longer listing</summary>\n\n- Access to a development environment. It is recommended to use AWS Cloud9 to avoid having to set up the tools needed to deploy the solution. See [Getting started with AWS Cloud9](https://aws.amazon.com/cloud9/getting-started/).\n- Using Cloud9, all of the following have already been configured for you. If you choose not to use Cloud9, you will need to install the following.\n - AWS CLI. See [Installing, updating and uninstalling the AWS CLI version 2](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)\n - Set up the AWS CLI with IAM access credentials. See [Configuring the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)\n - Node.js version 10.13.0 or later\n - AWS CDK version 2.90.0 or later. For installation instructions see [Getting Started with the AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html#getting_started_install)\n - Docker version 20.10.x or later **OR** [esbuild](https://esbuild.github.io/)\n</details>\n\n### Deploy\nYou can chose via embedding the construct in your cdk-app or use is directly via Cloudformation.\n#### CDK\n1. To start a new project we recommend using [projen](https://projen.io/).\n 1. Create a new projen project\n ```sh\n npx projen new awscdk-app-ts\n ```\n 2. Add `@mavogel/awscdk-rootmail` as a dependency to your project in the `.projenrc.ts` file\n 3. Run `npm run projen` to install it\n2. In you `main.ts` file add the following code\n```ts\nimport {\n App,\n Stack,\n aws_route53 as r53\n} from 'aws-cdk-lib';\nimport { Rootmail } from '@mavogel/awscdk-rootmail';\nimport { Construct } from 'constructs';\n\nexport class MyStack extends Stack {\n constructor(scope: Construct, id: string, props: StackProps = {}) {\n super(scope, id, props);\n\n const domain = 'mycompany.test'\n\n const hostedZone = r53.HostedZone.fromLookup(this, 'rootmail-parent-hosted-zone', {\n domainName: domain,\n });\n\n const rootmail = new Rootmail(this, 'rootmail-stack', {\n // 1. a domain you own, registered via Route53 in the SAME account\n domain: domain;\n // 2. so the subdomain will be aws.mycompany.test and\n // wired / delegated automatically\n autowireDNSParentHostedZoneID: hostedZone.hostedZoneId,\n env: {\n // 3. or any other region SES is available\n region: 'eu-west-1',\n },\n });\n }\n}\n```\n2. run on your commandline\n```sh\nnpm run deploy\n```\n1. No need to do anything, the NS records are **automatically** propagated as the parent Hosted Zone is in the same account!\n2. The `hosted-zone-dkim-propagation-provider.is-complete-handler` Lambda function checks every 10 seconds if the DNS for the subdomain is propagated. Details are in the Cloudwatch log group.\n\n### CDK with your own receiver function\nYou might also want to pass in you own function on what to do when an EMail is received\n\nfile `functions/custom-ses-receive-function.ts` which gets the 2 environment variables populated\n- `EMAIL_BUCKET`\n- `EMAIL_BUCKET_ARN`\n\nas well as `s3:GetObject` on the `RootMail/*` objects in the created Rootmail `S3` bucket.\n\n```ts\nimport { S3 } from '@aws-sdk/client-s3';\nimport { ParsedMail, simpleParser } from 'mailparser';\n// populated by default\nconst emailBucket = process.env.EMAIL_BUCKET;\nconst emailBucketArn = process.env.EMAIL_BUCKET_ARN;\nconst s3 = new S3();\n\n// SESEventRecordsToLambda\n// from https://docs.aws.amazon.com/ses/latest/dg/receiving-email-action-lambda-event.html\nexport const handler = async (event: SESEventRecordsToLambda) => {\n for (const record of event.Records) {\n\n const id = record.ses.mail.messageId;\n const key = `RootMail/${id}`;\n const response = await s3.getObject({ Bucket: emailBucket as string, Key: key });\n\n const msg: ParsedMail = await simpleParser(response.Body as unknown as Buffer);\n\n let title = msg.subject;\n console.log(`Title: ${title} from emailBucketArn: ${emailBucketArn}`);\n // use the content of the email body\n const body = msg.html;\n // add your custom code here ...\n\n // dummy example: list s3 buckets\n const buckets = await s3.listBuckets({});\n if (!buckets.Buckets) {\n console.log('No buckets found');\n return;\n }\n console.log('Buckets:');\n for (const bucket of buckets.Buckets || []) {\n console.log(bucket.Name);\n }\n }\n\n};\n```\n\n```ts\nconst customSesReceiveFunction = new NodejsFunction(stackUnderTest, 'custom-ses-receive-function', {\n functionName: PhysicalName.GENERATE_IF_NEEDED,\n entry: path.join(__dirname, 'functions', 'custom-ses-receive-function.ts'),\n runtime: lambda.Runtime.NODEJS_18_X,\n logRetention: 1,\n timeout: Duration.seconds(30),\n});\n\n// Note: any additional permissions you need to add to the function yourself!\ncustomSesReceiveFunction.addToRolePolicy(new iam.PolicyStatement({\n actions: [\n 's3:List*',\n ],\n resources: ['*'],\n}))\n```\n\n#### Cloudformation\nor use it directly a Cloudformation template `yaml` from the URL [here](https://mvc-test4-bucket-eu-west-1.s3.eu-west-1.amazonaws.com/rootmail/0.0.13-DEVELOPMENT/rootmailStack.template.yaml).\n\n\n<details>\n <summary>... click to expand</summary>\n\nand fill out the parameters\n\n\n</details>\n\n## Solution design: Version 1 - Domain in the same AWS account\n\n\n1. You own a domain, e.g., `mycompany.test`, registered via `Route53` in the **same** AWS account.\n2. The stack creates a `Route53` public Hosted Zone for the subdomain, e.g., `aws.mycompany.test`. It also automatically adds the `TXT` and `CNAME` records for verifying the domain towards SES **and** adds the NS server entries automatically to the main domain `mycompany.test`.\n3. When the subdomain `aws.mycompany.test` is successfully propagated, the stack creates a verified Domain in AWS SES and adds a recipient rule for `root@aws.mycompany.test`. On a successful propagation you will get a mail as follows to the root Email address of the account you are installing the stack 👇\n\n4. Now, any mail going to `root+<any-string>@aws.mycompany.test` will be processed by OpsSanta 🎅🏽 Lambda function and also stored in the rootmail S3 bucket 🪣.\n5. The OpsSanta function verifies the verdicts (DKIM etc.) of the sender, also skips AWS Account welcome EMails, and processes all other EMails. If it is a password reset link EMail it stores the link in the parameter store (and does *not* create an OpsItem for now). For all other mails, which are not skipped an OpsItem is created to have a central location for all items.\n> [!NOTE]\n> You can also connect your Jira to the OpsCenter.\n6. The bucket where all mail to `root@aws.mycompany.test` are stored.\n7. The [SSM parameter store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html) for the password reset links.\n\n8. The OpsItem which is created. It is open and shall be further processed either in the OpsCenter or any other issue tracker.\n\n\n> [!NOTE]\n> SES support alias, so mail to `root+random-string@aws.mycompany.test` will also be catched and forwarded.\n\n## Solution design: Version 2 - external DNS provider\n<details>\n <summary>open for details</summary>\n\n\n\n```ts\nconst rootmail = new Rootmail(this, 'rootmail-stack', {\n // 1. a domain you own, registered via Route53 in the same account\n domain: 'mycompany.test';\n // 2. '' is the default, so you can also remove it\n // autowireDNSParentHostedZoneID: '',\n env: {\n // 3. or any other region SES is available\n region: 'eu-west-1',\n },\n});\n```\n\n1. You own a domain, e.g., `mycompany.test`. It can be at any registrar such as `godaddy`, also `Route53` itself in another AWS account.\n2. The stack creates a `Route53` public Hosted Zone for the subdomain, e.g., `aws.mycompany.test`. It also automatically adds the TXT and CNAME records (for DKIM etc.) for verifying the domain towards SES. **NOTE:** You must now add the NS server entries into the Domain provider which owns the main domain `mycompany.test`.\n3. items 3-7 are the same as in `v1`\n\n\n> [!NOTE]\n> After running `npm run deploy` you need to do the following steps manually:\n\n1. watch out for the hosted zone `aws.mycompany.test` to be created\n By default you have **2 hours** to wire the DNS!\n2. Then create the NS record in your domain `mycompany.test` for the subdomain `aws.mycompany.test`. Here for Route53 in AWS:\n\n3. You can test it yourself via\n```sh\ndig +short NS 8.8.8.8 aws.mycompany.test\n# should return something like\nns-1111.your-dns-provider-10.org.\nns-2222.your-dns-provider-21.co.uk.\nns-33.your-dns-provider-04.com.\nns-444.your-dns-provider-12.net.\n```\nand also by sending an EMail, e.g. from Gmail to `root@aws.mycompany.test`\n\n</details>\n\n## Uninstall\n1. Delete the stack, the custom resources will delete most resources\n2. And by design you need to manually delete the S3 Bucket containing the mails. This is to prevent accidental deletion of the mails. You can find the bucket name in the stack output.\n\n3. Furthermore, the cloudwatch log groups are not deleted, since they might contain valuable information. You can delete them manually.\n\n## Known issues\n- [jsii/2071](https://github.com/aws/jsii/issues/2071): so adding `compilerOptions.\"esModuleInterop\": true,` in `tsconfig.json` is not possible. See aws-cdk usage with[typescript](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/#Usage_with_TypeScript). So we needed to change import from `import AWS from 'aws-sdk';` -> `import * as AWS from 'aws-sdk';` to be able to compile.\n\n## Related projects\n- [aws-account-factory-email](https://github.com/aws-samples/aws-account-factory-email): a similar approach with SES, however you need to manually configure it upfront and also it about delivering root mails for a specific account to a specific mailing list and mainly decouples the real email address from the one of the AWS account. The main difference is that we do not *hide* or decouple the email address, but more make those as unique and unguessable/bruteforable as possible (with `uuids`)."
|
|
3522
3522
|
},
|
|
3523
3523
|
"repository": {
|
|
3524
3524
|
"type": "git",
|
|
@@ -3544,7 +3544,7 @@
|
|
|
3544
3544
|
},
|
|
3545
3545
|
"locationInModule": {
|
|
3546
3546
|
"filename": "src/rootmail.ts",
|
|
3547
|
-
"line":
|
|
3547
|
+
"line": 65
|
|
3548
3548
|
},
|
|
3549
3549
|
"parameters": [
|
|
3550
3550
|
{
|
|
@@ -3570,7 +3570,7 @@
|
|
|
3570
3570
|
"kind": "class",
|
|
3571
3571
|
"locationInModule": {
|
|
3572
3572
|
"filename": "src/rootmail.ts",
|
|
3573
|
-
"line":
|
|
3573
|
+
"line": 61
|
|
3574
3574
|
},
|
|
3575
3575
|
"name": "Rootmail",
|
|
3576
3576
|
"properties": [
|
|
@@ -3581,7 +3581,7 @@
|
|
|
3581
3581
|
"immutable": true,
|
|
3582
3582
|
"locationInModule": {
|
|
3583
3583
|
"filename": "src/rootmail.ts",
|
|
3584
|
-
"line":
|
|
3584
|
+
"line": 63
|
|
3585
3585
|
},
|
|
3586
3586
|
"name": "emailBucket",
|
|
3587
3587
|
"type": {
|
|
@@ -3595,7 +3595,7 @@
|
|
|
3595
3595
|
"immutable": true,
|
|
3596
3596
|
"locationInModule": {
|
|
3597
3597
|
"filename": "src/rootmail.ts",
|
|
3598
|
-
"line":
|
|
3598
|
+
"line": 62
|
|
3599
3599
|
},
|
|
3600
3600
|
"name": "hostedZoneParameterName",
|
|
3601
3601
|
"type": {
|
|
@@ -3615,7 +3615,7 @@
|
|
|
3615
3615
|
"kind": "interface",
|
|
3616
3616
|
"locationInModule": {
|
|
3617
3617
|
"filename": "src/rootmail.ts",
|
|
3618
|
-
"line":
|
|
3618
|
+
"line": 20
|
|
3619
3619
|
},
|
|
3620
3620
|
"name": "RootmailProps",
|
|
3621
3621
|
"properties": [
|
|
@@ -3628,13 +3628,30 @@
|
|
|
3628
3628
|
"immutable": true,
|
|
3629
3629
|
"locationInModule": {
|
|
3630
3630
|
"filename": "src/rootmail.ts",
|
|
3631
|
-
"line":
|
|
3631
|
+
"line": 24
|
|
3632
3632
|
},
|
|
3633
3633
|
"name": "domain",
|
|
3634
3634
|
"type": {
|
|
3635
3635
|
"primitive": "string"
|
|
3636
3636
|
}
|
|
3637
3637
|
},
|
|
3638
|
+
{
|
|
3639
|
+
"abstract": true,
|
|
3640
|
+
"docs": {
|
|
3641
|
+
"stability": "stable",
|
|
3642
|
+
"summary": "The custom SES receive function to use."
|
|
3643
|
+
},
|
|
3644
|
+
"immutable": true,
|
|
3645
|
+
"locationInModule": {
|
|
3646
|
+
"filename": "src/rootmail.ts",
|
|
3647
|
+
"line": 51
|
|
3648
|
+
},
|
|
3649
|
+
"name": "customSesReceiveFunction",
|
|
3650
|
+
"optional": true,
|
|
3651
|
+
"type": {
|
|
3652
|
+
"fqn": "aws-cdk-lib.aws_lambda.Function"
|
|
3653
|
+
}
|
|
3654
|
+
},
|
|
3638
3655
|
{
|
|
3639
3656
|
"abstract": true,
|
|
3640
3657
|
"docs": {
|
|
@@ -3646,7 +3663,7 @@
|
|
|
3646
3663
|
"immutable": true,
|
|
3647
3664
|
"locationInModule": {
|
|
3648
3665
|
"filename": "src/rootmail.ts",
|
|
3649
|
-
"line":
|
|
3666
|
+
"line": 58
|
|
3650
3667
|
},
|
|
3651
3668
|
"name": "setDestroyPolicyToAllResources",
|
|
3652
3669
|
"optional": true,
|
|
@@ -3664,7 +3681,7 @@
|
|
|
3664
3681
|
"immutable": true,
|
|
3665
3682
|
"locationInModule": {
|
|
3666
3683
|
"filename": "src/rootmail.ts",
|
|
3667
|
-
"line":
|
|
3684
|
+
"line": 31
|
|
3668
3685
|
},
|
|
3669
3686
|
"name": "subdomain",
|
|
3670
3687
|
"optional": true,
|
|
@@ -3682,7 +3699,7 @@
|
|
|
3682
3699
|
"immutable": true,
|
|
3683
3700
|
"locationInModule": {
|
|
3684
3701
|
"filename": "src/rootmail.ts",
|
|
3685
|
-
"line":
|
|
3702
|
+
"line": 38
|
|
3686
3703
|
},
|
|
3687
3704
|
"name": "totalTimeToWireDNS",
|
|
3688
3705
|
"optional": true,
|
|
@@ -3700,7 +3717,7 @@
|
|
|
3700
3717
|
"immutable": true,
|
|
3701
3718
|
"locationInModule": {
|
|
3702
3719
|
"filename": "src/rootmail.ts",
|
|
3703
|
-
"line":
|
|
3720
|
+
"line": 46
|
|
3704
3721
|
},
|
|
3705
3722
|
"name": "wireDNSToHostedZoneID",
|
|
3706
3723
|
"optional": true,
|
|
@@ -3724,7 +3741,7 @@
|
|
|
3724
3741
|
},
|
|
3725
3742
|
"locationInModule": {
|
|
3726
3743
|
"filename": "src/ses-receive.ts",
|
|
3727
|
-
"line":
|
|
3744
|
+
"line": 48
|
|
3728
3745
|
},
|
|
3729
3746
|
"parameters": [
|
|
3730
3747
|
{
|
|
@@ -3750,7 +3767,7 @@
|
|
|
3750
3767
|
"kind": "class",
|
|
3751
3768
|
"locationInModule": {
|
|
3752
3769
|
"filename": "src/ses-receive.ts",
|
|
3753
|
-
"line":
|
|
3770
|
+
"line": 47
|
|
3754
3771
|
},
|
|
3755
3772
|
"name": "SESReceive",
|
|
3756
3773
|
"symbolId": "src/ses-receive:SESReceive"
|
|
@@ -3817,6 +3834,23 @@
|
|
|
3817
3834
|
"primitive": "string"
|
|
3818
3835
|
}
|
|
3819
3836
|
},
|
|
3837
|
+
{
|
|
3838
|
+
"abstract": true,
|
|
3839
|
+
"docs": {
|
|
3840
|
+
"stability": "stable",
|
|
3841
|
+
"summary": "The custom SES receive function to use."
|
|
3842
|
+
},
|
|
3843
|
+
"immutable": true,
|
|
3844
|
+
"locationInModule": {
|
|
3845
|
+
"filename": "src/ses-receive.ts",
|
|
3846
|
+
"line": 39
|
|
3847
|
+
},
|
|
3848
|
+
"name": "customSesReceiveFunction",
|
|
3849
|
+
"optional": true,
|
|
3850
|
+
"type": {
|
|
3851
|
+
"fqn": "aws-cdk-lib.aws_lambda.Function"
|
|
3852
|
+
}
|
|
3853
|
+
},
|
|
3820
3854
|
{
|
|
3821
3855
|
"abstract": true,
|
|
3822
3856
|
"docs": {
|
|
@@ -3827,7 +3861,7 @@
|
|
|
3827
3861
|
"immutable": true,
|
|
3828
3862
|
"locationInModule": {
|
|
3829
3863
|
"filename": "src/ses-receive.ts",
|
|
3830
|
-
"line":
|
|
3864
|
+
"line": 44
|
|
3831
3865
|
},
|
|
3832
3866
|
"name": "setDestroyPolicyToAllResources",
|
|
3833
3867
|
"optional": true,
|
|
@@ -3839,6 +3873,6 @@
|
|
|
3839
3873
|
"symbolId": "src/ses-receive:SESReceiveProps"
|
|
3840
3874
|
}
|
|
3841
3875
|
},
|
|
3842
|
-
"version": "0.0.
|
|
3843
|
-
"fingerprint": "
|
|
3876
|
+
"version": "0.0.18",
|
|
3877
|
+
"fingerprint": "ZCmksEjMthIJoEbvZEHdNGJF5TwNn18SUkvmUmRDt2k="
|
|
3844
3878
|
}
|
package/API.md
CHANGED
|
@@ -56,14 +56,14 @@ You can chose via embedding the construct in your cdk-app or use is directly via
|
|
|
56
56
|
```
|
|
57
57
|
2. Add `@mavogel/awscdk-rootmail` as a dependency to your project in the `.projenrc.ts` file
|
|
58
58
|
3. Run `npm run projen` to install it
|
|
59
|
-
2. In you `main.ts` file add the following
|
|
59
|
+
2. In you `main.ts` file add the following code
|
|
60
60
|
```ts
|
|
61
61
|
import {
|
|
62
62
|
App,
|
|
63
63
|
Stack,
|
|
64
64
|
aws_route53 as r53
|
|
65
65
|
} from 'aws-cdk-lib';
|
|
66
|
-
import { Rootmail
|
|
66
|
+
import { Rootmail } from '@mavogel/awscdk-rootmail';
|
|
67
67
|
import { Construct } from 'constructs';
|
|
68
68
|
|
|
69
69
|
export class MyStack extends Stack {
|
|
@@ -97,6 +97,73 @@ npm run deploy
|
|
|
97
97
|
1. No need to do anything, the NS records are **automatically** propagated as the parent Hosted Zone is in the same account!
|
|
98
98
|
2. The `hosted-zone-dkim-propagation-provider.is-complete-handler` Lambda function checks every 10 seconds if the DNS for the subdomain is propagated. Details are in the Cloudwatch log group.
|
|
99
99
|
|
|
100
|
+
### CDK with your own receiver function
|
|
101
|
+
You might also want to pass in you own function on what to do when an EMail is received
|
|
102
|
+
|
|
103
|
+
file `functions/custom-ses-receive-function.ts` which gets the 2 environment variables populated
|
|
104
|
+
- `EMAIL_BUCKET`
|
|
105
|
+
- `EMAIL_BUCKET_ARN`
|
|
106
|
+
|
|
107
|
+
as well as `s3:GetObject` on the `RootMail/*` objects in the created Rootmail `S3` bucket.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { S3 } from '@aws-sdk/client-s3';
|
|
111
|
+
import { ParsedMail, simpleParser } from 'mailparser';
|
|
112
|
+
// populated by default
|
|
113
|
+
const emailBucket = process.env.EMAIL_BUCKET;
|
|
114
|
+
const emailBucketArn = process.env.EMAIL_BUCKET_ARN;
|
|
115
|
+
const s3 = new S3();
|
|
116
|
+
|
|
117
|
+
// SESEventRecordsToLambda
|
|
118
|
+
// from https://docs.aws.amazon.com/ses/latest/dg/receiving-email-action-lambda-event.html
|
|
119
|
+
export const handler = async (event: SESEventRecordsToLambda) => {
|
|
120
|
+
for (const record of event.Records) {
|
|
121
|
+
|
|
122
|
+
const id = record.ses.mail.messageId;
|
|
123
|
+
const key = `RootMail/${id}`;
|
|
124
|
+
const response = await s3.getObject({ Bucket: emailBucket as string, Key: key });
|
|
125
|
+
|
|
126
|
+
const msg: ParsedMail = await simpleParser(response.Body as unknown as Buffer);
|
|
127
|
+
|
|
128
|
+
let title = msg.subject;
|
|
129
|
+
console.log(`Title: ${title} from emailBucketArn: ${emailBucketArn}`);
|
|
130
|
+
// use the content of the email body
|
|
131
|
+
const body = msg.html;
|
|
132
|
+
// add your custom code here ...
|
|
133
|
+
|
|
134
|
+
// dummy example: list s3 buckets
|
|
135
|
+
const buckets = await s3.listBuckets({});
|
|
136
|
+
if (!buckets.Buckets) {
|
|
137
|
+
console.log('No buckets found');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
console.log('Buckets:');
|
|
141
|
+
for (const bucket of buckets.Buckets || []) {
|
|
142
|
+
console.log(bucket.Name);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
};
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
const customSesReceiveFunction = new NodejsFunction(stackUnderTest, 'custom-ses-receive-function', {
|
|
151
|
+
functionName: PhysicalName.GENERATE_IF_NEEDED,
|
|
152
|
+
entry: path.join(__dirname, 'functions', 'custom-ses-receive-function.ts'),
|
|
153
|
+
runtime: lambda.Runtime.NODEJS_18_X,
|
|
154
|
+
logRetention: 1,
|
|
155
|
+
timeout: Duration.seconds(30),
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Note: any additional permissions you need to add to the function yourself!
|
|
159
|
+
customSesReceiveFunction.addToRolePolicy(new iam.PolicyStatement({
|
|
160
|
+
actions: [
|
|
161
|
+
's3:List*',
|
|
162
|
+
],
|
|
163
|
+
resources: ['*'],
|
|
164
|
+
}))
|
|
165
|
+
```
|
|
166
|
+
|
|
100
167
|
#### Cloudformation
|
|
101
168
|
or use it directly a Cloudformation template `yaml` from the URL [here](https://mvc-test4-bucket-eu-west-1.s3.eu-west-1.amazonaws.com/rootmail/0.0.13-DEVELOPMENT/rootmailStack.template.yaml).
|
|
102
169
|
|
|
@@ -425,6 +492,7 @@ const rootmailProps: RootmailProps = { ... }
|
|
|
425
492
|
| **Name** | **Type** | **Description** |
|
|
426
493
|
| --- | --- | --- |
|
|
427
494
|
| <code><a href="#@mavogel/awscdk-rootmail.RootmailProps.property.domain">domain</a></code> | <code>string</code> | Domain used for root mail feature. |
|
|
495
|
+
| <code><a href="#@mavogel/awscdk-rootmail.RootmailProps.property.customSesReceiveFunction">customSesReceiveFunction</a></code> | <code>aws-cdk-lib.aws_lambda.Function</code> | The custom SES receive function to use. |
|
|
428
496
|
| <code><a href="#@mavogel/awscdk-rootmail.RootmailProps.property.setDestroyPolicyToAllResources">setDestroyPolicyToAllResources</a></code> | <code>boolean</code> | Whether to set all removal policies to DESTROY. |
|
|
429
497
|
| <code><a href="#@mavogel/awscdk-rootmail.RootmailProps.property.subdomain">subdomain</a></code> | <code>string</code> | Subdomain used for root mail feature. |
|
|
430
498
|
| <code><a href="#@mavogel/awscdk-rootmail.RootmailProps.property.totalTimeToWireDNS">totalTimeToWireDNS</a></code> | <code>aws-cdk-lib.Duration</code> | The total time to wait for the DNS records to be available/wired. |
|
|
@@ -444,6 +512,18 @@ Domain used for root mail feature.
|
|
|
444
512
|
|
|
445
513
|
---
|
|
446
514
|
|
|
515
|
+
##### `customSesReceiveFunction`<sup>Optional</sup> <a name="customSesReceiveFunction" id="@mavogel/awscdk-rootmail.RootmailProps.property.customSesReceiveFunction"></a>
|
|
516
|
+
|
|
517
|
+
```typescript
|
|
518
|
+
public readonly customSesReceiveFunction: Function;
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
- *Type:* aws-cdk-lib.aws_lambda.Function
|
|
522
|
+
|
|
523
|
+
The custom SES receive function to use.
|
|
524
|
+
|
|
525
|
+
---
|
|
526
|
+
|
|
447
527
|
##### `setDestroyPolicyToAllResources`<sup>Optional</sup> <a name="setDestroyPolicyToAllResources" id="@mavogel/awscdk-rootmail.RootmailProps.property.setDestroyPolicyToAllResources"></a>
|
|
448
528
|
|
|
449
529
|
```typescript
|
|
@@ -515,6 +595,7 @@ const sESReceiveProps: SESReceiveProps = { ... }
|
|
|
515
595
|
| <code><a href="#@mavogel/awscdk-rootmail.SESReceiveProps.property.domain">domain</a></code> | <code>string</code> | Domain used for root mail feature. |
|
|
516
596
|
| <code><a href="#@mavogel/awscdk-rootmail.SESReceiveProps.property.emailbucket">emailbucket</a></code> | <code>aws-cdk-lib.aws_s3.Bucket</code> | S3 bucket to store received emails. |
|
|
517
597
|
| <code><a href="#@mavogel/awscdk-rootmail.SESReceiveProps.property.subdomain">subdomain</a></code> | <code>string</code> | Subdomain used for root mail feature. |
|
|
598
|
+
| <code><a href="#@mavogel/awscdk-rootmail.SESReceiveProps.property.customSesReceiveFunction">customSesReceiveFunction</a></code> | <code>aws-cdk-lib.aws_lambda.Function</code> | The custom SES receive function to use. |
|
|
518
599
|
| <code><a href="#@mavogel/awscdk-rootmail.SESReceiveProps.property.setDestroyPolicyToAllResources">setDestroyPolicyToAllResources</a></code> | <code>boolean</code> | Whether to set all removal policies to DESTROY. |
|
|
519
600
|
|
|
520
601
|
---
|
|
@@ -555,6 +636,18 @@ Subdomain used for root mail feature.
|
|
|
555
636
|
|
|
556
637
|
---
|
|
557
638
|
|
|
639
|
+
##### `customSesReceiveFunction`<sup>Optional</sup> <a name="customSesReceiveFunction" id="@mavogel/awscdk-rootmail.SESReceiveProps.property.customSesReceiveFunction"></a>
|
|
640
|
+
|
|
641
|
+
```typescript
|
|
642
|
+
public readonly customSesReceiveFunction: Function;
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
- *Type:* aws-cdk-lib.aws_lambda.Function
|
|
646
|
+
|
|
647
|
+
The custom SES receive function to use.
|
|
648
|
+
|
|
649
|
+
---
|
|
650
|
+
|
|
558
651
|
##### `setDestroyPolicyToAllResources`<sup>Optional</sup> <a name="setDestroyPolicyToAllResources" id="@mavogel/awscdk-rootmail.SESReceiveProps.property.setDestroyPolicyToAllResources"></a>
|
|
559
652
|
|
|
560
653
|
```typescript
|
package/README.md
CHANGED
|
@@ -56,14 +56,14 @@ You can chose via embedding the construct in your cdk-app or use is directly via
|
|
|
56
56
|
```
|
|
57
57
|
2. Add `@mavogel/awscdk-rootmail` as a dependency to your project in the `.projenrc.ts` file
|
|
58
58
|
3. Run `npm run projen` to install it
|
|
59
|
-
2. In you `main.ts` file add the following
|
|
59
|
+
2. In you `main.ts` file add the following code
|
|
60
60
|
```ts
|
|
61
61
|
import {
|
|
62
62
|
App,
|
|
63
63
|
Stack,
|
|
64
64
|
aws_route53 as r53
|
|
65
65
|
} from 'aws-cdk-lib';
|
|
66
|
-
import { Rootmail
|
|
66
|
+
import { Rootmail } from '@mavogel/awscdk-rootmail';
|
|
67
67
|
import { Construct } from 'constructs';
|
|
68
68
|
|
|
69
69
|
export class MyStack extends Stack {
|
|
@@ -97,6 +97,73 @@ npm run deploy
|
|
|
97
97
|
1. No need to do anything, the NS records are **automatically** propagated as the parent Hosted Zone is in the same account!
|
|
98
98
|
2. The `hosted-zone-dkim-propagation-provider.is-complete-handler` Lambda function checks every 10 seconds if the DNS for the subdomain is propagated. Details are in the Cloudwatch log group.
|
|
99
99
|
|
|
100
|
+
### CDK with your own receiver function
|
|
101
|
+
You might also want to pass in you own function on what to do when an EMail is received
|
|
102
|
+
|
|
103
|
+
file `functions/custom-ses-receive-function.ts` which gets the 2 environment variables populated
|
|
104
|
+
- `EMAIL_BUCKET`
|
|
105
|
+
- `EMAIL_BUCKET_ARN`
|
|
106
|
+
|
|
107
|
+
as well as `s3:GetObject` on the `RootMail/*` objects in the created Rootmail `S3` bucket.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { S3 } from '@aws-sdk/client-s3';
|
|
111
|
+
import { ParsedMail, simpleParser } from 'mailparser';
|
|
112
|
+
// populated by default
|
|
113
|
+
const emailBucket = process.env.EMAIL_BUCKET;
|
|
114
|
+
const emailBucketArn = process.env.EMAIL_BUCKET_ARN;
|
|
115
|
+
const s3 = new S3();
|
|
116
|
+
|
|
117
|
+
// SESEventRecordsToLambda
|
|
118
|
+
// from https://docs.aws.amazon.com/ses/latest/dg/receiving-email-action-lambda-event.html
|
|
119
|
+
export const handler = async (event: SESEventRecordsToLambda) => {
|
|
120
|
+
for (const record of event.Records) {
|
|
121
|
+
|
|
122
|
+
const id = record.ses.mail.messageId;
|
|
123
|
+
const key = `RootMail/${id}`;
|
|
124
|
+
const response = await s3.getObject({ Bucket: emailBucket as string, Key: key });
|
|
125
|
+
|
|
126
|
+
const msg: ParsedMail = await simpleParser(response.Body as unknown as Buffer);
|
|
127
|
+
|
|
128
|
+
let title = msg.subject;
|
|
129
|
+
console.log(`Title: ${title} from emailBucketArn: ${emailBucketArn}`);
|
|
130
|
+
// use the content of the email body
|
|
131
|
+
const body = msg.html;
|
|
132
|
+
// add your custom code here ...
|
|
133
|
+
|
|
134
|
+
// dummy example: list s3 buckets
|
|
135
|
+
const buckets = await s3.listBuckets({});
|
|
136
|
+
if (!buckets.Buckets) {
|
|
137
|
+
console.log('No buckets found');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
console.log('Buckets:');
|
|
141
|
+
for (const bucket of buckets.Buckets || []) {
|
|
142
|
+
console.log(bucket.Name);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
};
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
const customSesReceiveFunction = new NodejsFunction(stackUnderTest, 'custom-ses-receive-function', {
|
|
151
|
+
functionName: PhysicalName.GENERATE_IF_NEEDED,
|
|
152
|
+
entry: path.join(__dirname, 'functions', 'custom-ses-receive-function.ts'),
|
|
153
|
+
runtime: lambda.Runtime.NODEJS_18_X,
|
|
154
|
+
logRetention: 1,
|
|
155
|
+
timeout: Duration.seconds(30),
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Note: any additional permissions you need to add to the function yourself!
|
|
159
|
+
customSesReceiveFunction.addToRolePolicy(new iam.PolicyStatement({
|
|
160
|
+
actions: [
|
|
161
|
+
's3:List*',
|
|
162
|
+
],
|
|
163
|
+
resources: ['*'],
|
|
164
|
+
}))
|
|
165
|
+
```
|
|
166
|
+
|
|
100
167
|
#### Cloudformation
|
|
101
168
|
or use it directly a Cloudformation template `yaml` from the URL [here](https://mvc-test4-bucket-eu-west-1.s3.eu-west-1.amazonaws.com/rootmail/0.0.13-DEVELOPMENT/rootmailStack.template.yaml).
|
|
102
169
|
|
|
Binary file
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { S3 } from '@aws-sdk/client-s3';
|
|
2
|
+
import { ParsedMail, simpleParser } from 'mailparser';
|
|
3
|
+
// from https://docs.aws.amazon.com/ses/latest/dg/receiving-email-action-lambda-event.html
|
|
4
|
+
export interface EventRecord {
|
|
5
|
+
eventSource: string;
|
|
6
|
+
eventVersion: string;
|
|
7
|
+
ses: Ses;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface Ses {
|
|
11
|
+
mail: Mail;
|
|
12
|
+
receipt: Receipt;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Mail {
|
|
16
|
+
timestamp: string;
|
|
17
|
+
source: string;
|
|
18
|
+
messageId: string;
|
|
19
|
+
destination: string[];
|
|
20
|
+
headersTruncated: boolean;
|
|
21
|
+
headers: Header[];
|
|
22
|
+
commonHeaders: CommonHeaders;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Header {
|
|
26
|
+
name: string;
|
|
27
|
+
value: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface CommonHeaders {
|
|
31
|
+
returnPath: string;
|
|
32
|
+
from: string[];
|
|
33
|
+
date: string;
|
|
34
|
+
to: string[];
|
|
35
|
+
messageId: string;
|
|
36
|
+
subject: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface Receipt {
|
|
40
|
+
timestamp: string;
|
|
41
|
+
processingTimeMillis: number;
|
|
42
|
+
recipients: string[];
|
|
43
|
+
spamVerdict: Verdict;
|
|
44
|
+
virusVerdict: Verdict;
|
|
45
|
+
spfVerdict: Verdict;
|
|
46
|
+
dkimVerdict: Verdict;
|
|
47
|
+
dmarcVerdict: Verdict;
|
|
48
|
+
action: Action;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface Verdict {
|
|
52
|
+
status: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface Action {
|
|
56
|
+
type: string;
|
|
57
|
+
functionArn: string;
|
|
58
|
+
invocationType: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface SESEventRecordsToLambda {
|
|
62
|
+
Records: EventRecord[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const emailBucket = process.env.EMAIL_BUCKET;
|
|
66
|
+
const emailBucketArn = process.env.EMAIL_BUCKET_ARN;
|
|
67
|
+
const s3 = new S3();
|
|
68
|
+
|
|
69
|
+
export const handler = async (event: SESEventRecordsToLambda) => {
|
|
70
|
+
for (const record of event.Records) {
|
|
71
|
+
|
|
72
|
+
const id = record.ses.mail.messageId;
|
|
73
|
+
const key = `RootMail/${id}`;
|
|
74
|
+
const response = await s3.getObject({ Bucket: emailBucket as string, Key: key });
|
|
75
|
+
|
|
76
|
+
const msg: ParsedMail = await simpleParser(response.Body as unknown as Buffer);
|
|
77
|
+
|
|
78
|
+
let title = msg.subject;
|
|
79
|
+
console.log(`Title: ${title} from emailBucketArn: ${emailBucketArn}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// test custom permission to list s3 buckets
|
|
83
|
+
const buckets = await s3.listBuckets({});
|
|
84
|
+
if (!buckets.Buckets) {
|
|
85
|
+
console.log('No buckets found');
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
console.log('Buckets:');
|
|
89
|
+
for (const bucket of buckets.Buckets || []) {
|
|
90
|
+
console.log(bucket.Name);
|
|
91
|
+
}
|
|
92
|
+
};
|