|
| 1 | +import * as cdk from 'aws-cdk-lib/core'; |
| 2 | +import { Construct } from 'constructs'; |
| 3 | +import { CfnAutoScalingConfiguration } from 'aws-cdk-lib/aws-apprunner'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Properties of the App Runner Auto Scaling Configuration. |
| 7 | + */ |
| 8 | +export interface AutoScalingConfigurationProps { |
| 9 | + /** |
| 10 | + * The name for the Auto Scaling Configuration. |
| 11 | + * |
| 12 | + * @default - a name generated by CloudFormation |
| 13 | + */ |
| 14 | + readonly autoScalingConfigurationName?: string; |
| 15 | + |
| 16 | + /** |
| 17 | + * The maximum number of concurrent requests that an instance processes. |
| 18 | + * If the number of concurrent requests exceeds this limit, App Runner scales the service up. |
| 19 | + * |
| 20 | + * Must be between 1 and 200. |
| 21 | + * |
| 22 | + * @default 100 |
| 23 | + */ |
| 24 | + readonly maxConcurrency?: number; |
| 25 | + |
| 26 | + /** |
| 27 | + * The maximum number of instances that a service scales up to. |
| 28 | + * At most maxSize instances actively serve traffic for your service. |
| 29 | + * |
| 30 | + * Must be between 1 and 25. |
| 31 | + * |
| 32 | + * @default 25 |
| 33 | + */ |
| 34 | + readonly maxSize?: number; |
| 35 | + |
| 36 | + /** |
| 37 | + * The minimum number of instances that App Runner provisions for a service. |
| 38 | + * The service always has at least minSize provisioned instances. |
| 39 | + * |
| 40 | + * |
| 41 | + * Must be between 1 and 25. |
| 42 | + * |
| 43 | + * @default 1 |
| 44 | + */ |
| 45 | + readonly minSize?: number; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Attributes for the App Runner Auto Scaling Configuration. |
| 50 | + */ |
| 51 | +export interface AutoScalingConfigurationAttributes { |
| 52 | + /** |
| 53 | + * The name of the Auto Scaling Configuration. |
| 54 | + */ |
| 55 | + readonly autoScalingConfigurationName: string; |
| 56 | + |
| 57 | + /** |
| 58 | + * The revision of the Auto Scaling Configuration. |
| 59 | + */ |
| 60 | + readonly autoScalingConfigurationRevision: number; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Represents the App Runner Auto Scaling Configuration. |
| 65 | + */ |
| 66 | +export interface IAutoScalingConfiguration extends cdk.IResource { |
| 67 | + /** |
| 68 | + * The ARN of the Auto Scaling Configuration. |
| 69 | + * @attribute |
| 70 | + */ |
| 71 | + readonly autoScalingConfigurationArn: string; |
| 72 | + |
| 73 | + /** |
| 74 | + * The Name of the Auto Scaling Configuration. |
| 75 | + * @attribute |
| 76 | + */ |
| 77 | + readonly autoScalingConfigurationName: string; |
| 78 | + |
| 79 | + /** |
| 80 | + * The revision of the Auto Scaling Configuration. |
| 81 | + * @attribute |
| 82 | + */ |
| 83 | + readonly autoScalingConfigurationRevision: number; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * The App Runner Auto Scaling Configuration. |
| 88 | + * |
| 89 | + * @resource AWS::AppRunner::AutoScalingConfiguration |
| 90 | + */ |
| 91 | +export class AutoScalingConfiguration extends cdk.Resource implements IAutoScalingConfiguration { |
| 92 | + /** |
| 93 | + * Imports an App Runner Auto Scaling Configuration from attributes |
| 94 | + */ |
| 95 | + public static fromAutoScalingConfigurationAttributes(scope: Construct, id: string, |
| 96 | + attrs: AutoScalingConfigurationAttributes): IAutoScalingConfiguration { |
| 97 | + const autoScalingConfigurationName = attrs.autoScalingConfigurationName; |
| 98 | + const autoScalingConfigurationRevision = attrs.autoScalingConfigurationRevision; |
| 99 | + |
| 100 | + class Import extends cdk.Resource implements IAutoScalingConfiguration { |
| 101 | + public readonly autoScalingConfigurationName = autoScalingConfigurationName; |
| 102 | + public readonly autoScalingConfigurationRevision = autoScalingConfigurationRevision; |
| 103 | + public readonly autoScalingConfigurationArn = cdk.Stack.of(this).formatArn({ |
| 104 | + resource: 'autoscalingconfiguration', |
| 105 | + service: 'apprunner', |
| 106 | + resourceName: `${attrs.autoScalingConfigurationName}/${attrs.autoScalingConfigurationRevision}`, |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + return new Import(scope, id); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Imports an App Runner Auto Scaling Configuration from its ARN |
| 115 | + */ |
| 116 | + public static fromArn(scope: Construct, id: string, autoScalingConfigurationArn: string): IAutoScalingConfiguration { |
| 117 | + const resourceParts = cdk.Fn.split('/', autoScalingConfigurationArn); |
| 118 | + |
| 119 | + if (!resourceParts || resourceParts.length < 3) { |
| 120 | + throw new Error(`Unexpected ARN format: ${autoScalingConfigurationArn}`); |
| 121 | + } |
| 122 | + |
| 123 | + const autoScalingConfigurationName = cdk.Fn.select(0, resourceParts); |
| 124 | + const autoScalingConfigurationRevision = Number(cdk.Fn.select(1, resourceParts)); |
| 125 | + |
| 126 | + class Import extends cdk.Resource implements IAutoScalingConfiguration { |
| 127 | + public readonly autoScalingConfigurationName = autoScalingConfigurationName; |
| 128 | + public readonly autoScalingConfigurationRevision = autoScalingConfigurationRevision; |
| 129 | + public readonly autoScalingConfigurationArn = autoScalingConfigurationArn; |
| 130 | + } |
| 131 | + |
| 132 | + return new Import(scope, id); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * The ARN of the Auto Scaling Configuration. |
| 137 | + * @attribute |
| 138 | + */ |
| 139 | + readonly autoScalingConfigurationArn: string; |
| 140 | + |
| 141 | + /** |
| 142 | + * The name of the Auto Scaling Configuration. |
| 143 | + * @attribute |
| 144 | + */ |
| 145 | + readonly autoScalingConfigurationName: string; |
| 146 | + |
| 147 | + /** |
| 148 | + * The revision of the Auto Scaling Configuration. |
| 149 | + * @attribute |
| 150 | + */ |
| 151 | + readonly autoScalingConfigurationRevision: number; |
| 152 | + |
| 153 | + public constructor(scope: Construct, id: string, props: AutoScalingConfigurationProps = {}) { |
| 154 | + super(scope, id, { |
| 155 | + physicalName: props.autoScalingConfigurationName, |
| 156 | + }); |
| 157 | + |
| 158 | + this.validateAutoScalingConfiguration(props); |
| 159 | + |
| 160 | + const resource = new CfnAutoScalingConfiguration(this, 'Resource', { |
| 161 | + autoScalingConfigurationName: props.autoScalingConfigurationName, |
| 162 | + maxConcurrency: props.maxConcurrency, |
| 163 | + maxSize: props.maxSize, |
| 164 | + minSize: props.minSize, |
| 165 | + }); |
| 166 | + |
| 167 | + this.autoScalingConfigurationArn = resource.attrAutoScalingConfigurationArn; |
| 168 | + this.autoScalingConfigurationRevision = resource.attrAutoScalingConfigurationRevision; |
| 169 | + this.autoScalingConfigurationName = resource.ref; |
| 170 | + } |
| 171 | + |
| 172 | + private validateAutoScalingConfiguration(props: AutoScalingConfigurationProps) { |
| 173 | + if ( |
| 174 | + props.autoScalingConfigurationName !== undefined && |
| 175 | + !cdk.Token.isUnresolved(props.autoScalingConfigurationName) && |
| 176 | + !/^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$/.test(props.autoScalingConfigurationName) |
| 177 | + ) { |
| 178 | + throw new Error(`autoScalingConfigurationName must match the ^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$ pattern, got ${props.autoScalingConfigurationName}`); |
| 179 | + } |
| 180 | + |
| 181 | + const isMinSizeDefined = typeof props.minSize === 'number'; |
| 182 | + const isMaxSizeDefined = typeof props.maxSize === 'number'; |
| 183 | + const isMaxConcurrencyDefined = typeof props.maxConcurrency === 'number'; |
| 184 | + |
| 185 | + if (isMinSizeDefined && (props.minSize < 1 || props.minSize > 25)) { |
| 186 | + throw new Error(`minSize must be between 1 and 25, got ${props.minSize}`); |
| 187 | + } |
| 188 | + |
| 189 | + if (isMaxSizeDefined && (props.maxSize < 1 || props.maxSize > 25)) { |
| 190 | + throw new Error(`maxSize must be between 1 and 25, got ${props.maxSize}`); |
| 191 | + } |
| 192 | + |
| 193 | + if (isMinSizeDefined && isMaxSizeDefined && !(props.minSize < props.maxSize)) { |
| 194 | + throw new Error('maxSize must be greater than minSize'); |
| 195 | + } |
| 196 | + |
| 197 | + if (isMaxConcurrencyDefined && (props.maxConcurrency < 1 || props.maxConcurrency > 200)) { |
| 198 | + throw new Error(`maxConcurrency must be between 1 and 200, got ${props.maxConcurrency}`); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments