@fntio/infra-client
Version:
Self-hosted AWS access portal: credential vending, target-account trust, and grant-driven private networking as CDK stacks and constructs.
85 lines (84 loc) • 3.8 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppFunction = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const cdk = __importStar(require("aws-cdk-lib"));
const lambda = __importStar(require("aws-cdk-lib/aws-lambda"));
const aws_logs_1 = require("aws-cdk-lib/aws-logs");
const pkg_paths_1 = require("../pkg-paths");
/** Node Lambda over PREBUILT bundles — identical bytes wherever the synth
* runs. Extends lambda.Function so the CFN logical ids never moved. */
class AppFunction extends lambda.Function {
constructor(scope, id, props) {
const { asset, handler, ...rest } = props;
const dir = path.join((0, pkg_paths_1.packageRoot)(), "assets", asset);
if (!fs.existsSync(path.join(dir, "index.js"))) {
throw new Error(`Missing prebuilt bundle assets/${asset}/index.js — run ` +
`"node scripts/bundle-lambdas.mjs" in the package first (cdk.json ` +
`and prepack run it automatically).`);
}
// npm strips `node_modules` at EVERY depth from tarballs — deps ship as
// `vendor_modules` and local bundling restores the runtime name.
const vendor = path.join(dir, "vendor_modules");
const code = fs.existsSync(vendor)
? lambda.Code.fromAsset(dir, {
bundling: {
image: cdk.DockerImage.fromRegistry("scratch"), // never used
local: {
tryBundle(outputDir) {
fs.cpSync(dir, outputDir, { recursive: true });
fs.renameSync(path.join(outputDir, "vendor_modules"), path.join(outputDir, "node_modules"));
return true;
},
},
},
})
: lambda.Code.fromAsset(dir);
super(scope, id, {
runtime: lambda.Runtime.NODEJS_22_X,
// CDK-named group (never collides with pre-existing /aws/lambda/<fn>
// ones); three months bounds cost, default is never-expire.
logGroup: new aws_logs_1.LogGroup(scope, `${id}Logs`, {
retention: aws_logs_1.RetentionDays.THREE_MONTHS,
}),
code,
handler: `index.${handler ?? "handler"}`,
...rest,
});
}
}
exports.AppFunction = AppFunction;