From 4fffaccd8828de8b13997308d38a4f113d927c96 Mon Sep 17 00:00:00 2001 From: Alexander Melnyk Date: Mon, 25 Sep 2023 21:34:53 +0200 Subject: [PATCH 1/2] add parameters, batch, idempotency to layer --- layers/src/canary-stack.ts | 3 ++ layers/src/layer-publisher-stack.ts | 10 +++++- .../layerPublisher.class.test.functionCode.ts | 33 +++++++++++++++++++ layers/tests/e2e/layerPublisher.test.ts | 3 ++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/layers/src/canary-stack.ts b/layers/src/canary-stack.ts index 7c710cd976..ca3fec76b8 100644 --- a/layers/src/canary-stack.ts +++ b/layers/src/canary-stack.ts @@ -51,6 +51,9 @@ export class CanaryStack extends Stack { '@aws-lambda-powertools/metrics', '@aws-lambda-powertools/tracer', '@aws-lambda-powertools/commons', + '@aws-lambda-powertools/parameters', + '@aws-lambda-powertools/idempotency', + '@aws-lambda-powertools/batch', ], }, environment: { diff --git a/layers/src/layer-publisher-stack.ts b/layers/src/layer-publisher-stack.ts index ea75124e25..b5537467cb 100644 --- a/layers/src/layer-publisher-stack.ts +++ b/layers/src/layer-publisher-stack.ts @@ -60,7 +60,15 @@ export class LayerPublisherStack extends Stack { // This is the list of packages that we need include in the Lambda Layer // the name is the same as the npm workspace name - const utilities = ['commons', 'logger', 'metrics', 'tracer']; + const utilities = [ + 'commons', + 'logger', + 'metrics', + 'tracer', + 'parameters', + 'idempotency', + 'batch', + ]; // These files are relative to the tmp folder const filesToRemove = [ diff --git a/layers/tests/e2e/layerPublisher.class.test.functionCode.ts b/layers/tests/e2e/layerPublisher.class.test.functionCode.ts index fd89f22a44..f0c3636af4 100644 --- a/layers/tests/e2e/layerPublisher.class.test.functionCode.ts +++ b/layers/tests/e2e/layerPublisher.class.test.functionCode.ts @@ -3,6 +3,16 @@ import { readFile } from 'node:fs/promises'; import { Logger } from '@aws-lambda-powertools/logger'; import { Metrics } from '@aws-lambda-powertools/metrics'; import { Tracer } from '@aws-lambda-powertools/tracer'; +import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb'; +import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; +import { BatchProcessor, EventType } from '@aws-lambda-powertools/batch'; +import { SSMProvider } from '@aws-lambda-powertools/parameters/ssm'; +import { SecretsProvider } from '@aws-lambda-powertools/parameters/secrets'; +import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig'; +import { DynamoDBProvider } from '@aws-lambda-powertools/parameters/dynamodb'; +import { SSMClient } from '@aws-sdk/client-ssm'; +import { SecretsManagerClient } from '@aws-sdk/client-secrets-manager'; +import { AppConfigDataClient } from '@aws-sdk/client-appconfigdata'; const logger = new Logger({ logLevel: 'DEBUG', @@ -10,6 +20,29 @@ const logger = new Logger({ const metrics = new Metrics(); const tracer = new Tracer(); +const ddbClient = new DynamoDBClient({}); + +const ssmClient = new SSMClient({}); + +const secretsClient = new SecretsManagerClient({}); + +const appconfigClient = new AppConfigDataClient({}); + +new DynamoDBPersistenceLayer({ + tableName: 'my-idempotency-table', + awsSdkV3Client: ddbClient, +}); + +new BatchProcessor(EventType.SQS); + +new SSMProvider({ awsSdkV3Client: ssmClient }); + +new SecretsProvider({ awsSdkV3Client: secretsClient }); + +new AppConfigProvider({ environment: 'foo', awsSdkV3Client: appconfigClient }); + +new DynamoDBProvider({ tableName: 'foo', awsSdkV3Client: ddbClient }); + const layerPath = process.env.LAYERS_PATH || '/opt/nodejs/node_modules'; const expectedVersion = process.env.POWERTOOLS_PACKAGE_VERSION || '0.0.0'; diff --git a/layers/tests/e2e/layerPublisher.test.ts b/layers/tests/e2e/layerPublisher.test.ts index 52eff56c61..09aebc671d 100644 --- a/layers/tests/e2e/layerPublisher.test.ts +++ b/layers/tests/e2e/layerPublisher.test.ts @@ -97,6 +97,9 @@ describe(`Layers E2E tests, publisher stack`, () => { '@aws-lambda-powertools/logger', '@aws-lambda-powertools/metrics', '@aws-lambda-powertools/tracer', + '@aws-lambda-powertools/parameter', + '@aws-lambda-powertools/idempotency', + '@aws-lambda-powertools/batch', ], }, layers: [layerVersion], From ba063a04344200b12213d4701e29aca75f4d9abd Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 26 Sep 2023 13:24:53 +0200 Subject: [PATCH 2/2] chore: shave layer size --- layers/src/layer-publisher-stack.ts | 42 ++++++++++++++++--- .../layerPublisher.class.test.functionCode.ts | 9 +++- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/layers/src/layer-publisher-stack.ts b/layers/src/layer-publisher-stack.ts index b5537467cb..6ef6c19fda 100644 --- a/layers/src/layer-publisher-stack.ts +++ b/layers/src/layer-publisher-stack.ts @@ -75,17 +75,47 @@ export class LayerPublisherStack extends Stack { 'node_modules/@types', 'package.json', 'package-lock.json', - 'node_modules/**/README.md', - 'node_modules/.bin/semver', + 'node_modules/**/*.md', + 'node_modules/.bin', + 'node_modules/**/*.html', + 'node_modules/**/.travis.yml', + 'node_modules/**/.eslintrc', + 'node_modules/**/.npmignore', + 'node_modules/semver/bin', + 'node_modules/emitter-listener/test', + 'node_modules/fast-xml-parser/cli', 'node_modules/async-hook-jl/test', + 'node_modules/stack-chain/test', 'node_modules/shimmer/test', 'node_modules/jmespath/artifacts', - // We remove the type definitions since they can't be used in the Lambda Layer - 'node_modules/@aws-lambda-powertools/*/lib/*.d.ts', - 'node_modules/@aws-lambda-powertools/*/lib/*.d.ts.map', + 'node_modules/jmespath/bower.json', + 'node_modules/obliterator/*.d.ts', + 'node_modules/strnum/.vscode', + 'node_modules/strnum/*.test.js', + 'node_modules/uuid/bin', + 'node_modules/uuid/esm-browser', + 'node_modules/uuid/esm-node', + 'node_modules/uuid/umd', + 'node_modules/mnemonist/*.d.ts', + // We remove the type definitions and ES builds since they are not used in the Lambda Layer + 'node_modules/@aws-lambda-powertools/*/lib/**/*.d.ts', + 'node_modules/@aws-lambda-powertools/*/lib/**/*.d.ts.map', + 'node_modules/@aws-sdk/*/dist-types', + 'node_modules/@aws-sdk/*/dist-es', + 'node_modules/@smithy/*/dist-types', + 'node_modules/@smithy/*/dist-es', + 'node_modules/@smithy/**/README.md ', + 'node_modules/@aws-sdk/**/README.md ', ]; const buildCommands: string[] = []; - const modulesToInstall: string[] = []; + // We need these modules because they are not included in the nodejs14x and nodejs16x runtimes + const modulesToInstall: string[] = [ + '@aws-sdk/client-dynamodb', + '@aws-sdk/util-dynamodb', + '@aws-sdk/client-ssm', + '@aws-sdk/client-secrets-manager', + '@aws-sdk/client-appconfigdata', + ]; if (buildFromLocal) { for (const util of utilities) { diff --git a/layers/tests/e2e/layerPublisher.class.test.functionCode.ts b/layers/tests/e2e/layerPublisher.class.test.functionCode.ts index f0c3636af4..d63f389d7c 100644 --- a/layers/tests/e2e/layerPublisher.class.test.functionCode.ts +++ b/layers/tests/e2e/layerPublisher.class.test.functionCode.ts @@ -20,6 +20,10 @@ const logger = new Logger({ const metrics = new Metrics(); const tracer = new Tracer(); +// Instantiating these clients and the respective providers/persistence layers +// will ensure that Idempotency & Parameters are working with +// the AWS SDK v3 client, both coming from the Lambda Layer and the +// bundle const ddbClient = new DynamoDBClient({}); const ssmClient = new SSMClient({}); @@ -33,8 +37,6 @@ new DynamoDBPersistenceLayer({ awsSdkV3Client: ddbClient, }); -new BatchProcessor(EventType.SQS); - new SSMProvider({ awsSdkV3Client: ssmClient }); new SecretsProvider({ awsSdkV3Client: secretsClient }); @@ -43,6 +45,9 @@ new AppConfigProvider({ environment: 'foo', awsSdkV3Client: appconfigClient }); new DynamoDBProvider({ tableName: 'foo', awsSdkV3Client: ddbClient }); +// Instantiating the BatchProcessor will confirm that the utility can be used +new BatchProcessor(EventType.SQS); + const layerPath = process.env.LAYERS_PATH || '/opt/nodejs/node_modules'; const expectedVersion = process.env.POWERTOOLS_PACKAGE_VERSION || '0.0.0';