Page updated Jan 16, 2024

Export Amplify project to CDK

Export Amplify CLI-generated backends as a Cloud Development Kit (CDK) stack and incorporate into existing CDK deployment pipelines. This capability allows frontend developers to build their app backend quickly and, each time it is ready to ship, hand it over to DevOps teams to deploy to production.

1amplify export --out <your-cdk-project-location>

The command above exports your Amplify projects with CDK-compatible CloudFormation files and assets.

Note: notifications category is not supported for export.

In the exported location you should see a file structure like this:

1amplify-export-myAmplifyProject/
2├── <YOUR_AMPLIFY_CATEGORIES>/
3│ ├──...
4│ ├──...
5│ └──...
6├── amplify-export-manifest.json
7├── category-stack-mapping.json
8├── export-tags.json
9└── root-stack-template.json

Use an exported Amplify backend in AWS Cloud Development Kit (CDK)

amplify export uses the current state of the Amplify backend to build, package, and generate the CloudFormation files and assets to the provided path.

To integrate the Amplify backend into your CDK app, install the AmplifyExportedBackend CDK construct:

1npm i @aws-amplify/cdk-exported-backend

Then, import and initialize a new AmplifyExportedBackend stack:

1import { AmplifyExportedBackend } from '@aws-amplify/cdk-exported-backend'
2import * as path from 'path' // To resolve the path to your exported Amplify backend assets
3
4...
5
6const amplifyBackend = new AmplifyExportedBackend(this, "amplifyExportedBackend", {
7 amplifyEnvironment: "dev", // Specify your Amplify environment
8 path: path.resolve(__dirname, 'amplify-export-<YOUR_AMPLIFY_PROJECT_NAME>')
9})

Specify the amplifyEnvironment parameter to return the Amplify stack for the corresponding backend environment created through (amplify env add).

Deploy the CDK app:

1npx cdk deploy

Remember: "export" is not an "eject" workflow in the sense that "export" can iteratively apply Amplify CLI changes as they are ready to ship. Developers can use the Amplify CLI to iterate on their app backend quickly and prior to each new production deployment, run "amplify export" to provide an exported Amplify backend for an existing deployment system.

Use CDK for cross-account or cross-region Amplify backend deployments

To deploy an Amplify backend across accounts or regions, you must ensure that the amplifyEnvironment parameter is globally unique across all of AWS. This is due to the underlying resources, such as S3 buckets and IAM roles, need to be globally unique.

One way to ensure that is to use the AWS Account ID or Region as the Amplify environment identifier:

1const amplifyBackend = new AmplifyExportedBackend(this, "amplifyExportedBackend", {
2 amplifyEnvironment: cdk.Stack.of(this).region + cdk.Stack.of(this).account, // <---------
3 path: path.resolve(__dirname, 'amplify-export-<YOUR_AMPLIFY_PROJECT_NAME>')
4})

In order to get the region and account ID strings to populate in CDK, you need to provide the env parameter on the parent stack. The amplifyEnvironment can't include a CloudFormation Ref.

1const service = new MyAmplifyStack(this, 'AmplifyStack', {
2 env: {
3 account: process.env.CDK_DEFAULT_ACCOUNT, // or for example: "172387324923"
4 region: process.env.CDK_DEFAULT_REGION, // or "us-east-1"
5 }
6});