Skip to content

chore(layers): add Parser to layer #3101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions layers/src/canary-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class CanaryStack extends Stack {
'@aws-lambda-powertools/parameters',
'@aws-lambda-powertools/idempotency',
'@aws-lambda-powertools/batch',
'@aws-lambda-powertools/parser',
],
},
environment: {
Expand Down
2 changes: 2 additions & 0 deletions layers/src/layer-publisher-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class LayerPublisherStack extends Stack {
'parameters',
'idempotency',
'batch',
'parser',
];

// These files are relative to the tmp folder
Expand Down Expand Up @@ -108,6 +109,7 @@ export class LayerPublisherStack extends Stack {
'@aws-sdk/client-ssm',
'@aws-sdk/client-secrets-manager',
'@aws-sdk/client-appconfigdata',
'zod',
];

if (buildFromLocal) {
Expand Down
12 changes: 11 additions & 1 deletion layers/tests/e2e/layerPublisher.class.test.functionCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig';
import { DynamoDBProvider } from '@aws-lambda-powertools/parameters/dynamodb';
import { SecretsProvider } from '@aws-lambda-powertools/parameters/secrets';
import { SSMProvider } from '@aws-lambda-powertools/parameters/ssm';
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas';
import { Tracer } from '@aws-lambda-powertools/tracer';
import { AppConfigDataClient } from '@aws-sdk/client-appconfigdata';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
import { SSMClient } from '@aws-sdk/client-ssm';
import { z } from 'zod';

const logger = new Logger({
logLevel: 'DEBUG',
Expand Down Expand Up @@ -48,6 +50,10 @@ new DynamoDBProvider({ tableName: 'foo', awsSdkV3Client: ddbClient });
// Instantiating the BatchProcessor will confirm that the utility can be used
new BatchProcessor(EventType.SQS);

const testSchema = z.object({ instance_id: z.string(), state: z.string() });

const testEventSchema = EventBridgeSchema.extend({ detail: testSchema });

const layerPath = process.env.LAYERS_PATH || '/opt/nodejs/node_modules';
const expectedVersion = process.env.POWERTOOLS_PACKAGE_VERSION || '0.0.0';

Expand Down Expand Up @@ -79,7 +85,7 @@ const getVersionFromModule = async (moduleName: string): Promise<string> => {
return moduleVersion;
};

export const handler = async (): Promise<void> => {
export const handler = async (event: unknown): Promise<void> => {
// Check that the packages version matches the expected one
for (const moduleName of [
'commons',
Expand All @@ -89,6 +95,7 @@ export const handler = async (): Promise<void> => {
'parameters',
'idempotency',
'batch',
'parser',
]) {
const moduleVersion = await getVersionFromModule(moduleName);
if (moduleVersion !== expectedVersion) {
Expand All @@ -115,4 +122,7 @@ export const handler = async (): Promise<void> => {
// the presence of a log will indicate that the logger is working
// while the content of the log will indicate that the tracer is working
logger.debug('subsegment', { subsegment: subsegment.format() });

// Check that the parser is working
testEventSchema.parse(event);
};
19 changes: 19 additions & 0 deletions layers/tests/e2e/layerPublisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { TestNodejsFunction } from '@aws-lambda-powertools/testing-utils/resources/lambda';
import { App } from 'aws-cdk-lib';
import { LayerVersion } from 'aws-cdk-lib/aws-lambda';
import type { EventBridgeEvent } from 'aws-lambda';
import packageJson from '../../package.json';
import { LayerPublisherStack } from '../../src/layer-publisher-stack';
import {
Expand Down Expand Up @@ -132,6 +133,24 @@ describe('Layers E2E tests', () => {
functionName: testStack.findAndGetStackOutputValue(
`test${outputFormat}Fn`
),
// Uses an EventBridge event payload to test parser functionality
payload: {
version: '0',
id: '6a7e8feb-b491-4cf7-a9f1-bf3703467718',
'detail-type': 'EC2 Instance State-change Notification',
source: 'aws.ec2',
account: '111122223333',
time: '2017-12-22T18:43:48Z',
region: 'us-west-1',
resources: [
'arn:aws:ec2:us-west-1:123456789012:instance/i-1234567890abcdef0',
],
detail: {
instance_id: 'i-1234567890abcdef0',
state: 'terminated',
},
'replay-name': 'replay_archive',
} satisfies EventBridgeEvent<string, unknown>,
})
);
}
Expand Down