@@ -3,7 +3,7 @@ import * as cfn from '@aws-cdk/aws-cloudformation';
33import { getAccountId } from '../utils/accounts' ;
44import { JsonOutputValue } from '../common/json-output' ;
55import { getVpcConfig } from '../common/get-all-vpcs' ;
6- import { VpcOutputFinder } from '@aws-accelerator/common-outputs/src/vpc' ;
6+ import { VpcOutputFinder , SharedSecurityGroupIndexOutput } from '@aws-accelerator/common-outputs/src/vpc' ;
77import * as ec2 from '@aws-cdk/aws-ec2' ;
88import { PeeringConnectionConfig , VpcConfigType } from '@aws-accelerator/common-config/src' ;
99import { getVpcSharedAccountKeys } from '../common/vpc-subnet-sharing' ;
@@ -26,6 +26,7 @@ import * as centralServices from '../deployments/central-services';
2626import * as guardDutyDeployment from '../deployments/guardduty' ;
2727import * as snsDeployment from '../deployments/sns' ;
2828import * as ssmDeployment from '../deployments/ssm' ;
29+ import { getStackJsonOutput } from '@aws-accelerator/common-outputs/src/stack-output' ;
2930
3031/**
3132 * This is the main entry point to deploy phase 2
@@ -170,11 +171,23 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
170171 continue ;
171172 }
172173
173- const securityGroupStack = new cfn . NestedStack (
174- accountStack ,
175- `SecurityGroups${ vpcConfig . name } -Shared-${ index + 1 } ` ,
176- ) ;
177- const securityGroups = new SecurityGroup ( securityGroupStack , `SecurityGroups-SharedAccount-${ index + 1 } ` , {
174+ /* **********************************************************
175+ * Saving index in outputs to handle nasty bug occur while
176+ * changing construct name when account is suspended
177+ * *********************************************************/
178+ const sgOutputs : SharedSecurityGroupIndexOutput [ ] = getStackJsonOutput ( outputs , {
179+ accountKey : sharedAccountKey ,
180+ outputType : 'SecurityGroupIndexOutput' ,
181+ region : vpcConfig . region ,
182+ } ) ;
183+
184+ const vpcSgIndex = sgOutputs . find ( sgO => sgO . vpcName === vpcConfig . name ) ;
185+ let sgIndex = index + 1 ;
186+ if ( vpcSgIndex ) {
187+ sgIndex = vpcSgIndex . index ;
188+ }
189+ const securityGroupStack = new cfn . NestedStack ( accountStack , `SecurityGroups${ vpcConfig . name } -Shared-${ sgIndex } ` ) ;
190+ const securityGroups = new SecurityGroup ( securityGroupStack , `SecurityGroups-SharedAccount-${ sgIndex } ` , {
178191 securityGroups : vpcConfig [ 'security-groups' ] ! ,
179192 vpcName : vpcConfig . name ,
180193 vpcId : vpcOutput . vpcId ,
@@ -184,16 +197,10 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
184197 installerVersion : context . installerVersion ,
185198 } ) ;
186199
187- const accountId = getAccountId ( accounts , accountKey ) ;
188- if ( ! accountId ) {
189- console . warn ( `Cannot find account with key ${ accountKey } ` ) ;
190- continue ;
191- }
192-
193200 // Add Tags Output
194201 const securityGroupsResources = Object . values ( securityGroups . securityGroupNameMapping ) ;
195202
196- new JsonOutputValue ( securityGroupStack , `SecurityGroupOutput${ vpcConfig . name } - ${ index } ` , {
203+ new JsonOutputValue ( securityGroupStack , `SecurityGroupOutput ` , {
197204 type : 'SecurityGroupsOutput' ,
198205 value : {
199206 vpcId : vpcOutput . vpcId ,
@@ -205,6 +212,20 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
205212 } ,
206213 } ) ;
207214
215+ new JsonOutputValue ( securityGroupStack , `SecurityGroupIndexOutput` , {
216+ type : 'SecurityGroupIndexOutput' ,
217+ value : {
218+ vpcName : vpcConfig . name ,
219+ index : sgIndex ,
220+ } ,
221+ } ) ;
222+
223+ const accountId = getAccountId ( accounts , accountKey ) ;
224+ if ( ! accountId ) {
225+ console . warn ( `Cannot find account with key ${ accountKey } ` ) ;
226+ continue ;
227+ }
228+
208229 new AddTagsToResourcesOutput ( securityGroupStack , `OutputSharedResources${ vpcConfig . name } -Shared-${ index } ` , {
209230 dependencies : securityGroupsResources ,
210231 produceResources : ( ) =>
0 commit comments