Skip to content

Commit 5cfdfdd

Browse files
committed
Add tags support
1 parent ab0fed2 commit 5cfdfdd

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

API.md

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as path from 'node:path';
22
import { CustomResource, CustomResourceProvider, CustomResourceProviderRuntime } from 'aws-cdk-lib';
33
import { Construct } from 'constructs';
4+
import { CustomResourceProps } from './self-signed-certificate-lambda';
45

56
export interface SelfSignedCertificateProps {
67
readonly certificateDetails: {
78
commonName: string;
89
[key: string]: string;
910
};
11+
readonly tags?: Record<string, string>;
1012
}
1113

1214

@@ -54,10 +56,16 @@ export class SelfSignedCertificate extends Construct {
5456
}],
5557
});
5658

59+
const tags = props.tags ?? {};
60+
const resourceProps: CustomResourceProps = {
61+
certificateDetails: props.certificateDetails,
62+
tags: Object.keys(tags).map((key) => ({ key: key, value: tags[key] })),
63+
};
64+
5765
new CustomResource(this, 'resource', {
5866
serviceToken: this.provider.serviceToken,
5967
resourceType: CustomResourceType,
60-
properties: props,
68+
properties: resourceProps,
6169
});
6270
}
6371

src/self-signed-certificate-lambda/index.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
import type { CdkCustomResourceHandler } from 'aws-lambda';
1+
import { ACMClient, ImportCertificateCommand } from '@aws-sdk/client-acm';
2+
import type { CdkCustomResourceEvent, CdkCustomResourceHandler } from 'aws-lambda';
23
import type { pki } from 'node-forge';
34
import { generate } from 'selfsigned';
4-
import { ACMClient, ImportCertificateCommand } from '@aws-sdk/client-acm';
55

66
const acmClient = new ACMClient({});
77

8+
export type CustomResourceProps = {
9+
certificateDetails: { commonName: string; [key: string]: string };
10+
tags?: { key: string; value: string }[];
11+
};
12+
13+
type ResourceProps = CdkCustomResourceEvent['ResourceProperties'] & CustomResourceProps
14+
815
export const handler: CdkCustomResourceHandler = async (event) => {
916
if (event.RequestType == 'Delete') {
1017
// TODO: remove from imports?
1118
return {};
1219
}
1320

14-
const certificateDetails = event.ResourceProperties.certificateDetails;
21+
const resourceProps = event.ResourceProperties as ResourceProps;
22+
const certificateDetails = resourceProps.certificateDetails;
1523
const certFields: pki.CertificateField[] = Object.keys(certificateDetails).map(key => ({
1624
name: key,
1725
value: certificateDetails[key],
@@ -25,7 +33,13 @@ export const handler: CdkCustomResourceHandler = async (event) => {
2533
CertificateArn: event.RequestType == 'Update' ? event.PhysicalResourceId : undefined,
2634
Certificate: new Uint8Array(Buffer.from(generatedCertificate.cert, 'utf-8')),
2735
PrivateKey: new Uint8Array(Buffer.from(generatedCertificate.private, 'utf-8')),
28-
Tags: event.ResourceProperties.tags,
36+
Tags: resourceProps.tags?.map(({
37+
key,
38+
value,
39+
}) => ({
40+
Key: key,
41+
Value: value,
42+
})),
2943
}));
3044

3145
console.log('Import result', importResult.CertificateArn);

0 commit comments

Comments
 (0)