diff --git a/.gitignore b/.gitignore index a6e5736..c854617 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ node_modules/ yarn-error.log +lerna-debug.log # Ignore user config files .vscode/ diff --git a/packages/cdk-appconfig-handler-hosted-configuration-version/config/custom-environment-variables.json b/packages/cdk-appconfig-handler-hosted-configuration-version/config/custom-environment-variables.json new file mode 100644 index 0000000..6ac6a59 --- /dev/null +++ b/packages/cdk-appconfig-handler-hosted-configuration-version/config/custom-environment-variables.json @@ -0,0 +1,22 @@ +{ + "retryAppConfig": { + "max": { + "__name": "RETRY_APPCONFIG_MAX", + "__format": "number" + }, + "base": { + "__name": "RETRY_APPCONFIG_BASE_MS", + "__format": "number" + } + }, + "retryS3": { + "max": { + "__name": "RETRY_S3_MAX", + "__format": "number" + }, + "base": { + "__name": "RETRY_S3_BASE_MS", + "__format": "number" + } + } +} diff --git a/packages/cdk-appconfig-handler-hosted-configuration-version/config/default.json b/packages/cdk-appconfig-handler-hosted-configuration-version/config/default.json new file mode 100644 index 0000000..305eee5 --- /dev/null +++ b/packages/cdk-appconfig-handler-hosted-configuration-version/config/default.json @@ -0,0 +1,10 @@ +{ + "retryAppConfig": { + "max": 12, + "base": 100 + }, + "retryS3": { + "max": 12, + "base": 100 + } +} diff --git a/packages/cdk-appconfig-handler-hosted-configuration-version/index.js b/packages/cdk-appconfig-handler-hosted-configuration-version/index.js index eee9825..7be84df 100644 --- a/packages/cdk-appconfig-handler-hosted-configuration-version/index.js +++ b/packages/cdk-appconfig-handler-hosted-configuration-version/index.js @@ -1,5 +1,11 @@ const S3 = require('aws-sdk/clients/s3'); const AppConfig = require('aws-sdk/clients/appconfig'); +const config = require('config'); + +const RETRY_APPCONFIG_MAX = config.get('retryAppConfig.max'); +const RETRY_APPCONFIG_BASE_MS = config.get('retryAppConfig.base'); +const RETRY_S3_MAX = config.get('retryS3.max'); +const RETRY_S3_BASE_MS = config.get('retryS3.base'); async function getContent(contentConfig) { console.log('getContent', contentConfig); @@ -15,7 +21,12 @@ async function getContent(contentConfig) { throw new Error(`ContentConfig requires either InlineContent or S3Location`); } - const s3 = new S3(); + const s3 = new S3({ + maxRetries: RETRY_S3_MAX, + retryDelayOptions: { + base: RETRY_S3_BASE_MS + } + }); const params = { Bucket: s3Location.BucketName, @@ -33,7 +44,12 @@ async function createConfigurationVersion(props) { console.log('createConfigurationVersion', props); const content = await getContent(props.ContentConfig); - const appconfig = new AppConfig(); + const appconfig = new AppConfig({ + maxRetries: RETRY_APPCONFIG_MAX, + retryDelayOptions: { + base: RETRY_APPCONFIG_BASE_MS + } + }); const params = { ApplicationId: props.ApplicationId, ConfigurationProfileId: props.ConfigurationProfileId, @@ -51,7 +67,12 @@ async function createConfigurationVersion(props) { async function deleteConfigurationVersion(physicalId, props) { console.log('deleteConfigurationVersion', props); - const appconfig = new AppConfig(); + const appconfig = new AppConfig({ + maxRetries: RETRY_APPCONFIG_MAX, + retryDelayOptions: { + base: RETRY_APPCONFIG_BASE_MS + } + }); const params = { ApplicationId: props.ApplicationId, ConfigurationProfileId: props.ConfigurationProfileId, diff --git a/packages/cdk-appconfig-handler-hosted-configuration-version/package.json b/packages/cdk-appconfig-handler-hosted-configuration-version/package.json index 9524df0..e538477 100644 --- a/packages/cdk-appconfig-handler-hosted-configuration-version/package.json +++ b/packages/cdk-appconfig-handler-hosted-configuration-version/package.json @@ -24,6 +24,7 @@ "jest": "^26.6.3" }, "dependencies": { - "aws-sdk": "^2.903.0" + "aws-sdk": "^2.903.0", + "config": "^3.3.8" } } diff --git a/packages/cdk-appconfig-handler-hosted-configuration-version/yarn.lock b/packages/cdk-appconfig-handler-hosted-configuration-version/yarn.lock index 109a03c..eac65f0 100644 --- a/packages/cdk-appconfig-handler-hosted-configuration-version/yarn.lock +++ b/packages/cdk-appconfig-handler-hosted-configuration-version/yarn.lock @@ -1067,6 +1067,13 @@ concat-map@0.0.1: resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= +config@^3.3.8: + version "3.3.8" + resolved "https://registry.yarnpkg.com/config/-/config-3.3.8.tgz#14ef7aef22af25877fdaee696ec64d761feb7be0" + integrity sha512-rFzF6VESOdp7wAXFlB9IOZI4ouL05g3A03v2eRcTHj2JBQaTNJ40zhAUl5wRbWHqLZ+uqp/7OE0BWWtAVgrong== + dependencies: + json5 "^2.2.1" + convert-source-map@^1.4.0, convert-source-map@^1.6.0, convert-source-map@^1.7.0: version "1.7.0" resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.7.0.tgz#17a2cb882d7f77d3490585e2ce6c524424a3a442" @@ -2378,6 +2385,11 @@ json5@^2.1.2: dependencies: minimist "^1.2.5" +json5@^2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c" + integrity sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA== + jsprim@^1.2.2: version "1.4.1" resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2" diff --git a/packages/cdk-appconfig/lib/hosted_configuration_version.ts b/packages/cdk-appconfig/lib/hosted_configuration_version.ts index 2f06ec2..f9b83e9 100644 --- a/packages/cdk-appconfig/lib/hosted_configuration_version.ts +++ b/packages/cdk-appconfig/lib/hosted_configuration_version.ts @@ -30,6 +30,11 @@ export interface IHostedConfigurationVersion { readonly hostedConfigurationVersionArn: string; } +export interface RetryOptions { + readonly maxRetries: number; + readonly baseDelay: cdk.Duration; +} + export interface HostedConfigurationVersionProps { readonly application: IApplication; readonly configurationProfile: IConfigurationProfile; @@ -39,6 +44,8 @@ export interface HostedConfigurationVersionProps { readonly latestVersionNumber?: string; readonly initOnly?: boolean; readonly removalPolicy?: cdk.RemovalPolicy; + readonly appConfigRetryOptions?: RetryOptions; + readonly s3RetryOptions?: RetryOptions; } export class HostedConfigurationVersion extends cdk.Resource implements IHostedConfigurationVersion { @@ -57,13 +64,26 @@ export class HostedConfigurationVersion extends cdk.Resource implements IHostedC const contentConfig = props.content.bind(this); - const onEventHandler = new lambda.SingletonFunction(this, 'OnEventHandler', { + const onEventHandler = new lambda.Function(this, 'OnEventHandler', { runtime: lambda.Runtime.NODEJS_14_X, code: lambda.Code.fromAsset(HANDLER_CODE_PATH), handler: 'index.onEvent', - uuid: 'c67842de-c9ed-4cbb-906f-3b490af456b8' + // uuid: 'c67842de-c9ed-4cbb-906f-3b490af456b8' + timeout: props.appConfigRetryOptions || props.s3RetryOptions ? cdk.Duration.minutes(15) : undefined }); + if (props.appConfigRetryOptions) { + const { maxRetries, baseDelay } = props.appConfigRetryOptions; + onEventHandler.addEnvironment('RETRY_APPCONFIG_MAX', maxRetries.toString()); + onEventHandler.addEnvironment('RETRY_APPCONFIG_BASE_MS', baseDelay.toMilliseconds().toString()); + } + + if (props.s3RetryOptions) { + const { maxRetries, baseDelay } = props.s3RetryOptions; + onEventHandler.addEnvironment('RETRY_S3_MAX', maxRetries.toString()); + onEventHandler.addEnvironment('RETRY_S3_BASE_MS', baseDelay.toMilliseconds().toString()); + } + onEventHandler.addToRolePolicy( new iam.PolicyStatement({ actions: ['appconfig:CreateHostedConfigurationVersion', 'appconfig:DeleteHostedConfigurationVersion'],