Skip to content
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

feat: adds logLevel option, so users can debug lambda functions #286

Merged
merged 1 commit into from
Mar 22, 2024
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
12 changes: 11 additions & 1 deletion lambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ import {
Callback,
CustomResource,
Event,
LogLevel,
Logger,
StandardLogger,
} from 'aws-cloudformation-custom-resource';
import * as forge from 'node-forge';
import { PublicKeyFormat, ResourceProperties } from './types';
Expand All @@ -53,14 +55,22 @@ export const handler = function (
context: Context,
callback: Callback,
) {
new CustomResource<ResourceProperties>(
const resource = new CustomResource<ResourceProperties>(
event,
context,
callback,
createResource,
updateResource,
deleteResource,
);
if (event.ResourceProperties.LogLevel) {
resource.setLogger(
new StandardLogger(
// because jsii is forcing us to expose enums with all capitals and the enum in aws-cloudformation-custom-resource is all lowercase, we need to cast here. Other than the capitalization, the enums are identical
event.ResourceProperties.LogLevel as unknown as LogLevel,
),
);
}
};

async function createResource(
Expand Down
10 changes: 10 additions & 0 deletions lambda/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// this file is physically present in /lambda, as it is required for build the lambda zip
// the file is symlinked into /lib, as otherwise jsii is refusing to find it, even when the whole lambda directory is not ignored

export enum LogLevel {
/* eslint-disable @typescript-eslint/naming-convention */
ERROR,
WARN,
INFO,
DEBUG,
/* eslint-enable @typescript-eslint/naming-convention */
}

export enum PublicKeyFormat {
/* eslint-disable @typescript-eslint/naming-convention */
OPENSSH = 'OPENSSH',
Expand All @@ -22,5 +31,6 @@ export interface ResourceProperties {
RemoveKeySecretsAfterDays: number;
StackName: string;
Tags: Record<string, string>;
LogLevel?: LogLevel;
/* eslint-enable @typescript-eslint/naming-convention */
}
12 changes: 10 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
import { IKeyPair, OperatingSystemType } from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
import * as path from 'path';
import { PublicKeyFormat, ResourceProperties } from './types';
export { PublicKeyFormat } from './types';
import { LogLevel, PublicKeyFormat, ResourceProperties } from './types';
export { LogLevel, PublicKeyFormat } from './types';

const resourceType = 'Custom::EC2-Key-Pair';
const ID = `CFN::Resource::${resourceType}`;
Expand Down Expand Up @@ -140,6 +140,13 @@ export interface KeyPairProps extends ResourceProps {
* @default false
*/
readonly legacyLambdaName?: boolean;

/**
* The log level of the Lambda function
*
* @default LogLevel.warn
*/
readonly logLevel?: LogLevel;
}

/**
Expand Down Expand Up @@ -249,6 +256,7 @@ export class KeyPair extends Resource implements ITaggable, IKeyPair {
Tags: Lazy.any({
produce: () => this.tags.renderTags() as Record<string, string>,
}) as unknown as Record<string, string>,
LogLevel: props.logLevel,
/* eslint-enable @typescript-eslint/naming-convention */
};

Expand Down
6 changes: 5 additions & 1 deletion test/lib/test-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
} from 'aws-cdk-lib';
import cloudfront = require('aws-cdk-lib/aws-cloudfront');
import { Construct } from 'constructs';
import { PublicKeyFormat } from '../../lambda/types';
import { LogLevel, PublicKeyFormat } from '../../lambda/types';
import { KeyPair } from '../../lib';

interface Props extends StackProps {
currentUserName: string;
}

const logLevel = LogLevel.DEBUG;

export class TestStack extends Stack {
constructor(scope: Construct, id: string, props: Props) {
super(scope, id, props);
Expand Down Expand Up @@ -47,6 +49,7 @@ export class TestStack extends Stack {
storePublicKey: false,
exposePublicKey: true,
publicKey: keyPair.publicKeyValue,
logLevel,
});

if (process.env.with_ec2 === 'true') {
Expand Down Expand Up @@ -76,6 +79,7 @@ export class TestStack extends Stack {
storePublicKey: true,
publicKeyFormat: PublicKeyFormat.PEM,
legacyLambdaName: true,
logLevel,
});

const currentUser = aws_iam.User.fromUserName(
Expand Down
Loading