Repository: RenovoSolutions/cdk-library-certbot (language: python)

Issue:
Allow setting a Lambda function description for the resulting function.



Discussion / hints:

Fix patch:
diff --git a/API.md b/API.md
index 3340290e..a963fdab 100644
--- a/API.md
+++ b/API.md
@@ -121,6 +121,30 @@ Whether or not to enable Lambda Insights.
 
 ---
 
+##### `functionDescription`<sup>Optional</sup> <a name="@renovosolutions/cdk-library-certbot.ICertbotProps.property.functionDescription"></a>
+
+```typescript
+public readonly functionDescription: string;
+```
+
+- *Type:* `string`
+
+The description for the resulting Lambda function.
+
+---
+
+##### `functionName`<sup>Optional</sup> <a name="@renovosolutions/cdk-library-certbot.ICertbotProps.property.functionName"></a>
+
+```typescript
+public readonly functionName: string;
+```
+
+- *Type:* `string`
+
+The name of the resulting Lambda function.
+
+---
+
 ##### `insightsARN`<sup>Optional</sup> <a name="@renovosolutions/cdk-library-certbot.ICertbotProps.property.insightsARN"></a>
 
 ```typescript
diff --git a/src/index.ts b/src/index.ts
index 729874d3..88c7eb5a 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -85,6 +85,14 @@ export interface ICertbotProps {
    * @default 10
    */
   runOnDeployWaitMinutes?: number;
+  /**
+   * The description for the resulting Lambda function.
+   */
+  functionDescription?: string;
+  /**
+   * The name of the resulting Lambda function.
+   */
+  functionName?: string;
 }
 
 export class Certbot extends cdk.Construct {
@@ -94,6 +102,7 @@ export class Certbot extends cdk.Construct {
   constructor(scope: cdk.Construct, id: string, props: ICertbotProps) {
     super(scope, id);
 
+    // Create a bucket if one is not provided
     if (props.bucket === undefined) {
       props.bucket = new s3.Bucket(this, 'bucket', {
         objectOwnership: s3.ObjectOwnership.BUCKET_OWNER_PREFERRED,
@@ -111,16 +120,20 @@ export class Certbot extends cdk.Construct {
 
     const functionDir = path.join(__dirname, '../function');
 
+    // Create an SNS topic if one is not provided
     if (props.snsTopic === undefined) {
       props.snsTopic = new sns.Topic(this, 'topic');
       props.snsTopic.addSubscription(new subscriptions.EmailSubscription(props.letsencryptEmail));
     }
 
-    props.layers = (props.layers === undefined) ? [] : props.layers;
-    props.runOnDeploy = (props.runOnDeploy === undefined ) ? true : props.runOnDeploy;
-    props.enableInsights = (props.enableInsights === undefined) ? false : props.enableInsights;
-    props.insightsARN = (props.insightsARN === undefined) ? 'arn:aws:lambda:' + cdk.Stack.of(this).region + ':580247275435:layer:LambdaInsightsExtension:14' : props.insightsARN;
+    // Set property defaults
+    props.layers = props.layers ?? [];
+    props.runOnDeploy = props.runOnDeploy ?? true;
+    props.functionDescription = props.functionDescription ?? 'Certbot Renewal Lambda for domain ' + props.letsencryptDomains.split(',')[0];
+    props.enableInsights = props.enableInsights ?? false;
+    props.insightsARN = props.insightsARN ?? 'arn:aws:lambda:' + cdk.Stack.of(this).region + ':580247275435:layer:LambdaInsightsExtension:14';
 
+    // Set up role policies
     let managedPolicies = [iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole')];
     if (props.enableInsights) {
       managedPolicies.push(iam.ManagedPolicy.fromAwsManagedPolicyName('CloudWatchLambdaInsightsExecutionRolePolicy'));
@@ -195,11 +208,14 @@ export class Certbot extends cdk.Construct {
 
     props.bucket.grantWrite(role);
 
+    // Create the Lambda function
     this.handler = new lambda.Function(this, 'handler', {
       runtime: lambda.Runtime.PYTHON_3_8,
       role,
       code: lambda.Code.fromAsset(functionDir + '/function.zip'),
       handler: 'index.handler',
+      functionName: props.functionName,
+      description: props.functionDescription,
       environment: {
         LETSENCRYPT_DOMAINS: props.letsencryptDomains,
         LETSENCRYPT_EMAIL: props.letsencryptEmail,
@@ -213,6 +229,7 @@ export class Certbot extends cdk.Construct {
       timeout: props.timeout || cdk.Duration.seconds(180),
     });
 
+    // Add function triggers
     new events.Rule(this, 'trigger', {
       schedule: props.schedule || events.Schedule.cron({ minute: '0', hour: '0', weekDay: '1' }),
       targets: [new targets.LambdaFunction(this.handler)],


Test patch:
diff --git a/test/__snapshots__/certbot.test.ts.snap b/test/__snapshots__/certbot.test.ts.snap
index 6b81c7ba..7e349081 100644
--- a/test/__snapshots__/certbot.test.ts.snap
+++ b/test/__snapshots__/certbot.test.ts.snap
@@ -175,12 +175,13 @@ Object {
             ],
           },
         },
+        "Description": "Certbot Renewal Lambda for domain test.local",
         "Environment": Object {
           "Variables": Object {
             "CERTIFICATE_BUCKET": Object {
               "Ref": "Certbotbucket4B0141F9",
             },
-            "LETSENCRYPT_DOMAINS": "test.local",
+            "LETSENCRYPT_DOMAINS": "test.local, www.test.local",
             "LETSENCRYPT_EMAIL": "test@test.local",
             "NOTIFICATION_SNS_ARN": Object {
               "Ref": "CertbottopicCA144CF6",
diff --git a/test/certbot.test.ts b/test/certbot.test.ts
index 7457464d..eafa94eb 100644
--- a/test/certbot.test.ts
+++ b/test/certbot.test.ts
@@ -14,7 +14,7 @@ test('Snapshot', () => {
   });
 
   new Certbot(stack, 'Certbot', {
-    letsencryptDomains: 'test.local',
+    letsencryptDomains: 'test.local, www.test.local',
     letsencryptEmail: 'test@test.local',
     hostedZoneNames: ['example.com'],
   });