aws-amplify / amplify-backend

Home to all tools related to Amplify's code-first DX (Gen 2) for building fullstack apps on AWS
Apache License 2.0
185 stars 62 forks source link

Export Amplify Gen2 backend to CDK #2123

Open muvashi opened 1 month ago

muvashi commented 1 month ago

Environment information

.

Describe the feature

The requirement is to export the Amplify Gen 2 application and import and use in CDK. It is supported in Gen1 :-

https://docs.amplify.aws/gen1/react/tools/cli/usage/export-to-cdk/

But there does not seem to be any documentation on how to do this with Gen 2.

If this is not supported, can we somehow pass Amplify gen2 data schema under definition while using Amplify GraphQL API Construct.

https://constructs.dev/packages/@aws-amplify/graphql-api-construct/v/1.16.0-gen2-migration-1015.0?lang=typescript

Use case

.

ykethan commented 1 month ago

Marking this as feature request

chrisbeyer commented 1 month ago

Thanks for adding this as a feature request. Like many companies we use Amplify to rapidly prototype an new app or feature. But when it comes to deploying we need the ability to export to CDK and deploy to multiple customer accounts using CDK. This works really well in Gen 1 using https://constructs.dev/packages/@aws-amplify/graphql-api-construct/v/1.16.0-gen2-migration-1015.0?lang=typescript. Just need the ability to do the same in Gen 2. Thanks.

josefaidt commented 1 month ago

Hey @muvashi and @chrisbeyer :wave: thanks for filing this and chiming in! Amplify Gen 2 is built atop of CDK and enables the ease of extensibility with existing CDK constructs and the ability to deploy with Amplify managing CDK context. We're actively thinking about additional enhancements to allow more control over CDK tooling behavior, but out of curiosity is there a particular feature you're looking for?

@muvashi you can supply the typed schema to the construct by calling .transform() on the schema return

import { App, Stack, Duration } from "aws-cdk-lib/core"
import {
  AmplifyGraphqlApi,
  AmplifyGraphqlDefinition,
} from "@aws-amplify/graphql-api-construct"
import { schema } from "./schema"

const app = new App()
const stack = new Stack(app, "DemoAmplifyDataConstructWithSchema")

new AmplifyGraphqlApi(stack, "GraphqlApi", {
  authorizationModes: {
    apiKeyConfig: {
      expires: Duration.days(30),
    },
  },
  definition: AmplifyGraphqlDefinition.fromString(schema.transform().schema),
})
chrisbeyer commented 3 weeks ago

Hi @josefaidt ,

import { App, Stack, Duration } from "aws-cdk-lib/core"
import {
  AmplifyGraphqlApi,
  AmplifyGraphqlDefinition,
} from "@aws-amplify/graphql-api-construct"
import { schema } from "./schema"

const app = new App()
const stack = new Stack(app, "DemoAmplifyDataConstructWithSchema")

new AmplifyGraphqlApi(stack, "GraphqlApi", {
  authorizationModes: {
    apiKeyConfig: {
      expires: Duration.days(30),
    },
  },
  definition: AmplifyGraphqlDefinition.fromString(schema.transform().schema),
})

That's great! Thanks for providing your example. We are working with @muvashi in the AWS Support case to try and get this working in our CDK project but are getting strange errors when we try to import the schema.

Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/@aws-amplify/backend/lib/index.js

We're actively thinking about additional enhancements to allow more control over CDK tooling behavior, but out of curiosity is there a particular feature you're looking for?

The main thing we really need is the ability to prototype a GraphQL API POC quickly in Amplify, then be able to export that out and use the schema in our proper CDK solution.

We typically have...

  1. Amplify POC Project
  2. Proper AWS CDK Project Used for deploying solutions to multiple customer AWS accounts

Kind regards,

Chris

josefaidt commented 3 weeks ago

Hey @chrisbeyer thanks for sharing those details! The error you're experiencing comes from the distribution of the backend package as ESM-only. You will need to set "type": "module" in your package.json to resolve

chrisbeyer commented 2 weeks ago

Thanks @josefaidt . We have tried adding "type": "module" in the root /package.json in our CDK project. However we now get TypeError: Unknown file extension ".ts".

Do you have an example of this working that we could clone?

Suspect there must be something different in your tsconfig.json file as well to get this to work with CDK.