Skip to content

Commit

Permalink
AWS - deploy pure ESM code for Lambda and client, on node 20 (#279)
Browse files Browse the repository at this point in the history
* upgrade aws-cdk-lib and aws-sdk, add package.json to build to get esm enabled in lambda, move esbuild dep to top of monorepo, add node 20 runtime

* update data scripts to work in monorepo

* fix precalc script env

* build - create isolated mini package per function with own entry point. Add type assertion to language json import
  • Loading branch information
twelch authored Apr 17, 2024
1 parent 6b745bf commit 68fd2f8
Show file tree
Hide file tree
Showing 16 changed files with 1,521 additions and 1,010 deletions.
2,183 changes: 1,279 additions & 904 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.21.0",
"@craftamap/esbuild-plugin-html": "^0.6.1",
"@types/fs-extra": "^11.0.1",
"@types/react": "^16.14.0",
"@types/request": "^2.48.8",
"babel-plugin-i18next-extract": "0.9.0",
"esbuild": "0.20.2",
"esbuild-plugin-inline-image": "^0.0.9",
"esbuild-plugins-node-modules-polyfill": "^1.6.3",
"fs-extra": "^11.1.0",
"http-server": "^14.1.1",
"husky": "^8.0.0",
Expand Down
10 changes: 2 additions & 8 deletions packages/geoprocessing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@
],
"dependencies": {
"@aws-cdk/assert": "2.68.0",
"@aws-cdk/aws-apigatewayv2-alpha": "2.84.0-alpha.0",
"@aws-cdk/aws-apigatewayv2-integrations-alpha": "2.84.0-alpha.0",
"@babel/core": "^7.13.15",
"@babel/plugin-proposal-class-properties": "^7.13.15",
"@babel/plugin-proposal-nullish-coalescing-operator": "7.16.7",
Expand Down Expand Up @@ -137,9 +135,9 @@
"@types/uuid": "^8.3.0",
"@vitejs/plugin-react": "^4.2.1",
"abortcontroller-polyfill": "^1.4.0",
"aws-cdk-lib": "2.84.0",
"aws-cdk-lib": "2.137.0",
"aws-regions": "2.3.1",
"aws-sdk": "2.1399.0",
"aws-sdk": "2.1599.0",
"babel-loader": "^8.2.2",
"bbox-fns": "^0.19.0",
"bytes": "^3.1.0",
Expand Down Expand Up @@ -228,12 +226,8 @@
"zx": "^4.3.0"
},
"devDependencies": {
"@craftamap/esbuild-plugin-html": "^0.6.1",
"@testing-library/jest-dom": "^6.4.2",
"@types/finalhandler": "^1.2.0",
"esbuild": "0.20.2",
"esbuild-plugin-inline-image": "^0.0.9",
"esbuild-plugins-node-modules-polyfill": "^1.6.3",
"eslint": "^8.57.0",
"eslint-plugin-unicorn": "^51.0.1",
"identity-obj-proxy": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/geoprocessing/scripts/aws/GeoprocessingStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
setupWebSocketFunctionAccess,
} from "./socketApiGateway.js";
import { RestApi } from "aws-cdk-lib/aws-apigateway";
import { WebSocketApi } from "@aws-cdk/aws-apigatewayv2-alpha";
import { WebSocketApi } from "aws-cdk-lib/aws-apigatewayv2";
import {
GpPublicBuckets,
GpProjectFunctions,
Expand Down
2 changes: 1 addition & 1 deletion packages/geoprocessing/scripts/aws/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Duration } from "aws-cdk-lib";

const config = {
STAGE_NAME: "prod",
NODE_RUNTIME: Runtime.NODEJS_16_X,
NODE_RUNTIME: Runtime.NODEJS_20_X,
SYNC_LAMBDA_TIMEOUT: 10, // seconds
ASYNC_LAMBDA_START_TIMEOUT: 5,
ASYNC_LAMBDA_RUN_TIMEOUT: 60,
Expand Down
43 changes: 36 additions & 7 deletions packages/geoprocessing/scripts/aws/functionResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface CreateFunctionOptions {
}
import path from "path";

const GP_ROOT = process.env.GP_ROOT;

/**
* Create Lambda function constructs
*/
Expand All @@ -47,7 +49,9 @@ export const createFunctions = (
const createRootFunction = (stack: GeoprocessingStack): Function => {
return new Function(stack, "GpServiceRootFunction", {
runtime: config.NODE_RUNTIME,
code: Code.fromAsset(path.join(stack.props.projectPath, ".build")),
code: Code.fromAsset(
path.join(stack.props.projectPath, ".build", "serviceHandlers")
),
functionName: `gp-${stack.props.projectName}-metadata`,
handler: "serviceHandlers.projectMetadata",
});
Expand All @@ -65,7 +69,9 @@ export const createSocketFunctions = (
if (stack.hasAsyncFunctions()) {
const subscribe = new Function(stack, "GpSubscribeHandler", {
runtime: config.NODE_RUNTIME,
code: Code.fromAsset(path.join(stack.props.projectPath, ".build/")),
code: Code.fromAsset(
path.join(stack.props.projectPath, ".build", "connect")
),
handler: "connect.connectHandler",
functionName: `gp-${stack.props.projectName}-subscribe`,
memorySize: config.SOCKET_HANDLER_MEMORY,
Expand All @@ -75,7 +81,9 @@ export const createSocketFunctions = (

const unsubscribe = new Function(stack, "GpUnsubscribeHandler", {
runtime: config.NODE_RUNTIME,
code: Code.fromAsset(path.join(stack.props.projectPath, ".build/")),
code: Code.fromAsset(
path.join(stack.props.projectPath, ".build", "disconnect")
),
handler: "disconnect.disconnectHandler",
functionName: `gp-${stack.props.projectName}-unsubscribe`,
memorySize: config.SOCKET_HANDLER_MEMORY,
Expand All @@ -85,7 +93,9 @@ export const createSocketFunctions = (

const send = new Function(stack, "GpSendHandler", {
runtime: config.NODE_RUNTIME,
code: Code.fromAsset(path.join(stack.props.projectPath, ".build/")),
code: Code.fromAsset(
path.join(stack.props.projectPath, ".build", "sendmessage")
),
handler: "sendmessage.sendHandler",
functionName: `gp-${stack.props.projectName}-send`,
memorySize: config.SOCKET_HANDLER_MEMORY,
Expand Down Expand Up @@ -117,10 +127,15 @@ const createSyncFunctions = (
return syncFunctionMetas.map(
(functionMeta: ProcessingFunctionMetadata, index: number) => {
const rootPointer = getHandlerPointer(functionMeta);
const pkgName = getHandlerPkgName(functionMeta);
const functionName = `gp-${stack.props.projectName}-sync-${functionMeta.title}`;
const codePath = path.join(stack.props.projectPath, ".build", pkgName);
// console.log("codePath", codePath);
// console.log("rootPointer", rootPointer);

const func = new Function(stack, `${functionMeta.title}GpSyncHandler`, {
runtime: config.NODE_RUNTIME,
code: Code.fromAsset(path.join(stack.props.projectPath, ".build")),
code: Code.fromAsset(codePath),
handler: rootPointer,
functionName,
memorySize: functionMeta.memory,
Expand Down Expand Up @@ -148,6 +163,7 @@ const createAsyncFunctions = (
return asyncFunctionMetas.map(
(functionMeta: GeoprocessingFunctionMetadata, index: number) => {
const rootPointer = getHandlerPointer(functionMeta);
const pkgName = getHandlerPkgName(functionMeta);
const startFunctionName = `gp-${stack.props.projectName}-async-${functionMeta.title}-start`;
const runFunctionName = `gp-${stack.props.projectName}-async-${functionMeta.title}-run`;

Expand All @@ -160,7 +176,9 @@ const createAsyncFunctions = (
`${functionMeta.title}GpAsyncHandlerStart`,
{
runtime: config.NODE_RUNTIME,
code: Code.fromAsset(path.join(stack.props.projectPath, ".build")),
code: Code.fromAsset(
path.join(stack.props.projectPath, ".build", pkgName)
),
handler: rootPointer,
functionName: startFunctionName,
memorySize: functionMeta.memory,
Expand All @@ -182,7 +200,9 @@ const createAsyncFunctions = (
`${functionMeta.title}GpAsyncHandlerRun`,
{
runtime: config.NODE_RUNTIME,
code: Code.fromAsset(path.join(stack.props.projectPath, ".build")),
code: Code.fromAsset(
path.join(stack.props.projectPath, ".build", pkgName)
),

handler: rootPointer,
functionName: runFunctionName,
Expand Down Expand Up @@ -226,3 +246,12 @@ export function getHandlerPointer(funcMeta: ProcessingFunctionMetadata) {
.replace(/\.js$/, "")
.replace(/\.ts$/, "")}Handler.handler`;
}

/**
* Returns build package name to look for handler
*/
export function getHandlerPkgName(funcMeta: ProcessingFunctionMetadata) {
return `${funcMeta.handlerFilename
.replace(/\.js$/, "")
.replace(/\.ts$/, "")}`;
}
10 changes: 3 additions & 7 deletions packages/geoprocessing/scripts/aws/socketApiGateway.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { GeoprocessingStack } from "./GeoprocessingStack.js";
import { WebSocketApi, WebSocketStage } from "@aws-cdk/aws-apigatewayv2-alpha";
import { WebSocketLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
import { WebSocketApi, WebSocketStage } from "aws-cdk-lib/aws-apigatewayv2";
import { WebSocketLambdaIntegration } from "aws-cdk-lib/aws-apigatewayv2-integrations";
import { Stack } from "aws-cdk-lib";
import config from "./config.js";
import { CfnStage } from "aws-cdk-lib/aws-apigatewayv2";
import {
PolicyStatement,
Effect,
ServicePrincipal,
} from "aws-cdk-lib/aws-iam";
import { PolicyStatement, Effect, ServicePrincipal } from "aws-cdk-lib/aws-iam";
import { Function } from "aws-cdk-lib/aws-lambda";

/**
Expand Down
Loading

0 comments on commit 68fd2f8

Please sign in to comment.