Skip to content

Commit

Permalink
chore(layer): fix bundling and e2e tests by manually setting hash of …
Browse files Browse the repository at this point in the history
…asset (#1116)

Co-authored-by: Florian Chazal <chazalf@amazon.com>
  • Loading branch information
flochaz and Florian Chazal authored Oct 13, 2022
1 parent d4174eb commit 3b5c799
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
16 changes: 15 additions & 1 deletion layer-publisher/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion layer-publisher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"aws-cdk-lib": "2.25.0",
"constructs": "^10.0.0",
"if-node-version": "^1.1.1",
"source-map-support": "^0.5.16"
"source-map-support": "^0.5.16",
"ts-md5": "^1.3.1"
}
}
35 changes: 20 additions & 15 deletions layer-publisher/src/powertools-typescript-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path';
import { aws_lambda as lambda } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { execSync } from 'child_process';
import { Md5 } from 'ts-md5';

export interface PowerToolsTypeScriptLayerProps {
/**
Expand All @@ -17,10 +18,28 @@ export interface PowerToolsTypeScriptLayerProps {

export class PowerToolsTypeScriptLayer extends lambda.LayerVersion {
public constructor(scope: Construct, id: string, props?: PowerToolsTypeScriptLayerProps) {
const version = props?.version ?? 'latest';
console.log(`publishing layer ${props?.layerVersionName} version : ${version}`);

const commands = [
'mkdir nodejs',
'cd nodejs',
'npm init -y',
`npm install --save \
@aws-lambda-powertools/commons@${version} \
@aws-lambda-powertools/logger@${version} \
@aws-lambda-powertools/metrics@${version} \
@aws-lambda-powertools/tracer@${version}`,
'rm package.json package-lock.json',
];
const commandJoined = commands.join(' && ');

super(scope, id, {
layerVersionName: props?.layerVersionName,
description: `Lambda Powertools for TypeScript version ${props?.version}`,
compatibleRuntimes: [ lambda.Runtime.NODEJS_12_X, lambda.Runtime.NODEJS_14_X, lambda.Runtime.NODEJS_16_X ],
code: lambda.Code.fromAsset(path.join(__dirname, '.'), {
assetHash: Md5.hashStr(commandJoined),
bundling: {
image: lambda.Runtime.NODEJS_12_X.bundlingImage,
local: {
Expand All @@ -31,21 +50,7 @@ export class PowerToolsTypeScriptLayer extends lambda.LayerVersion {
return false;
}

const commands = [
'mkdir nodejs && cd nodejs',
'npm init -y',
`npm install @aws-lambda-powertools/commons@${
props?.version ?? 'latest'
} @aws-lambda-powertools/logger@${props?.version ?? 'latest'} @aws-lambda-powertools/metrics@${
props?.version ?? 'latest'
} @aws-lambda-powertools/tracer@${props?.version ?? 'latest'}`,
'rm package.json package-lock.json',
'cd ..',
`cp -a nodejs ${outputDir}`,
`rm -rf nodejs`,
];

execSync(commands.join(' && '));
execSync(commandJoined, { cwd: outputDir });

return true;
},
Expand Down
2 changes: 1 addition & 1 deletion layer-publisher/tests/e2e/happy-case.test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports.handler = function (_event: never, _ctx: unknown): void {

if (packageJSON.version != process.env.POWERTOOLS_PACKAGE_VERSION) {
throw new Error(
`Package version mismatch: \${packageJSON.version} != \${process.env.POWERTOOLS_PACKAGE_VERSION}`
`Package version mismatch: ${packageJSON.version} != ${process.env.POWERTOOLS_PACKAGE_VERSION}`
);
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ Object {
"S3Bucket": Object {
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}",
},
"S3Key": "dfbb723532549b2040a663602bbe51ab6b3f16dfd063eafc5bbd7e64b13f997d.zip",
"S3Key": "c2f621503b147cecccf2e6cc6df420a8f11ee29ae042d2c1f523cf5db3f47ca2.zip",
},
"Description": "Lambda Powertools for TypeScript version 1.0.1",
"LayerName": "AWSLambdaPowertoolsTypeScript",
},
"Type": "AWS::Lambda::LayerVersion",
Expand Down

0 comments on commit 3b5c799

Please sign in to comment.