cdk-nextjs
Version:
Deploy Next.js apps on AWS with CDK
176 lines (175 loc) • 7.21 kB
TypeScript
import { DockerImageAsset, Platform } from "aws-cdk-lib/aws-ecr-assets";
import { AssetImageCodeProps, DockerImageCode } from "aws-cdk-lib/aws-lambda";
import { Construct } from "constructs";
import { NextjsType } from "../constants";
import { OptionalDockerImageAssetProps } from "../generated-structs/OptionalDockerImageAssetProps";
import { NextjsBaseProps } from "../root-constructs/nextjs-base-construct";
export interface BuilderImageProps {
/**
* Build Args to be passed to `docker build` command.
* @see https://docs.docker.com/build/building/variables/#build-arguments
*/
readonly buildArgs?: Record<string, string>;
/**
* `docker build ...` command to run in {@link NextBaseProps.buildContext}.
* Default interpolates other props. If you override, other props will have
* no effect on command.
*/
readonly command?: string;
/**
* Environment variables names to pass from host to container during build process.
*
* These variable names will be set before the build command in builder.Dockerfile
* like: `API_KEY="MY_API_KEY" npm run build`
*
* @example ["MY_API_KEY"]
*/
readonly envVarNames?: string[];
/**
* Lines in .dockerignore file which will be created in your {@link NextBaseProps.buildContext}
* @default ["node_modules", ".git", ".gitignore", ".md"]
*/
readonly exclude?: string[];
/**
* Name of Dockerfile in builder build context. If specified, you are responsible
* for ensuring it exists in build context before construct is instantiated.
* @default "builder.Dockerfile"
*/
readonly file?: string;
readonly platform?: Platform;
/**
* Skip building the builder image.
* @default false
*/
readonly skipBuild?: boolean;
}
export interface NextjsBuildOverrides {
readonly nextjsContainersDockerImageAssetProps?: OptionalDockerImageAssetProps;
readonly nextjsFunctionsAssetImageCodeProps?: AssetImageCodeProps;
readonly nextjsAssetDeploymentAssetImageCodeProps?: AssetImageCodeProps;
/**
* Default folder for build context is the "lib/nextjs-build" folder in the
* installed cdk-nextjs library which has the "global-functions.Dockerfile".
* Note, if you specify this then you're responsible for ensuring the dockerfile
* is present in the build context directory and any referenced files are
* present as well. You can specify dockerfile name with adjacent
* `nextjsFunctionsAssetImageCodeProps.file` property.
* @default "cdk-nextjs/lib/nextjs-build"
*/
readonly functionsImageBuildContext?: string;
/**
* Default folder for build context is the "lib/nextjs-build" folder in the
* installed cdk-nextjs library which has the "assets-deployment.Dockerfile".
* Note, if you specify this then you're responsible for ensuring the dockerfile
* is present in the build context directory and any referenced files are
* present as well. You can specify dockerfile name with adjacent
* `nextjsAssetDeploymentAssetImageCodeProps.file` property.
* @default "cdk-nextjs/lib/nextjs-build"
*/
readonly assetsDeploymentImageBuildContext?: string;
/**
* Default folder for build context is the "assets/lambdas/assets-deployment/assets-deployment.lambda" folder in the
* installed cdk-nextjs library which has the "{...}-containers.Dockerfile".
* Note, if you specify this then you're responsible for ensuring the dockerfile
* is present in the build context directory and any referenced files are
* present as well. You can specify dockerfile name with adjacent
* `nextjsContainersDockerImageAssetProps.file` property.
* @default "cdk-nextjs/lib/nextjs-build"
*/
readonly containersImageBuildContext?: string;
}
export interface NextjsBuildProps {
/**
* @see {@link NextjsBaseProps["buildCommand"]}
*/
readonly buildCommand: NextjsBaseProps["buildCommand"];
/**
* @see {@link NextjsBaseProps["buildContext"]}
*/
readonly buildContext: NextjsBaseProps["buildContext"];
/**
*
*/
readonly builderImageProps?: BuilderImageProps;
/**
* @see {@link NextjsBaseProps.relativePathToPackage}
*/
readonly relativePathToPackage?: NextjsBaseProps["relativePathToPackage"];
readonly nextjsType: NextjsType;
readonly overrides?: NextjsBuildOverrides;
}
export interface PublicDirEntry {
readonly name: string;
readonly isDirectory: boolean;
}
/**
* Builds Next.js assets.
* @link https://nextjs.org/docs/pages/api-reference/next-config-js/output
*/
export declare class NextjsBuild extends Construct {
/**
* Image alias of builder image Next.js app which is built for other images to be
* built `FROM`. This image isn't built with CDK Assets construct b/c it
* doesn't need to be uploaded to ECR. We still need to include slice of
* `node.addr` in tag in case multiple cdk-nextjs constructs are used.
*/
builderImageAlias: string;
/**
* Unique id for Next.js build. Used to partition EFS FileSystem.
*/
buildId: string;
/**
* Hash of builder image which will change whenever the image changes. Useful
* for passing to properties of custom resources that depend upon the builder
* image to re-run when build image changes.
*/
buildImageDigest: string;
/**
* Docker image built if using Fargate.
*/
imageForNextjsContainers?: DockerImageAsset;
/**
* Docker image built if using Lambda.
*/
imageForNextjsFunctions?: DockerImageCode;
/**
* Docker image built for `NextjsAssetsDeployment`
*/
imageForNextjsAssetsDeployment: DockerImageCode;
/**
* Absolute path to public. Use by CloudFront/ALB to create behaviors/rules
* @example "/Users/john/myapp/public"
*/
publicDirEntries: PublicDirEntry[];
/**
* The entrypoint JavaScript file used as an argument for Node.js to run the
* Next.js standalone server relative to the standalone directory.
* @example "./server.js"
* @example "./packages/ui/server.js" (monorepo)
*/
relativePathToEntrypoint: string;
/**
* Repository name for the builder image.
*/
private builderImageRepo;
private containerRuntime;
private props;
private relativePathToPackage;
constructor(scope: Construct, id: string, props: NextjsBuildProps);
private getRelativeEntrypointPath;
/**
* A builder or base image needs to be created so that the same image can be
* built `FROM` for `NextjsFunctions` or `NextjsContainers` and `NextjsAssetsDeployment`.
* This image doesn't need to be uploaded to ECR so we're "manually" creating
* it with `execSync` and other images will be built `FROM` it.
*/
private createBuilderImage;
private injectBuilderDockerfileEnvVars;
private createBuildArgStr;
private getBuilderImageDigest;
private getPublicDirEntries;
private getBuildId;
private createImageForNextjsContainers;
private createImageForNextjsFunctions;
private createImageForNextjsAssetsDeployment;
}