0.0.311 โ€ข Published 7 months ago

@mavogel/awscdk-rootmail v0.0.311

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
7 months ago

Source Build Status ESLint Code Formatting Latest release GitHub npm typescript

awscdk-rootmail

A single email box for all your root user emails in all AWS accounts of the organization.

  • The cdk implementation and adaption of the superwerker rootmail feature.
  • See here for a detailed Architectural Decision Record (ADR)

TL;DR โšก

Each AWS account needs one unique email address (the so-called "AWS account root user email address").

Access to these email addresses must be adequately secured since they provide privileged access to AWS accounts, such as account deletion procedures.

This is why you only need 1 mailing list for the AWS Management (formerly root) account, we recommend the following pattern aws-roots+<uuid>@mycompany.test

!NOTE Maximum 64 characters are allowed for the whole address.

And 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.

Feel free to take a look at the design rootmail-solution-diagram-v1

Usage โœจ

Install the dependencies:

brew install aws-cli node@18 esbuild

You can chose via embedding the construct in your cdk-app or use is directly via Cloudformation.

cdk ๐Ÿค–

  1. To start a new project we recommend using projen.
    1. Create a new projen project
    npx projen new awscdk-app-ts
    1. Add @mavogel/awscdk-rootmail as a dependency to your project in the .projenrc.ts file
    2. Run yarn run projen to install it
  2. In you main.ts file add the following code
import { Rootmail } from '@mavogel/awscdk-rootmail';
import {
  App,
  Stack,
  StackProps,
  aws_route53 as r53,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps = {}) {
    super(scope, id, props);

    const domain = 'mycompany.com' // registered via Route53 in the SAME account

    const hostedZone = r53.HostedZone.fromLookup(this, 'rootmail-parent-hosted-zone', {
      domainName: domain,
    });

    new Rootmail(this, 'rootmail', {
      // 1. a domain you own, registered via Route53 in the SAME account
      domain: domain,
      // 2. so the subdomain will be aws.mycompany.test and
      subdomain: 'aws',
      // 3. wired / delegated automatically to
      wireDNSToHostedZoneID: hostedZone.hostedZoneId,
    });
  }
}
  1. run on your commandline
yarn run deploy
  1. No need to do anything, the NS records are automatically propagated as the parent Hosted Zone is in the same account!
  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.

!TIP Take a look at the solution design here for more details.

cdk with your own receiver function ๐Ÿ—๏ธ

You might also want to pass in you own function on what to do when an EMail is received

!TIP You can add any custom code as receiver function you want.

file functions/custom-ses-receive-function.ts which gets the 2 environment variables populated

  • EMAIL_BUCKET
  • EMAIL_BUCKET_ARN

as well as s3:GetObject on the RootMail/* objects in the created Rootmail S3 bucket.

import { S3 } from '@aws-sdk/client-s3';
import { ParsedMail, simpleParser } from 'mailparser';
// populated by default
const emailBucket = process.env.EMAIL_BUCKET;
const emailBucketArn = process.env.EMAIL_BUCKET_ARN;
const s3 = new S3();

// SESEventRecordsToLambda
// from https://docs.aws.amazon.com/ses/latest/dg/receiving-email-action-lambda-event.html
export const handler = async (event: SESEventRecordsToLambda) => {
    for (const record of event.Records) {
        
        const id = record.ses.mail.messageId;
        const key = `RootMail/${id}`;
        const response = await s3.getObject({ Bucket: emailBucket as string, Key: key });
        
        const msg: ParsedMail = await simpleParser(response.Body as unknown as Buffer);
        
        let title = msg.subject;
        console.log(`Title: ${title} from emailBucketArn: ${emailBucketArn}`);
        // use the content of the email body 
        const body = msg.html;
        // add your custom code here ...

        // dummy example: list s3 buckets
        const buckets = await s3.listBuckets({});
        if (!buckets.Buckets) {
            console.log('No buckets found');
            return;
        }
        console.log('Buckets:');
        for (const bucket of buckets.Buckets || []) {
            console.log(bucket.Name);
        }
    }

};

and you create a separate NodejsFunction as follows with the additionally needed IAM permissions:

const customSesReceiveFunction = new NodejsFunction(stackUnderTest, 'custom-ses-receive-function', {
  functionName: PhysicalName.GENERATE_IF_NEEDED,
  entry: path.join(__dirname, 'functions', 'custom-ses-receive-function.ts'),
  runtime: lambda.Runtime.NODEJS_18_X,
  logRetention: 1,
  timeout: Duration.seconds(30),
});

// Note: any additional permissions you need to add to the function yourself!
customSesReceiveFunction.addToRolePolicy(new iam.PolicyStatement({
  actions: [
    's3:List*',
  ],
  resources: ['*'],
}))

and then pass it into the Rootmail Stack

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps = {}) {
    super(scope, id, props);

    const domain = 'mycompany.test'
    const hostedZone = r53.HostedZone.fromLookup(this, 'rootmail-parent-hosted-zone', {
      domainName: domain,
    });

    const rootmail = new Rootmail(this, 'rootmail-stack', {
      domain: domain;
      autowireDNSParentHostedZoneID: hostedZone.hostedZoneId,
      env: {
        region: 'eu-west-1',
      },
      customSesReceiveFunction: customSesReceiveFunction, // <- pass it in here
    }); 
  }
}

!TIP Take a look at the solution design for external DNS here for more details.

Cloudformation ๐Ÿ“ฆ

or use it directly a Cloudformation template yaml from the URL here.

and fill out the parameters cloudformation-template

Known issues

  • jsii/2071: so adding compilerOptions."esModuleInterop": true, in tsconfig.json is not possible. See aws-cdk usage withtypescript. So we needed to change import from import AWS from 'aws-sdk'; -> import * as AWS from 'aws-sdk'; to be able to compile.

Related projects / questions

  • 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).
  • The question Is it best practise to use a shared mailbox as AWS root user address? from stackoverflow: yes of course you can also use root+alias-1@mycompany.com and root+alias-2@mycompany.com etc. for your root EMail boxes.

๐Ÿš€ Unlock the Full Potential of Your AWS Cloud Infrastructure

Hi, Iโ€™m Manuel, an AWS expert passionate about empowering businesses with scalable, resilient, and cost-optimized cloud solutions. With MV Consulting, I specialize in crafting tailored AWS architectures and DevOps-driven workflows that not only meet your current needs but grow with you.


๐ŸŒŸ Why Work With Me?

โœ”๏ธ Tailored AWS Solutions: Every business is unique, so I design custom solutions that fit your goals and challenges.
โœ”๏ธ Well-Architected Designs: From scalability to security, my solutions align with AWS Well-Architected Framework.
โœ”๏ธ Cloud-Native Focus: I specialize in modern, cloud-native systems that embrace the full potential of AWS.
โœ”๏ธ Business-Driven Tech: Technology should serve your business, not the other way around.


๐Ÿ›  What I Bring to the Table

๐Ÿ”‘ 12x AWS Certifications
Iโ€™m AWS Certified Solutions Architect and DevOps โ€“ Professional and hold numerous additional certifications, so you can trust Iโ€™ll bring industry best practices to your projects. Feel free to explose by badges

โš™๏ธ Infrastructure as Code (IaC)
With deep expertise in AWS CDK and Terraform, I ensure your infrastructure is automated, maintainable, and scalable.

๐Ÿ“ฆ DevOps Expertise
From CI/CD pipelines with GitHub Actions and GitLab CI to container orchestration Kubernetes and others, I deliver workflows that are smooth and efficient.

๐ŸŒ Hands-On Experience
With over 7 years of AWS experience and a decade in the tech world, Iโ€™ve delivered solutions for companies large and small. My open-source contributions showcase my commitment to transparency and innovation. Feel free to explore my GitHub profile


๐Ÿ’ผ Letโ€™s Build Something Great Together

I know that choosing the right partner is critical to your success. When you work with me, youโ€™re not just contracting an engineer โ€“ youโ€™re gaining a trusted advisor and hands-on expert who cares about your business as much as you do.

โœ”๏ธ Direct Collaboration: No middlemen or red tape โ€“ you work with me directly.
โœ”๏ธ Transparent Process: Expect open communication, clear timelines, and visible results.
โœ”๏ธ Real Value: My solutions focus on delivering measurable impact for your business.


๐Ÿ™Œ Acknowledgements

Big shoutout to the amazing team behind Projen!
Their groundbreaking work simplifies cloud infrastructure projects and inspires us every day. ๐Ÿ’ก

Author

Manuel Vogel

npm.io npm.io

0.0.311

7 months ago

0.0.310

7 months ago

0.0.309

7 months ago

0.0.205

1 year ago

0.0.204

1 year ago

0.0.203

1 year ago

0.0.202

1 year ago

0.0.209

1 year ago

0.0.208

1 year ago

0.0.207

1 year ago

0.0.206

1 year ago

0.0.201

1 year ago

0.0.200

1 year ago

0.0.216

1 year ago

0.0.215

1 year ago

0.0.214

1 year ago

0.0.213

1 year ago

0.0.219

1 year ago

0.0.218

1 year ago

0.0.217

1 year ago

0.0.212

1 year ago

0.0.211

1 year ago

0.0.210

1 year ago

0.0.227

1 year ago

0.0.226

1 year ago

0.0.225

1 year ago

0.0.224

1 year ago

0.0.229

1 year ago

0.0.228

1 year ago

0.0.223

1 year ago

0.0.222

1 year ago

0.0.221

1 year ago

0.0.220

1 year ago

0.0.197

1 year ago

0.0.196

1 year ago

0.0.195

1 year ago

0.0.194

1 year ago

0.0.199

1 year ago

0.0.198

1 year ago

0.0.304

8 months ago

0.0.303

9 months ago

0.0.302

9 months ago

0.0.301

9 months ago

0.0.308

7 months ago

0.0.307

8 months ago

0.0.306

8 months ago

0.0.305

8 months ago

0.0.300

9 months ago

0.0.279

11 months ago

0.0.274

12 months ago

0.0.273

12 months ago

0.0.272

12 months ago

0.0.271

12 months ago

0.0.278

11 months ago

0.0.277

12 months ago

0.0.276

12 months ago

0.0.275

12 months ago

0.0.270

12 months ago

0.0.285

11 months ago

0.0.284

11 months ago

0.0.283

11 months ago

0.0.282

11 months ago

0.0.289

11 months ago

0.0.288

11 months ago

0.0.287

11 months ago

0.0.286

11 months ago

0.0.281

11 months ago

0.0.280

11 months ago

0.0.296

10 months ago

0.0.295

10 months ago

0.0.294

11 months ago

0.0.293

11 months ago

0.0.299

9 months ago

0.0.298

10 months ago

0.0.297

10 months ago

0.0.292

11 months ago

0.0.291

11 months ago

0.0.290

11 months ago

0.0.238

1 year ago

0.0.237

1 year ago

0.0.236

1 year ago

0.0.235

1 year ago

0.0.239

1 year ago

0.0.230

1 year ago

0.0.234

1 year ago

0.0.233

1 year ago

0.0.232

1 year ago

0.0.231

1 year ago

0.0.249

1 year ago

0.0.248

1 year ago

0.0.247

1 year ago

0.0.246

1 year ago

0.0.241

1 year ago

0.0.240

1 year ago

0.0.245

1 year ago

0.0.244

1 year ago

0.0.243

1 year ago

0.0.242

1 year ago

0.0.259

12 months ago

0.0.258

12 months ago

0.0.257

12 months ago

0.0.252

1 year ago

0.0.251

1 year ago

0.0.250

1 year ago

0.0.256

12 months ago

0.0.255

12 months ago

0.0.254

1 year ago

0.0.253

1 year ago

0.0.269

12 months ago

0.0.268

12 months ago

0.0.263

12 months ago

0.0.262

12 months ago

0.0.261

12 months ago

0.0.260

12 months ago

0.0.267

12 months ago

0.0.266

12 months ago

0.0.265

12 months ago

0.0.264

12 months ago

0.0.193

1 year ago

0.0.192

1 year ago

0.0.186

1 year ago

0.0.185

1 year ago

0.0.189

1 year ago

0.0.188

1 year ago

0.0.187

1 year ago

0.0.191

1 year ago

0.0.190

1 year ago

0.0.184

1 year ago

0.0.179

1 year ago

0.0.183

1 year ago

0.0.182

1 year ago

0.0.181

1 year ago

0.0.180

1 year ago

0.0.178

1 year ago

0.0.177

1 year ago

0.0.175

1 year ago

0.0.174

1 year ago

0.0.173

1 year ago

0.0.172

1 year ago

0.0.176

1 year ago

0.0.171

1 year ago

0.0.159

1 year ago

0.0.158

1 year ago

0.0.153

1 year ago

0.0.152

1 year ago

0.0.151

1 year ago

0.0.157

1 year ago

0.0.156

1 year ago

0.0.155

1 year ago

0.0.154

1 year ago

0.0.169

1 year ago

0.0.164

1 year ago

0.0.163

1 year ago

0.0.162

1 year ago

0.0.161

1 year ago

0.0.168

1 year ago

0.0.167

1 year ago

0.0.166

1 year ago

0.0.165

1 year ago

0.0.160

1 year ago

0.0.170

1 year ago

0.0.150

1 year ago

0.0.149

1 year ago

0.0.148

1 year ago

0.0.147

1 year ago

0.0.146

1 year ago

0.0.145

1 year ago

0.0.144

1 year ago

0.0.143

1 year ago

0.0.139

1 year ago

0.0.138

1 year ago

0.0.137

1 year ago

0.0.142

1 year ago

0.0.141

1 year ago

0.0.140

1 year ago

0.0.84

1 year ago

0.0.85

1 year ago

0.0.86

1 year ago

0.0.87

1 year ago

0.0.88

1 year ago

0.0.89

1 year ago

0.0.80

2 years ago

0.0.81

2 years ago

0.0.82

2 years ago

0.0.83

1 year ago

0.0.73

2 years ago

0.0.74

2 years ago

0.0.75

2 years ago

0.0.76

2 years ago

0.0.77

2 years ago

0.0.78

2 years ago

0.0.79

2 years ago

0.0.70

2 years ago

0.0.71

2 years ago

0.0.72

2 years ago

0.0.62

2 years ago

0.0.63

2 years ago

0.0.64

2 years ago

0.0.65

2 years ago

0.0.66

2 years ago

0.0.67

2 years ago

0.0.68

2 years ago

0.0.69

2 years ago

0.0.61

2 years ago

0.0.106

1 year ago

0.0.105

1 year ago

0.0.104

1 year ago

0.0.103

1 year ago

0.0.109

1 year ago

0.0.108

1 year ago

0.0.107

1 year ago

0.0.102

1 year ago

0.0.101

1 year ago

0.0.100

1 year ago

0.0.117

1 year ago

0.0.116

1 year ago

0.0.115

1 year ago

0.0.114

1 year ago

0.0.119

1 year ago

0.0.118

1 year ago

0.0.113

1 year ago

0.0.112

1 year ago

0.0.111

1 year ago

0.0.110

1 year ago

0.0.128

1 year ago

0.0.127

1 year ago

0.0.126

1 year ago

0.0.125

1 year ago

0.0.129

1 year ago

0.0.120

1 year ago

0.0.124

1 year ago

0.0.123

1 year ago

0.0.122

1 year ago

0.0.121

1 year ago

0.0.136

1 year ago

0.0.131

1 year ago

0.0.130

1 year ago

0.0.135

1 year ago

0.0.134

1 year ago

0.0.133

1 year ago

0.0.132

1 year ago

0.0.95

1 year ago

0.0.96

1 year ago

0.0.97

1 year ago

0.0.98

1 year ago

0.0.99

1 year ago

0.0.90

1 year ago

0.0.91

1 year ago

0.0.92

1 year ago

0.0.93

1 year ago

0.0.94

1 year ago

0.0.60

2 years ago

0.0.59

2 years ago

0.0.58

2 years ago

0.0.57

2 years ago

0.0.56

2 years ago

0.0.55

2 years ago

0.0.54

2 years ago

0.0.52

2 years ago

0.0.53

2 years ago

0.0.51

2 years ago

0.0.50

2 years ago

0.0.49

2 years ago

0.0.48

2 years ago

0.0.47

2 years ago

0.0.46

2 years ago

0.0.45

2 years ago

0.0.44

2 years ago

0.0.43

2 years ago

0.0.42

2 years ago

0.0.41

2 years ago

0.0.40

2 years ago

0.0.39

2 years ago

0.0.38

2 years ago

0.0.37

2 years ago

0.0.36

2 years ago

0.0.35

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.32

2 years ago

0.0.31

2 years ago

0.0.30

2 years ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.15

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago