diff --git a/.projen/deps.json b/.projen/deps.json index af335c16..db56a01f 100644 --- a/.projen/deps.json +++ b/.projen/deps.json @@ -183,6 +183,11 @@ "name": "@aws-cdk/core", "version": "^1.128.0", "type": "runtime" + }, + { + "name": "@renovosolutions/cdk-library-one-time-event", + "version": "^0.0.2", + "type": "runtime" } ], "//": "~~ Generated by projen. To modify, edit .projenrc.js and run \"npx projen\"." diff --git a/.projen/tasks.json b/.projen/tasks.json index e8c4f9dc..eaf1ac10 100644 --- a/.projen/tasks.json +++ b/.projen/tasks.json @@ -296,7 +296,7 @@ "exec": "yarn install --check-files" }, { - "exec": "yarn upgrade @aws-cdk/assert @types/jest @types/node @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-import-resolver-node eslint-import-resolver-typescript eslint-plugin-import jest jest-junit jsii jsii-diff jsii-docgen jsii-pacmak json-schema npm-check-updates standard-version ts-jest typescript @aws-cdk/aws-events-targets @aws-cdk/aws-events @aws-cdk/aws-iam @aws-cdk/aws-lambda @aws-cdk/aws-route53 @aws-cdk/aws-s3 @aws-cdk/aws-sns-subscriptions @aws-cdk/aws-sns @aws-cdk/core constructs @aws-cdk/aws-events-targets @aws-cdk/aws-events @aws-cdk/aws-iam @aws-cdk/aws-lambda @aws-cdk/aws-route53 @aws-cdk/aws-s3 @aws-cdk/aws-sns-subscriptions @aws-cdk/aws-sns @aws-cdk/core" + "exec": "yarn upgrade @aws-cdk/assert @types/jest @types/node @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-import-resolver-node eslint-import-resolver-typescript eslint-plugin-import jest jest-junit jsii jsii-diff jsii-docgen jsii-pacmak json-schema npm-check-updates standard-version ts-jest typescript @aws-cdk/aws-events-targets @aws-cdk/aws-events @aws-cdk/aws-iam @aws-cdk/aws-lambda @aws-cdk/aws-route53 @aws-cdk/aws-s3 @aws-cdk/aws-sns-subscriptions @aws-cdk/aws-sns @aws-cdk/core constructs @aws-cdk/aws-events-targets @aws-cdk/aws-events @aws-cdk/aws-iam @aws-cdk/aws-lambda @aws-cdk/aws-route53 @aws-cdk/aws-s3 @aws-cdk/aws-sns-subscriptions @aws-cdk/aws-sns @aws-cdk/core @renovosolutions/cdk-library-one-time-event" }, { "exec": "npx projen" diff --git a/.projenrc.js b/.projenrc.js index cba569a4..41bdc0ba 100644 --- a/.projenrc.js +++ b/.projenrc.js @@ -18,6 +18,9 @@ const project = new AwsCdkConstructLibrary({ '@aws-cdk/aws-sns-subscriptions', '@aws-cdk/aws-route53', ], + deps: [ + '@renovosolutions/cdk-library-one-time-event@^0.0.2', + ], keywords: [ 'letsencrypt', 'certbot', diff --git a/package.json b/package.json index ce292b66..5ce232a7 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,8 @@ "@aws-cdk/aws-s3": "^1.128.0", "@aws-cdk/aws-sns": "^1.128.0", "@aws-cdk/aws-sns-subscriptions": "^1.128.0", - "@aws-cdk/core": "^1.128.0" + "@aws-cdk/core": "^1.128.0", + "@renovosolutions/cdk-library-one-time-event": "^0.0.2" }, "bundledDependencies": [], "keywords": [ diff --git a/src/index.ts b/src/index.ts index f213ebe8..d6fb536a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import * as s3 from '@aws-cdk/aws-s3'; import * as sns from '@aws-cdk/aws-sns'; import * as subscriptions from '@aws-cdk/aws-sns-subscriptions'; import * as cdk from '@aws-cdk/core'; +import * as oneTimeEvents from '@renovosolutions/cdk-library-one-time-event'; export interface ICertbotProps { /** @@ -206,23 +207,10 @@ export class Certbot extends cdk.Construct { }); if (props.runOnDeploy) { - const dateToCron = (date:Date) => { - const minutesToAdd = props.runOnDeployWaitMinutes || 10; - const future = new Date(date.getTime() + minutesToAdd * 60000); - const minutes = future.getUTCMinutes(); - const hours = future.getUTCHours(); - const days = future.getUTCDate(); - const months = future.getUTCMonth() + 1; - const dayOfWeek = '?'; - const years = future.getUTCFullYear(); - - return `${minutes} ${hours} ${days} ${months} ${dayOfWeek} ${years}`; - }; - - const oneTimeSchedule = events.Schedule.expression('cron(' + dateToCron(new Date()) + ')'); - new events.Rule(this, 'triggerImmediate', { - schedule: oneTimeSchedule, + schedule: new oneTimeEvents.OnDeploy(this, 'schedule', { + offsetMinutes: 10, + }).schedule, targets: [new targets.LambdaFunction(this.handler)], }); } diff --git a/yarn.lock b/yarn.lock index 2c0d69ae..2839c115 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1357,6 +1357,14 @@ resolved "https://registry.yarnpkg.com/@oozcitak/util/-/util-8.3.8.tgz#10f65fe1891fd8cde4957360835e78fd1936bfdd" integrity sha512-T8TbSnGsxo6TDBJx/Sgv/BlVJL3tshxZP7Aq5R1mSnM5OcHY2dQaxLMu2+E8u3gN0MLOzdjurqN4ZRVuzQycOQ== +"@renovosolutions/cdk-library-one-time-event@^0.0.2": + version "0.0.2" + resolved "https://registry.yarnpkg.com/@renovosolutions/cdk-library-one-time-event/-/cdk-library-one-time-event-0.0.2.tgz#01740c42f0f4103f87c866041fe45ca74f59db25" + integrity sha512-mpqnVKzna2FcML9liQMrc/oRxPpLxnqJV2AILQYAW0EmoGlXQUPuk78oZPvgKW0rqni2l32djcmxSYr0VJjpUg== + dependencies: + "@aws-cdk/aws-events" "^1.128.0" + "@aws-cdk/core" "^1.128.0" + "@sindresorhus/is@^0.14.0": version "0.14.0" resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea"