Skip to content
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
52 changes: 49 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,45 @@ class LogForwardingPlugin {
this.hooks = {
'package:initialize': this.updateResources.bind(this),
};

// schema for the function section of serverless.yml
this.serverless.configSchemaHandler.defineFunctionProperties('aws', {
properties: {
logForwarding: {
type: 'object',
properties: {
enabled: {
type: 'boolean',
},
},
required: ['enabled'],
},
},
required: [],
});

// schema for the custom props section of serverless.yml
this.serverless.configSchemaHandler.defineCustomProperties({
properties: {
logForwarding: {
type: 'object',
properties: {
destinationARN: { type: 'string' },
roleArn: { type: 'string' },
filterPattern: { type: 'string' },
normalizedFilterID: { type: 'string' },
stages: {
type: 'array',
uniqueItems: true,
items: { type: 'string' },
},
createLambdaPermission: { type: 'boolean' },
},
required: ['destinationARN'],
},
},
required: ['logForwarding'],
});
}

loadConfig(): void {
Expand All @@ -45,7 +84,9 @@ class LogForwardingPlugin {
}
this.config = { ...CONFIG_DEFAULTS, ...service.custom.logForwarding };
if (this.config.destinationARN === undefined) {
throw new Error('Serverless-log-forwarding is not configured correctly. Please see README for proper setup.');
throw new Error(
'Serverless-log-forwarding is not configured correctly. Please see README for proper setup.',
);
}
}

Expand Down Expand Up @@ -118,11 +159,16 @@ class LogForwardingPlugin {
return this.serverless.service.provider.stage;
}

makeSubsctiptionFilter(functionName: string, deps?: string[]): SubscriptionFilterCF {
makeSubsctiptionFilter(
functionName: string,
deps?: string[],
): SubscriptionFilterCF {
const functionObject = this.serverless.service.getFunction(functionName);
const logGroupName = this.provider.naming.getLogGroupName(functionObject.name);
const logGroupId = this.provider.naming.getLogGroupLogicalId(functionName);
const roleObject = this.config.roleArn ? { RoleArn: this.config.roleArn } : {};
const roleObject = this.config.roleArn
? { RoleArn: this.config.roleArn }
: {};
return {
Type: 'AWS::Logs::SubscriptionFilter',
Properties: {
Expand Down
103 changes: 80 additions & 23 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface SlsFunction {
name: string;
logForwarding?: {
enabled?: boolean;
}
};
}

export interface PluginConfig {
Expand All @@ -14,34 +14,91 @@ export interface PluginConfig {
destinationARN: string;
}

/**
* If your plugin adds new properties to any level in the serverless.yml
* you can use these functions to add JSON ajv based schema validation for
* those properties
*
* @see https://www.serverless.com/framework/docs/guides/plugins/custom-configuration
*/
export interface ConfigSchemaHandler {
/**
* If your plugin requires additional top-level properties (like provider, custom, service...)
* you can use the defineTopLevelProperty helper to add their definition.
* @see https://www.serverless.com/framework/docs/guides/plugins/custom-configuration#top-level-properties-via-definetoplevelproperty
*/
defineTopLevelProperty(
providerName: string,
schema: Record<string, unknown>
): void;
/**
* If your plugin depends on properties defined in the custom: section, you can use the
* defineCustomProperties helper
* @see https://www.serverless.com/framework/docs/guides/plugins/custom-configuration#properties-in-custom-via-definecustomproperties
*/
defineCustomProperties(jsonSchema: object): void;
/**
* If your plugin adds support to a new function event, you can use the
* defineFunctionEvent helper
* @see https://www.serverless.com/framework/docs/guides/plugins/custom-configuration#function-events-via-definefunctionevent
*/
defineFunctionEvent(
providerName: string,
event: string,
jsonSchema: Record<string, object>
): void;
/**
* If your plugin adds new properties to a function event, you can use the
* defineFunctionEventProperties helper
* @see https://www.serverless.com/framework/docs/guides/plugins/custom-configuration#function-event-properties-via-definefunctioneventproperties
*/
defineFunctionEventProperties(
providerName: string,
existingEvent: string,
jsonSchema: object
): void;
/**
* If your plugin adds new properties to functions, you can use the
* defineFunctionProperties helper.
* @see https://www.serverless.com/framework/docs/guides/plugins/custom-configuration#function-properties-via-definefunctionproperties
*/
defineFunctionProperties(providerName: string, schema: object): void;
/**
* If your plugin provides support for a new provider, register it via defineProvider
* @see https://www.serverless.com/framework/docs/guides/plugins/custom-configuration#new-provider-via-defineprovider
*/
defineProvider(providerName: string, options?: Record<string, unknown>): void;
}

export interface ServerlessInstance {
service: {
service: string
service: string;
resources: {
Resources: ResourcesCF
},
Resources: ResourcesCF;
};
provider: {
stage: string,
region: string
},
stage: string;
region: string;
};
functions: {
name: unknown[];
},
getFunction (name: string): SlsFunction,
};
getFunction(name: string): SlsFunction;
custom: {
logForwarding: PluginConfig
},
},
logForwarding: PluginConfig;
};
};
providers: {
aws: {
getRegion (): string,
},
},
getProvider (name: string): AWSProvider,
getRegion(): string;
};
};
getProvider(name: string): AWSProvider;
configSchemaHandler: ConfigSchemaHandler;
cli: {
log (str: string, entity?: string): void,
consoleLog (str: string): void,
}
log(str: string, entity?: string): void;
consoleLog(str: string): void;
};
}

export interface ServerlessConfig {
Expand All @@ -52,10 +109,10 @@ export interface ServerlessConfig {

export interface AWSProvider {
naming: {
getLogGroupName(name: string): string,
getNormalizedFunctionName(name: string): string,
getLogGroupLogicalId(name: string): string
}
getLogGroupName(name: string): string;
getNormalizedFunctionName(name: string): string;
getLogGroupLogicalId(name: string): string;
};
}

export interface ObjectCF<TProps> {
Expand Down
6 changes: 4 additions & 2 deletions test/unit-tests/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ const constructPluginResources = (logForwarding, functions?) => {
const config = {
commands: [],
options: {},
stage: 'test-stage',
};
const serverless = createServerless(config, {
provider: {
region: 'us-moon-1',
stage: 'test-stage',
stage: config.stage,
},
custom: {
logForwarding,
Expand All @@ -89,11 +90,12 @@ const constructPluginNoResources = (logForwarding) => {
const config = {
commands: [],
options: {},
stage: 'test-stage',
};
const serverless = createServerless(config, {
provider: {
region: 'us-moon-1',
stage: 'test-stage',
stage: config.stage,
},
custom: {
logForwarding,
Expand Down