Skip to content

Commit 2b59ed1

Browse files
authored
fix(appconfig): fromDeploymentStrategyId takes an enum-like class rather than a string (#28743)
Previously, we were typing this as a `string` and providing an enum for `PredefinedDeploymentStrategyId`s. This is a CDK anti-pattern because this makes the enum undiscoverable, since users see that it is typed only as a `string`. It also may not work in non-TS languages. Instead, we are moving the type to explicitly be an enum-like class. Follow up from #28671. BREAKING CHANGE: `deploymentStrategyId` prop in `fromDeploymentStrategyId` now takes a `DeploymentStrategyId` rather than a `string`. To import a predefined deployment strategy id, use `DeploymentStrategyId.CANARY_10_PERCENT_20_MINUTES`. Otherwise, use `DeploymentStrategyId.fromString('abc123')`. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 3960720 commit 2b59ed1

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

packages/@aws-cdk/aws-appconfig-alpha/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ new appconfig.DeploymentStrategy(this, 'MyDeploymentStrategy', {
7070
});
7171
```
7272

73+
Importing a deployment strategy by ID:
74+
75+
```ts
76+
appconfig.DeploymentStrategy.fromDeploymentStrategyId(this, 'MyImportedDeploymentStrategy', appconfig.DeploymentStrategyId.fromString('abc123'));
77+
```
78+
79+
Importing an AWS AppConfig predefined deployment strategy by ID:
80+
81+
```ts
82+
appconfig.DeploymentStrategy.fromDeploymentStrategyId(
83+
this,
84+
'MyImportedPredefinedDeploymentStrategy',
85+
appconfig.DeploymentStrategyId.CANARY_10_PERCENT_20_MINUTES,
86+
);
87+
```
88+
7389
## Configuration
7490

7591
A configuration is a higher-level construct that can either be a `HostedConfiguration` (stored internally through AWS

packages/@aws-cdk/aws-appconfig-alpha/awslint.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
"docs-public-apis:@aws-cdk/aws-appconfig-alpha.IConfiguration",
2626
"docs-public-apis:@aws-cdk/aws-appconfig-alpha.IApplication",
2727

28-
"no-unused-type:@aws-cdk/aws-appconfig-alpha.PredefinedDeploymentStrategyId",
2928
"ref-via-interface:@aws-cdk/aws-appconfig-alpha.Application.addAgentToEcs.taskDef",
3029
"events-in-interface",
3130
"events-method-signature",
32-
"events-generic"
31+
"events-generic",
32+
"from-signature:@aws-cdk/aws-appconfig-alpha.DeploymentStrategy.fromDeploymentStrategyId.params[2]"
3333
]
3434
}

packages/@aws-cdk/aws-appconfig-alpha/lib/deployment-strategy.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ export class DeploymentStrategy extends Resource implements IDeploymentStrategy
6666
* @param id The name of the deployment strategy construct
6767
* @param deploymentStrategyId The ID of the deployment strategy
6868
*/
69-
public static fromDeploymentStrategyId(scope: Construct, id: string, deploymentStrategyId: string): IDeploymentStrategy {
69+
public static fromDeploymentStrategyId(scope: Construct, id: string, deploymentStrategyId: DeploymentStrategyId): IDeploymentStrategy {
7070
const stack = Stack.of(scope);
7171
const deploymentStrategyArn = stack.formatArn({
7272
service: 'appconfig',
7373
resource: 'deploymentstrategy',
74-
resourceName: deploymentStrategyId,
74+
resourceName: deploymentStrategyId.id,
7575
});
7676

7777
class Import extends Resource implements IDeploymentStrategy {
78-
public readonly deploymentStrategyId = deploymentStrategyId;
78+
public readonly deploymentStrategyId = deploymentStrategyId.id;
7979
public readonly deploymentStrategyArn = deploymentStrategyArn;
8080
}
8181

@@ -182,36 +182,52 @@ export enum GrowthType {
182182
}
183183

184184
/**
185-
* Defines the deployment strategy ID's of AWS AppConfig predefined strategies.
185+
* Defines the deployment strategy ID's of AWS AppConfig deployment strategies.
186186
*
187187
* @see https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-creating-deployment-strategy.html
188188
*/
189-
export enum PredefinedDeploymentStrategyId {
189+
export abstract class DeploymentStrategyId {
190190
/**
191191
* **AWS Recommended**. This strategy processes the deployment exponentially using a 10% growth factor over 20 minutes.
192192
* AWS AppConfig recommends using this strategy for production deployments because it aligns with AWS best practices
193193
* for configuration deployments.
194194
*/
195-
CANARY_10_PERCENT_20_MINUTES = 'AppConfig.Canary10Percent20Minutes',
195+
public static readonly CANARY_10_PERCENT_20_MINUTES = DeploymentStrategyId.fromString('AppConfig.Canary10Percent20Minutes');
196196

197197
/**
198198
* **Testing/Demonstration**. This strategy deploys the configuration to half of all targets every 30 seconds for a
199199
* one-minute deployment. AWS AppConfig recommends using this strategy only for testing or demonstration purposes because
200200
* it has a short duration and bake time.
201201
*/
202-
LINEAR_50_PERCENT_EVERY_30_SECONDS = 'AppConfig.Linear50PercentEvery30Seconds',
202+
public static readonly LINEAR_50_PERCENT_EVERY_30_SECONDS = DeploymentStrategyId.fromString('AppConfig.Linear50PercentEvery30Seconds');
203203

204204
/**
205205
* **AWS Recommended**. This strategy deploys the configuration to 20% of all targets every six minutes for a 30 minute deployment.
206206
* AWS AppConfig recommends using this strategy for production deployments because it aligns with AWS best practices
207207
* for configuration deployments.
208208
*/
209-
LINEAR_20_PERCENT_EVERY_6_MINUTES = 'AppConfig.Linear20PercentEvery6Minutes',
209+
public static readonly LINEAR_20_PERCENT_EVERY_6_MINUTES = DeploymentStrategyId.fromString('AppConfig.Linear20PercentEvery6Minutes');
210210

211211
/**
212212
* **Quick**. This strategy deploys the configuration to all targets immediately.
213213
*/
214-
ALL_AT_ONCE = 'AppConfig.AllAtOnce',
214+
public static readonly ALL_AT_ONCE = DeploymentStrategyId.fromString('AppConfig.AllAtOnce');
215+
216+
/**
217+
* Builds a deployment strategy ID from a string.
218+
*
219+
* @param deploymentStrategyId The deployment strategy ID.
220+
*/
221+
public static fromString(deploymentStrategyId: string): DeploymentStrategyId {
222+
return {
223+
id: deploymentStrategyId,
224+
};
225+
}
226+
227+
/**
228+
* The deployment strategy ID.
229+
*/
230+
public abstract readonly id: string;
215231
}
216232

217233
/**

packages/@aws-cdk/aws-appconfig-alpha/test/deployment-strategy.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as cdk from 'aws-cdk-lib';
22
import { App } from 'aws-cdk-lib';
33
import { Template } from 'aws-cdk-lib/assertions';
4-
import { DeploymentStrategy, PredefinedDeploymentStrategyId, RolloutStrategy } from '../lib';
4+
import { DeploymentStrategy, DeploymentStrategyId, RolloutStrategy } from '../lib';
55

66
describe('deployment strategy', () => {
77
test('default deployment strategy', () => {
@@ -166,7 +166,7 @@ describe('deployment strategy', () => {
166166
account: '123456789012',
167167
},
168168
});
169-
const deploymentStrategy = DeploymentStrategy.fromDeploymentStrategyId(stack, 'MyDeploymentStrategy', 'abc123');
169+
const deploymentStrategy = DeploymentStrategy.fromDeploymentStrategyId(stack, 'MyDeploymentStrategy', DeploymentStrategyId.fromString('abc123'));
170170

171171
expect(deploymentStrategy.deploymentStrategyId).toEqual('abc123');
172172
expect(deploymentStrategy.env.account).toEqual('123456789012');
@@ -181,7 +181,7 @@ describe('deployment strategy', () => {
181181
account: '123456789012',
182182
},
183183
});
184-
const deploymentStrategy = DeploymentStrategy.fromDeploymentStrategyId(stack, 'MyDeploymentStrategy', PredefinedDeploymentStrategyId.ALL_AT_ONCE);
184+
const deploymentStrategy = DeploymentStrategy.fromDeploymentStrategyId(stack, 'MyDeploymentStrategy', DeploymentStrategyId.ALL_AT_ONCE);
185185

186186
expect(deploymentStrategy.deploymentStrategyId).toEqual('AppConfig.AllAtOnce');
187187
expect(deploymentStrategy.env.account).toEqual('123456789012');
@@ -196,7 +196,7 @@ describe('deployment strategy', () => {
196196
account: '123456789012',
197197
},
198198
});
199-
const deploymentStrategy = DeploymentStrategy.fromDeploymentStrategyId(stack, 'MyDeploymentStrategy', PredefinedDeploymentStrategyId.CANARY_10_PERCENT_20_MINUTES);
199+
const deploymentStrategy = DeploymentStrategy.fromDeploymentStrategyId(stack, 'MyDeploymentStrategy', DeploymentStrategyId.CANARY_10_PERCENT_20_MINUTES);
200200

201201
expect(deploymentStrategy.deploymentStrategyId).toEqual('AppConfig.Canary10Percent20Minutes');
202202
expect(deploymentStrategy.env.account).toEqual('123456789012');
@@ -211,7 +211,7 @@ describe('deployment strategy', () => {
211211
account: '123456789012',
212212
},
213213
});
214-
const deploymentStrategy = DeploymentStrategy.fromDeploymentStrategyId(stack, 'MyDeploymentStrategy', PredefinedDeploymentStrategyId.LINEAR_50_PERCENT_EVERY_30_SECONDS);
214+
const deploymentStrategy = DeploymentStrategy.fromDeploymentStrategyId(stack, 'MyDeploymentStrategy', DeploymentStrategyId.LINEAR_50_PERCENT_EVERY_30_SECONDS);
215215

216216
expect(deploymentStrategy.deploymentStrategyId).toEqual('AppConfig.Linear50PercentEvery30Seconds');
217217
expect(deploymentStrategy.env.account).toEqual('123456789012');

0 commit comments

Comments
 (0)