Skip to content

Commit

Permalink
integration test ensures that NodeJSFunction lambda is bundled
Browse files Browse the repository at this point in the history
  • Loading branch information
iankhou committed Feb 6, 2025
1 parent c39b0b3 commit 2becfd2
Showing 1 changed file with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as path from 'path';
import { App, Stack, StackProps } from 'aws-cdk-lib';
import * as fs from 'fs';
import { App, Stack, StackProps, ValidationError } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as lambdaNodeJs from 'aws-cdk-lib/aws-lambda-nodejs';
import { ExpectedResult, IntegTest } from '@aws-cdk/integ-tests-alpha';
import { IFunction, Runtime } from 'aws-cdk-lib/aws-lambda';

Expand All @@ -18,13 +20,13 @@ class TestStack extends Stack {
const uniqueRuntimes: Runtime[] = runtimes.filter((value, index, array) => array.findIndex(value1 => value1.runtimeEquals(value)) === index);

uniqueRuntimes.forEach((runtime) => {
this.lambdaFunctions.push(new lambda.NodejsFunction(this, `func-${runtime.name}`, {
this.lambdaFunctions.push(new lambdaNodeJs.NodejsFunction(this, `func-${runtime.name}`, {
entry: path.join(__dirname, 'integ-handlers/dependencies.ts'),
runtime: runtime,
bundling: {
minify: true,
sourceMap: true,
sourceMapMode: lambda.SourceMapMode.BOTH,
sourceMapMode: lambdaNodeJs.SourceMapMode.BOTH,
},
}));
});
Expand All @@ -49,3 +51,45 @@ stack.lambdaFunctions.forEach(func=> {
ExecutedVersion: '$LATEST',
}));
});

// Ensure that the code is bundled
const assembly = app.synth();

stack.lambdaFunctions.forEach((func) => {
// Find the S3 bucket and key from the stack's template
const template = assembly.getStackArtifact(stack.artifactId).template;
const resourceName = stack.getLogicalId(func.node.defaultChild as lambda.CfnFunction);
const resource = template.Resources[resourceName];

if (!resource || resource.Type !== 'AWS::Lambda::Function') {
throw new ValidationError(`Could not find Lambda function resource for ${func.functionName}`, stack);
}

const s3Bucket = resource.Properties.Code.S3Bucket;
const s3Key = resource.Properties.Code.S3Key;

if (!s3Bucket || !s3Key) {
throw new ValidationError(`Could not find S3 location for function ${func.functionName}`, stack);
}

const assetId = s3Key.split('.')[1]; // The format is "asset.<hash>.zip"
const fullAssetPath = path.join(assembly.directory, `asset.${assetId}`);

if (!fs.existsSync(fullAssetPath)) {
throw new ValidationError(`Asset file does not exist for function ${func.functionName}`, stack);
}

const bundledContent = fs.readFileSync(fullAssetPath, 'utf-8');

if (!bundledContent.includes('exports.handler =')) {
throw new ValidationError(`Bundled content does not contain expected handler export for function ${func.functionName}`, stack);
}

if (bundledContent.includes('import ')) {
throw new ValidationError(`Bundled content contains unexpected import statement for function ${func.functionName}`, stack);
}

if (!bundledContent.includes('//# sourceMappingURL=')) {
throw new ValidationError(`Bundled content does not contain source map for function ${func.functionName}`, stack);
}
});

0 comments on commit 2becfd2

Please sign in to comment.