serverless-next.js icon indicating copy to clipboard operation
serverless-next.js copied to clipboard

CDK Construct for users of python CDK

Open lbustelo opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe. The CDK constructs in this lib are only usable in a Typescript CDK project. Would like to be able to use them in a Python based CDK project.

Describe the solution you'd like AWS has a jsii project that is used as part of CDK to create bindings for other languages. This involved generating the bindings and publishing the projects to corresponding repositories. For Python, Pypi would be the appropriate location.

Describe alternatives you've considered Would have to recreate our existing CDK set of utilities in Typescript or deploy the Next.js application separately.

Additional context Are there any plans to do such thing? Any recommendations?

lbustelo avatar Jan 18 '22 22:01 lbustelo

This does not seem to be in the radar for the contributors, but I wanted to update some of my findings here.

As it stands, the current construct cannot be processed by JSII. The class is just not compatible with JSSI conventions and I suspect too much work to make it so. Having said that, it is still possible to expose this construct to other languages by doing the following. NOTE that my goal was to not have to fork and create my own version of this repo.

  1. Create a separate project modeled after a new CDK Construct.
  2. Add @sls-next/cdk-construct as a dependency.
  3. To have JSII ignore the dependency in its processing, mark it as a bundled dependency. See JSII dependency requirements.
  4. Create a new module, import NextJSLambdaEdge
  5. Create a new class that extends core:Construct and then instantiate NextJSLambdaEdge in the constructor. Something like this:
import { NextJSLambdaEdge } from '@sls-next/cdk-construct';
import * as cdk from "@aws-cdk/core";

export class NextJs extends cdk.Construct {

    private nestJs: NextJSLambdaEdge;

    constructor(scope: cdk.Construct, id: string, build_dir: string) {
        super(scope, `${id}_proxy`);
        this.nestJs = new NextJSLambdaEdge(this, id, {
            serverlessBuildOutDir: build_dir
        });
    }
    public bucketName() {
        return this.nestJs.bucket.bucketArn;
    }
}

  1. npm run build; npm run package to build and package the library. You will end up with distributions in different language based on JSII configurations. Similar to this.

lbustelo avatar Jan 25 '22 20:01 lbustelo