UNPKG

aws-cloudformation-custom-resource

Version:

Helper for managing custom AWS CloudFormation resources in a Lambda function

221 lines (220 loc) 6.63 kB
export type Callback<TResult = unknown> = (error?: Error | string | null, result?: TResult) => void; export interface Context { callbackWaitsForEmptyEventLoop: boolean; functionName: string; functionVersion: string; invokedFunctionArn: string; memoryLimitInMB: string; awsRequestId: string; logGroupName: string; logStreamName: string; getRemainingTimeInMillis(): number; } /** * Default resource properties, if user does not provide any via generic */ type DefaultResourceProperties = Record<string, string>; interface ResourceProperty<T> { /** * The value of the property */ value: T; /** * The value of the property before the update * * Only available during a resource update */ before?: T; /** * Indicates whether the value of the property has changed */ changed: boolean; /** * Returns the value of the property * @returns The value of the property */ toString(): string; /** * Returns the value of the property * @returns The value of the property */ valueOf(): string; } type CustomResourceProperties<ResourceProperties extends object> = { [P in keyof ResourceProperties]: ResourceProperty<ResourceProperties[P]>; }; /** * The event passed to the Lambda handler */ export type Event<ResourceProperties = DefaultResourceProperties> = Omit<Record<string, unknown>, 'ResourceProperties'> & { PhysicalResourceId?: string; StackId: string; RequestId: string; LogicalResourceId: string; ResponseURL?: string; RequestType: 'Create' | 'Update' | 'Delete'; ResourceProperties: ResourceProperties; OldResourceProperties?: ResourceProperties; }; /** * A response value returned to CloudFormation. */ type ResponseValue = string; /** * Function signature */ export type HandlerFunction<ResourceProperties extends object> = (resource: CustomResource<ResourceProperties>, logger: Logger) => Promise<void>; /** * Custom CloudFormation resource helper */ export declare class CustomResource<ResourceProperties extends object = DefaultResourceProperties> { /** * Stores function executed when resource creation is requested */ private createFunction; /** * Stores function executed when resource update is requested */ private updateFunction; /** * Stores function executed when resource deletion is requested */ private deleteFunction; /** * The event passed to the Lambda handler */ readonly event: Event<ResourceProperties>; /** * The context passed to the Lambda handler */ readonly context: Context; /** * The callback function passed to the Lambda handler */ readonly callback: Callback; /** * The properties passed to the Lambda function */ readonly properties: CustomResourceProperties<ResourceProperties>; /** * Stores values returned to CloudFormation */ private responseData; /** * Stores values physical ID of the resource */ private physicalResourceId?; /** * Indicates whether to mask the output of the custom resource when it's retrieved by using the `Fn::GetAtt` function. * * If set to `true`, all returned values are masked with asterisks (*****), except for information stored in the locations specified below. By default, this value is `false`. */ private noEcho; /** * Logger class */ private logger; /** * Timer for the Lambda timeout * * One second before the Lambda times out, we send a FAILED response to CloudFormation. * We store the timer, so we can clear it when we send the response. */ private timeoutTimer?; constructor(event: Event<ResourceProperties>, context: Context, callback: Callback, createFunction: HandlerFunction<ResourceProperties>, updateFunction: HandlerFunction<ResourceProperties>, deleteFunction: HandlerFunction<ResourceProperties>); /** * Proxy handler for ResourceProperties */ private propertiesProxyHandler; /** * Adds values to the response returned to CloudFormation */ addResponseValue(key: string, value: ResponseValue): void; /** * Set the physical ID of the resource */ setPhysicalResourceId(value: string): void; /** * Get the physical ID of the resource */ getPhysicalResourceId(): string | undefined; /** * Set whether to mask the output of the custom resource when it's retrieved by using the `Fn::GetAtt` function. * * If set to `true`, all returned values are masked with asterisks (*****), except for information stored in the locations specified below. By default, this value is `false`. */ setNoEcho(value: boolean): void; /** * Get whether to mask the output of the custom resource when it's retrieved by using the `Fn::GetAtt` function. */ getNoEcho(): boolean; /** * Set the logger class */ setLogger(logger: Logger): void; /** * Handles the Lambda event */ private handle; private handleError; /** * Sends CloudFormation response just before the Lambda times out */ private timeout; /** * Sends CloudFormation response */ private sendResponse; } /** * Logger class */ export interface Logger { log(message: unknown, ...optionalParams: unknown[]): void; info(message: unknown, ...optionalParams: unknown[]): void; debug(message: unknown, ...optionalParams: unknown[]): void; warn(message: unknown, ...optionalParams: unknown[]): void; error(message: unknown, ...optionalParams: unknown[]): void; } /** * LogLevels supported by the logger */ export declare enum LogLevel { error = 0, warn = 1, info = 2, debug = 3 } /** * Standard logger class */ export declare class StandardLogger { /** * The log level * * @default LogLevel.warn */ level: LogLevel; constructor(level?: LogLevel); /** * Logs message with level ERROR */ error(message: unknown, ...optionalParams: unknown[]): void; /** * Logs message with level WARN */ warn(message: unknown, ...optionalParams: unknown[]): void; /** * Logs message with level INFO */ info(message: unknown, ...optionalParams: unknown[]): void; /** * Logs message with level DEBUG */ debug(message: unknown, ...optionalParams: unknown[]): void; /** * Alias for info */ log(message: unknown, ...optionalParams: unknown[]): void; } export {};