Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
feat: show sample on how to package custom layer
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jan 25, 2021
1 parent 2c3f63d commit b837cd8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ in
[this blog post](https://coderbyheart.com/how-i-package-typescript-lambdas-for-aws/).

Also, have a look at the [test stack](./cdk/cloudformation.ts), which uses this
library to publish a lambda.
library to publish a lambda and especially the
[`cdk/prepareResources.ts`](./cdk/prepareResources.ts) which shows how to
package a lambda and a layer.

## Use with Yarn

Expand Down
59 changes: 57 additions & 2 deletions cdk/prepareResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
WebpackMode,
LayeredLambdas,
} from '../src'
import { spawn } from 'child_process'

export const prepareResources = async ({
stackName,
Expand All @@ -20,7 +21,7 @@ export const prepareResources = async ({
baseLayerZipFileName: string
lambdas: LayeredLambdas<TestStackLambdas>
}> => {
// Pack the lambdas
// Prepare the output directory
const outDir = path.resolve(rootDir, 'dist', 'lambdas')
try {
await fs.stat(outDir)
Expand All @@ -30,11 +31,65 @@ export const prepareResources = async ({
const sourceCodeBucketName = await getLambdaSourceCodeBucketName({
stackName,
})

// Pack the baselayer, only with needed dependencies

// - This will contain the package.json to be used for the layer
const layerFolder = path.resolve(
rootDir,
'dist',
'lambdas',
'cloudFormationLayer',
)
try {
await fs.stat(layerFolder)
} catch (_) {
await fs.mkdir(layerFolder)
}

// - Pick relevant dependencies from the project's package.json
// so it they have the right version
const { dependencies } = JSON.parse(
await fs.readFile(path.resolve(rootDir, 'package.json'), 'utf-8'),
)
const cdkLambdaDeps = {
'@aws-sdk/client-s3': dependencies['@aws-sdk/client-s3'],
}
if (Object.values(cdkLambdaDeps).find((v) => v === undefined) !== undefined) {
throw new Error(
`Could not resolve all dependencies in "${JSON.stringify(
cdkLambdaDeps,
)}"`!,
)
}
// - add them to the layers package.json
await fs.writeFile(
path.join(layerFolder, 'package.json'),
JSON.stringify({
dependencies: cdkLambdaDeps,
}),
'utf-8',
)
// - install them
await new Promise<void>((resolve, reject) => {
const p = spawn('npm', ['i', '--ignore-scripts', '--only=prod'], {
cwd: layerFolder,
})
p.on('close', (code) => {
if (code !== 0) {
const msg = `[CloudFormation Layer] npm i in ${layerFolder} exited with code ${code}.`
return reject(new Error(msg))
}
return resolve()
})
})
const baseLayerZipFileName = await packBaseLayer({
srcDir: rootDir,
srcDir: layerFolder,
outDir,
Bucket: sourceCodeBucketName,
})

// Pack the lambda
const lambdas = await packLayeredLambdas<TestStackLambdas>({
id: 'test-lambdas',
mode: WebpackMode.production,
Expand Down

0 comments on commit b837cd8

Please sign in to comment.