@@ -27,14 +27,15 @@ import * as ssm from '../deployments/ssm/session-manager';
2727import * as macie from '../deployments/macie' ;
2828import * as guardDutyDeployment from '../deployments/guardduty' ;
2929import { PhaseInput } from './shared' ;
30- import { getIamUserPasswordSecretValue } from '../deployments/iam' ;
30+ import { createIamRoleOutput , getIamUserPasswordSecretValue } from '../deployments/iam' ;
3131import * as cwlCentralLoggingToS3 from '../deployments/central-services/central-logging-s3' ;
3232import * as vpcDeployment from '../deployments/vpc' ;
3333import * as transitGateway from '../deployments/transit-gateway' ;
3434import * as centralEndpoints from '../deployments/central-endpoints' ;
3535import { CfnResourceStackCleanupOutput } from '../deployments/cleanup/outputs' ;
3636import { VpcOutputFinder , VpcSubnetOutput } from '@aws-accelerator/common-outputs/src/vpc' ;
3737import { TransitGatewayAttachmentOutputFinder } from '@aws-accelerator/common-outputs/src/transit-gateway' ;
38+ import { IamRoleOutputFinder } from '@aws-accelerator/common-outputs/src/iam-role' ;
3839
3940export interface IamPolicyArtifactsOutput {
4041 bucketArn : string ;
@@ -82,7 +83,7 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
8283 throw new Error ( `Cannot find mandatory primary account ${ masterAccountKey } ` ) ;
8384 }
8485
85- const { acceleratorName, installerVersion } = context ;
86+ const { acceleratorName, installerVersion, defaultRegion , acceleratorExecutionRoleName } = context ;
8687 // Find the central bucket in the outputs
8788 const centralBucket = CentralBucketOutput . getBucket ( {
8889 accountStacks,
@@ -110,25 +111,29 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
110111 * @param sourceAccount : Source Account Key, Role will be created in this
111112 * @param accountKey : Target Account Key, Access will be provided to this account
112113 */
113- const createIamRoleForPCXAcceptence = (
114- roleName : string ,
115- sourceAccount : string ,
116- sourceVpcConfig : VpcConfig ,
117- targetAccount : string ,
118- ) => {
119- const accountStack = accountStacks . tryGetOrCreateAccountStack ( sourceAccount , sourceVpcConfig . region ) ;
114+ const createIamRoleForPCXAcceptence = ( roleName : string , sourceAccount : string ) => {
115+ const accountStack = accountStacks . tryGetOrCreateAccountStack ( sourceAccount , defaultRegion ) ;
120116 if ( ! accountStack ) {
121117 console . warn ( `Cannot find account stack ${ sourceAccount } ` ) ;
122118 return ;
123119 }
124- const existing = accountStack . node . tryFindChild ( roleName ) ;
120+ const existing = accountStack . node . tryFindChild ( 'PeeringRole' ) ;
125121 if ( existing ) {
126122 return ;
127123 }
124+ const targetAccounts = acceleratorConfig
125+ . getVpcConfigs ( )
126+ . filter ( rsv => PeeringConnectionConfig . is ( rsv . vpcConfig . pcx ) && rsv . vpcConfig . pcx . source === sourceAccount ) ;
127+ const targetAccountKeys = Array . from ( new Set ( targetAccounts . map ( rsv => rsv . accountKey ) ) ) ;
128128 const peeringRole = new iam . Role ( accountStack , 'PeeringRole' , {
129129 roleName,
130- assumedBy : new iam . ArnPrincipal (
131- `arn:aws:iam::${ getAccountId ( accounts , targetAccount ) } :role/${ context . acceleratorExecutionRoleName } ` ,
130+ assumedBy : new iam . CompositePrincipal (
131+ ...targetAccountKeys . map (
132+ targetAccountKey =>
133+ new iam . ArnPrincipal (
134+ `arn:aws:iam::${ getAccountId ( accounts , targetAccountKey ) } :role/${ acceleratorExecutionRoleName } ` ,
135+ ) ,
136+ ) ,
132137 ) ,
133138 } ) ;
134139
@@ -138,6 +143,8 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
138143 actions : [ 'ec2:AcceptVpcPeeringConnection' ] ,
139144 } ) ,
140145 ) ;
146+
147+ createIamRoleOutput ( accountStack , peeringRole , 'PeeringConnectionAcceptRole' ) ;
141148 } ;
142149
143150 // Auxiliary method to create a VPC in the account with given account key
@@ -246,9 +253,16 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
246253 console . warn ( `Cannot find PCX source VPC ${ pcxConfig [ 'source-vpc' ] } in account ${ pcxConfig . source } ` ) ;
247254 } else {
248255 // Create Accepter Role for Peering Connection **WITHOUT** random suffix
249- // TODO Region support
250- const roleName = createRoleName ( `VPC-PCX-${ pascalCase ( accountKey ) } To${ pascalCase ( pcxConfig . source ) } ` , 0 ) ;
251- createIamRoleForPCXAcceptence ( roleName , pcxConfig . source , sourceVpcConfig . vpcConfig , accountKey ) ;
256+ const pcxAcceptRole = IamRoleOutputFinder . tryFindOneByName ( {
257+ outputs,
258+ accountKey : pcxConfig . source ,
259+ roleKey : 'PeeringConnectionAcceptRole' ,
260+ } ) ;
261+ let roleName = createRoleName ( `VPC-PCX-${ pascalCase ( accountKey ) } To${ pascalCase ( pcxConfig . source ) } ` , 0 ) ;
262+ if ( pcxAcceptRole ) {
263+ roleName = pcxAcceptRole . roleName ;
264+ }
265+ createIamRoleForPCXAcceptence ( roleName , pcxConfig . source ) ;
252266 }
253267 }
254268
0 commit comments