Skip to content

Commit 2075559

Browse files
authored
fix(appconfig-alpha): extensions always create cdk diff (#28264)
Fixing extensions CDK diff since before it would always generate a new diff even if nothing changed. Closes #27676. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d64a85b commit 2075559

14 files changed

+889
-467
lines changed

packages/@aws-cdk/aws-appconfig-alpha/lib/application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ export class Application extends ApplicationBase {
352352
resourceName: this.applicationId,
353353
});
354354

355-
this.extensible = new ExtensibleBase(scope, this.applicationArn, this.name);
355+
this.extensible = new ExtensibleBase(this, this.applicationArn, this.name);
356356
}
357357
}
358358

packages/@aws-cdk/aws-appconfig-alpha/lib/configuration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ export class HostedConfiguration extends ConfigurationBase {
455455
resource: 'application',
456456
resourceName: `${this.applicationId}/configurationprofile/${this.configurationProfileId}`,
457457
});
458-
this.extensible = new ExtensibleBase(scope, this.configurationProfileArn, this.name);
458+
this.extensible = new ExtensibleBase(this, this.configurationProfileArn, this.name);
459459

460460
this.content = props.content.content;
461461
this.contentType = props.content.contentType;
@@ -617,7 +617,7 @@ export class SourcedConfiguration extends ConfigurationBase {
617617
resource: 'application',
618618
resourceName: `${this.applicationId}/configurationprofile/${this.configurationProfileId}`,
619619
});
620-
this.extensible = new ExtensibleBase(scope, this.configurationProfileArn, this.name);
620+
this.extensible = new ExtensibleBase(this, this.configurationProfileArn, this.name);
621621

622622
this.addExistingEnvironmentsToApplication();
623623
this.deployConfigToEnvironments();

packages/@aws-cdk/aws-appconfig-alpha/lib/environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export class Environment extends EnvironmentBase {
269269
resource: 'application',
270270
resourceName: `${this.applicationId}/environment/${this.environmentId}`,
271271
});
272-
this.extensible = new ExtensibleBase(scope, this.environmentArn, this.name);
272+
this.extensible = new ExtensibleBase(this, this.environmentArn, this.name);
273273

274274
this.application.addExistingEnvironment(this);
275275
}

packages/@aws-cdk/aws-appconfig-alpha/lib/extension.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export class LambdaDestination implements IEventDestination {
7575
this.policyDocument = new iam.PolicyDocument({
7676
statements: [policy],
7777
});
78+
79+
if (!func.permissionsNode.tryFindChild('AppConfigPermission')) {
80+
func.addPermission('AppConfigPermission', {
81+
principal: new iam.ServicePrincipal('appconfig.amazonaws.com'),
82+
});
83+
}
7884
}
7985
}
8086

@@ -500,21 +506,19 @@ export class Extension extends Resource implements IExtension {
500506
this.parameters = props.parameters;
501507

502508
const resource = new CfnExtension(this, 'Resource', {
503-
actions: this.actions.reduce((acc: {[key: string]: {[key: string]: string}[]}, cur: Action) => {
509+
actions: this.actions.reduce((acc: {[key: string]: {[key: string]: string}[]}, cur: Action, index: number) => {
504510
const extensionUri = cur.eventDestination.extensionUri;
505511
const sourceType = cur.eventDestination.type;
506512
this.executionRole = cur.executionRole;
513+
const name = cur.name ?? `${this.name}-${index}`;
507514
cur.actionPoints.forEach((actionPoint) => {
508515
acc[actionPoint] = [
509516
{
510-
Name: cur.name || Names.uniqueResourceName(this, {
511-
maxLength: 64,
512-
separator: '-',
513-
}),
517+
Name: name,
514518
Uri: extensionUri,
515519
...(sourceType === SourceType.EVENTS || cur.invokeWithoutExecutionRole
516520
? {}
517-
: { RoleArn: this.executionRole?.roleArn || this.getExecutionRole(cur.eventDestination).roleArn }),
521+
: { RoleArn: this.executionRole?.roleArn || this.getExecutionRole(cur.eventDestination, name).roleArn }),
518522
...(cur.description ? { Description: cur.description } : {}),
519523
},
520524
];
@@ -543,8 +547,10 @@ export class Extension extends Resource implements IExtension {
543547
});
544548
}
545549

546-
private getExecutionRole(eventDestination: IEventDestination): iam.IRole {
547-
this.executionRole = new iam.Role(this, `Role${getHash(eventDestination.extensionUri)}`, {
550+
private getExecutionRole(eventDestination: IEventDestination, actionName: string): iam.IRole {
551+
const versionNumber = this.latestVersionNumber ? this.latestVersionNumber + 1 : 1;
552+
const combinedObjects = stringifyObjects(this.name, versionNumber, actionName);
553+
this.executionRole = new iam.Role(this, `Role${getHash(combinedObjects)}`, {
548554
roleName: PhysicalName.GENERATE_IF_NEEDED,
549555
assumedBy: new iam.ServicePrincipal('appconfig.amazonaws.com'),
550556
inlinePolicies: {
@@ -652,13 +658,13 @@ export class ExtensibleBase implements IExtensible {
652658
}
653659

654660
public addExtension(extension: IExtension) {
655-
this.addExtensionAssociation(extension, {
656-
parameters: extension.parameters,
657-
});
661+
this.addExtensionAssociation(extension);
658662
}
659663

660664
private getExtensionForActionPoint(eventDestination: IEventDestination, actionPoint: ActionPoint, options?: ExtensionOptions) {
661-
const extension = new Extension(this.scope, `Extension${this.getExtensionHash(eventDestination, actionPoint, options)}`, {
665+
const name = options?.name || this.getExtensionDefaultName();
666+
const versionNumber = options?.latestVersionNumber ? options?.latestVersionNumber + 1 : 1;
667+
const extension = new Extension(this.scope, `Extension${this.getExtensionHash(name, versionNumber)}`, {
662668
actions: [
663669
new Action({
664670
eventDestination,
@@ -667,20 +673,22 @@ export class ExtensibleBase implements IExtensible {
667673
],
668674
}),
669675
],
676+
name,
670677
...(options?.description ? { description: options.description } : {}),
671678
...(options?.latestVersionNumber ? { latestVersionNumber: options.latestVersionNumber } : {}),
672-
...(options?.name ? { name: options.name } : {}),
673679
...(options?.parameters ? { parameters: options.parameters } : {}),
674680
});
675-
this.addExtensionAssociation(extension, options);
681+
this.addExtensionAssociation(extension);
676682
}
677683

678-
private addExtensionAssociation(extension: IExtension, options?: ExtensionOptions) {
679-
new CfnExtensionAssociation(this.scope, `AssociationResource${this.getExtensionAssociationHash(extension)}`, {
684+
private addExtensionAssociation(extension: IExtension) {
685+
const versionNumber = extension?.latestVersionNumber ? extension?.latestVersionNumber + 1 : 1;
686+
const name = extension.name ?? this.getExtensionDefaultName();
687+
new CfnExtensionAssociation(this.scope, `AssociationResource${this.getExtensionAssociationHash(name, versionNumber)}`, {
680688
extensionIdentifier: extension.extensionId,
681689
resourceIdentifier: this.resourceArn,
682690
extensionVersionNumber: extension.extensionVersionNumber,
683-
parameters: options?.parameters?.reduce((acc: {[key: string]: string}, cur: Parameter) => {
691+
parameters: extension.parameters?.reduce((acc: {[key: string]: string}, cur: Parameter) => {
684692
if (cur.value) {
685693
acc[cur.name] = cur.value;
686694
}
@@ -689,16 +697,23 @@ export class ExtensibleBase implements IExtensible {
689697
});
690698
}
691699

692-
private getExtensionHash(eventDestination: IEventDestination, actionPoint: ActionPoint, options?: ExtensionOptions) {
693-
const combinedString = stringifyObjects(eventDestination, actionPoint, options);
700+
private getExtensionHash(name: string, versionNumber: number) {
701+
const combinedString = stringifyObjects(name, versionNumber);
694702
return getHash(combinedString);
695703
}
696704

697-
private getExtensionAssociationHash(extension: IExtension) {
698-
const resourceIdentifier = this.resourceName ? this.resourceName : this.resourceArn;
699-
const combinedString = stringifyObjects(resourceIdentifier, extension.name, extension.extensionVersionNumber);
705+
private getExtensionAssociationHash(name: string, versionNumber: number) {
706+
const resourceIdentifier = this.resourceName ?? this.resourceArn;
707+
const combinedString = stringifyObjects(resourceIdentifier, name, versionNumber);
700708
return getHash(combinedString);
701709
}
710+
711+
private getExtensionDefaultName() {
712+
return Names.uniqueResourceName(this.scope, {
713+
maxLength: 54,
714+
separator: '-',
715+
}) + '-Extension';
716+
}
702717
}
703718

704719
/**

0 commit comments

Comments
 (0)