export const meta = {
  title: `Export Amplify project to CDK`,
  description: `Export your Amplify CLI-generated backends as a Cloud Development Kit (CDK) stack and incorporate it 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.`,
};

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. 

```bash
amplify 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:

```bash
amplify-export-myAmplifyProject/
├── <YOUR_AMPLIFY_CATEGORIES>/
│   ├──...
│   ├──...
│   └──...
├── amplify-export-manifest.json
├── category-stack-mapping.json
├── export-tags.json
└── 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:

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

Then, import and initialize a new `AmplifyExportedBackend` stack:

```ts
import { AmplifyExportedBackend } from '@aws-amplify/cdk-exported-backend'
import * as path from 'path' // To resolve the path to your exported Amplify backend assets

...

const amplifyBackend = new AmplifyExportedBackend(this, "amplifyExportedBackend", {
  amplifyEnvironment: "dev", // Specify your Amplify environment
  path: path.resolve(__dirname, 'amplify-export-<YOUR_AMPLIFY_PROJECT_NAME>')
})
```

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

Deploy the CDK app:

```bash
npx 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:

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

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`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html).

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