diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.nodejs.build.images.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.nodejs.build.images.ts index 87e6168c831fc..af8c2eec75827 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.nodejs.build.images.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.nodejs.build.images.ts @@ -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'; @@ -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, }, })); }); @@ -49,3 +51,39 @@ stack.lambdaFunctions.forEach(func=> { ExecutedVersion: '$LATEST', })); }); + +// Ensure that the code is bundled +const assembly = app.synth(); + +stack.lambdaFunctions.forEach((func) => { + 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('.')[0]; // S3Key format is .zip" + const assetDir = path.join(assembly.directory, `asset.${assetId}`); + + try { + if (!fs.existsSync(assetDir) || !fs.statSync(assetDir).isDirectory()) { + throw new ValidationError(`Asset directory does not exist for function ${func.functionName}: ${assetDir}`, stack); + } + + const indexPath = path.join(assetDir, 'index.js'); + if (!fs.existsSync(indexPath)) { + throw new ValidationError(`index.js not found in asset directory for function ${func.functionName}`, stack); + } + } catch (error) { + throw error; + } +}); diff --git a/packages/aws-cdk/lib/cli/user-configuration.ts b/packages/aws-cdk/lib/cli/user-configuration.ts index caf865bc4107a..b4e40c94a6194 100644 --- a/packages/aws-cdk/lib/cli/user-configuration.ts +++ b/packages/aws-cdk/lib/cli/user-configuration.ts @@ -41,6 +41,7 @@ const BUNDLING_COMMANDS = [ Command.SYNTH, Command.SYNTHESIZE, Command.WATCH, + Command.IMPORT, ]; export type Arguments = {