I am using aws-cdk-lib as a dev dependency in my serverless aws project using Lambda Functions and aws cdk for resources provisioning in typescript and this package is listed under the dev Dependecncies in the package.json in the root of my project
"aws-cdk-lib": "2.138.0",
I am using RequestBodyValidator in my cdk code to inject body valdiation schema againt API Requests , I have created a file bodyValidationSchemas.ts which exports schemas needed to validate against for each resource and method combination i.e /user+ POST
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
export const updateUserById: apigateway.JsonSchema = {
type: apigateway.JsonSchemaType.OBJECT,
properties: {
userId: { type: apigateway.JsonSchemaType.NUMBER },
email: { type: apigateway.JsonSchemaType.STRING },
firstName: { type: apigateway.JsonSchemaType.STRING },
lastName: { type: apigateway.JsonSchemaType.STRING },
country: {
type: apigateway.JsonSchemaType.OBJECT,
properties: {
countryCode: { type: apigateway.JsonSchemaType.NUMBER },
countryName: { type: apigateway.JsonSchemaType.STRING }
},
required: ['countryCode', 'countryName']
},
phoneNumber: { type: apigateway.JsonSchemaType.NUMBER }
},
required: ['userId', 'email', 'firstName']
};
My question is , aws-cdk-lib is listed under the dev dependencies in the package.json file in the root of my project , and the file bodyValidationSchemas.ts is not a lambda function definition file it is just a .ts file the exports consts of type apigateway.JsonSchema , so no lambda layer dependencies are being attached to (package.json file in my lambda layer doesn't have an entry for aws-cdk-lib).
1- Why when I deploy my stack it doesn't through an error ? reference error for the aws-cdk-lib as it is listed under the dev dependencies in the package.json in the root of my project.
2- How does the types get resolved ?
export const updateUserById: apigateway.JsonSchema
how does the type apigateway.JsonSchema get resolved
3- in my tsconfig.build.json am excluding custom types am using during development
"exclude": ["lambda/**/test", "lambda/types"]
even though after building the dist folder container the lambda/types , why is this happening ?
the build script is as follows
#!/bin/bash
green='\033[0;32m'
reset='\033[0m'
dist_dependencies_layer_layer_dir_path_from_layer_dir="../../../../dist/lambda/layers/dependencies-layer/nodejs/"
echo -e "Removing dist dir...\n"
rm -rf dist
echo -e "${green}'dist' dir Successfully Removed${reset}\n"
echo -e "Building Lambda... \n"
tsc --project tsconfig.build.json
echo -e "${green}Lambda Successfully Built${reset}\n"
echo -e "Installing Layer Dependencies...\n"
npm ci --only=production --prefix ./lambda/layers/dependencies-layer/nodejs
echo -e "${green}Dependencies Successfully Installed${reset}\n"
echo -e "Navigating into Lambda layer...\n"
cd ./lambda/layers/dependencies-layer/nodejs
echo -e "${green}Currently in Lambda layer...${reset}\n"
echo -e "Copying node_modules to distributable directory...\n"
cp -r node_modules "$dist_dependencies_layer_layer_dir_path_from_layer_dir"
echo -e "${green}Node Modules Successfully Copied${reset}\n"
**tsconfig :
**
{
"compilerOptions": {
"module": "CommonJS",
"declaration": false,
"lib": ["ES2022"],
"target": "ES2022",
"emitDecoratorMetadata": true,
"rootDir": "./",
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"/opt/nodejs/*": ["lambda/layers/dependencies-layer/nodejs/*"]
},
"outDir": "dist",
"importsNotUsedAsValues": "remove",
"preserveConstEnums": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"useUnknownInCatchVariables": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"skipLibCheck": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"experimentalDecorators": true,
"strictPropertyInitialization": false
}
}
tsconfig.build.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist/lambda",
"rootDir": "lambda"
},
"include": ["lambda"],
"exclude": ["lambda/**/test", "lambda/types"]
}
4-
project\.
|---------lambda
|----------ResourceMethodPairSchemas
|----------------------------user
|------bodyValidationSchemas.ts
|-----------functions
|-----------layers
does the bodyValidationSchemas.ts file is included in the lambda deployed pakcage? given that is just a typescript file exporting some consts ?
and if so would the 'aws-cdk-lib/aws-apigateway' be included in the lambda deployed package ?
Note :
For now everything works correctly, Request Body Validation is working as it should , Am asking Just In case am missing something and some unexpected behavior that may happen in the future and because I don't understand how does dependencies and typescript types get resolved in the lambda runtime In this current setup.