Skip to content

Deploy with AWS CDK

This is the code-first path to a ServerlessInbox deployment: you write a small CDK app that composes the published MailServer and MailServerUserPool constructs yourself. If you’d rather not write CDK code, use Install ServerlessInbox Standard instead, which deploys the same constructs via a pre-built CloudFormation template.

  • Node.js and the AWS CDK Toolkit: npm install -g aws-cdk
  • A Route 53 hosted zone for your domain, in the same AWS account you deploy into. The zone must exist before you deploy — your stack looks it up by ID, the same requirement as the CloudFormation template’s install path. See Create a Route 53 hosted zone if you don’t have one yet.
  • Region: eu-west-1 only, for now. Like the CloudFormation-based install, ServerlessInbox currently deploys in the EU (Ireland) region. More regions are added on request — ask in GitHub Discussions if you need one. The CDK path takes a region setting, but no other region has been validated yet.

The MailServer construct takes its identity provider as a prop rather than creating one, so it needs an IdP addon package alongside it. @serverlessinbox/mail-server-cognito is the addon shipped today.

Terminal window
npm install aws-cdk-lib constructs @serverlessinbox/mail-server @serverlessinbox/mail-server-cognito
import * as cdk from 'aws-cdk-lib';
import { MailServerStack } from '../lib/stack';
const app = new cdk.App();
new MailServerStack(app, 'MailServerStack', {
env: { region: 'eu-west-1' },
});
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { MailServerUserPool } from '@serverlessinbox/mail-server-cognito';
import { HostedZoneLookup, MailServer } from '@serverlessinbox/mail-server';
export class MailServerStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Looks the zone up once, at deploy time. MailServer derives the domain name from this
// zone — there is no separate domain-name prop to fill in.
const zone = new HostedZoneLookup(this, 'HostedZone', {
hostedZoneId: 'Z1D633PJN98FT9',
});
const userPool = new MailServerUserPool(this, 'MailServerUserPool', {
domain: { kind: 'cognito', cognitoDomainPrefix: 'my-mail-server' },
});
// Create the first admin user. There is no automatic bootstrap user for a raw CDK
// deployment (unlike the CloudFormation template) — you provision it explicitly.
userPool.addBootstrapUser({
username: 'admin',
groups: ['admin'],
temporaryPassword: 'MailServer-Admin-1!',
});
const mailServer = new MailServer(this, 'MailServer', {
hostedZone: zone.hostedZone,
authProvider: userPool,
});
// The Admin UI and Webmail UI are both optional; their URLs exist only when the UI is deployed.
if (mailServer.urls.adminUi) {
new cdk.CfnOutput(this, 'AdminUiUrl', { value: mailServer.urls.adminUi });
}
if (mailServer.urls.webmailUi) {
new cdk.CfnOutput(this, 'WebmailUrl', { value: mailServer.urls.webmailUi });
}
new cdk.CfnOutput(this, 'JmapApiUrl', { value: mailServer.urls.jmapApi });
new cdk.CfnOutput(this, 'AdminApiUrl', { value: mailServer.urls.adminApi });
}
}

Replace the hostedZoneId passed to HostedZoneLookup, cognitoDomainPrefix, username, and temporaryPassword with values appropriate for your deployment. Choose your own temporary password rather than reusing the example above — it will otherwise be guessable from this documentation.

Terminal window
cdk deploy

CDK displays a summary of the IAM and security changes to be applied and prompts for confirmation before creating any resources.

  1. Note the AdminUiUrl, WebmailUrl, JmapApiUrl, and AdminApiUrl values printed in the cdk deploy output (or in the CloudFormation console’s Outputs tab for MailServerStack). AdminUiUrl and WebmailUrl appear only when the corresponding UI is deployed; the example above deploys both.
  2. Open AdminUiUrl and log in with the username and temporary password you set on addBootstrapUser. The identity provider will require you to set a new password immediately.
  3. Configure mail domain DNS records. In the Admin UI, go to the Domains section, find your domain, and click Create records in Route 53. This creates the mail-handling records (MX, SPF, DKIM, DMARC) required for inbound and outbound mail. Until you apply these records, mail routing to your domain will not work.
  4. Verify inbound email by sending a test message to a mailbox on your domain.
  5. Add SES verified email identities via the Admin UI for outbound sending while your account remains in SES sandbox mode (see below).

By default, all AWS accounts are placed in SES sandbox mode: outbound email can only be sent to verified email addresses until AWS grants your account production access. See SES sandbox for what this means while you’re testing and how to work within it — the Admin UI’s Sending page is the same one used by the CloudFormation install path.