Skip to content

Commit b3337d3

Browse files
author
Taniya Mathur
committed
check existing service role
1 parent 181fd55 commit b3337d3

File tree

2 files changed

+112
-7
lines changed

2 files changed

+112
-7
lines changed

scripts/sdlc/idp-cli/poetry.lock

Lines changed: 23 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/sdlc/idp-cli/src/idp_cli/service/install_service.py

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ def deploy_service_role(self):
247247
service_role_template = 'iam-roles/cloudformation-management/IDP-Cloudformation-Service-Role.yaml'
248248

249249
try:
250+
# Check if the service role is managed by a different stack
251+
existing_stack_name = self._find_managing_stack()
252+
if existing_stack_name and existing_stack_name != service_role_stack_name:
253+
logger.info(f"Service role managed by different stack: {existing_stack_name}. Using existing stack for deployment.")
254+
service_role_stack_name = existing_stack_name
255+
250256
# Verify template file exists
251257
template_path = os.path.join(self.abs_cwd, service_role_template)
252258
if not os.path.exists(template_path):
@@ -278,8 +284,8 @@ def deploy_service_role(self):
278284
if process.stderr:
279285
logger.debug(f"Service role deploy stderr: {process.stderr}")
280286

281-
# Get the service role ARN from stack outputs
282-
service_role_arn = self.get_service_role_arn()
287+
# Get the service role ARN - use the actual deployed stack name
288+
service_role_arn = self._get_service_role_arn_from_stack(service_role_stack_name)
283289
if service_role_arn:
284290
logger.info(f"Successfully deployed service role: {service_role_arn}")
285291
return service_role_arn
@@ -297,14 +303,92 @@ def deploy_service_role(self):
297303
if e.stderr:
298304
logger.debug(f"Command stderr: {e.stderr}")
299305

300-
# Cleanup failed service role deployment
301-
logger.info("Cleaning up failed service role deployment...")
302-
self.cleanup_failed_stack(service_role_stack_name)
306+
logger.info(f"Service role deployment failed. Stack '{service_role_stack_name}' left for debugging.")
303307
return None
304308
except Exception as e:
305309
logger.error(f"Unexpected error during service role deployment: {e}")
306310
return None
307311

312+
def _find_managing_stack(self):
313+
"""
314+
Find which CloudFormation stack manages the IDPAcceleratorCloudFormationServiceRole.
315+
316+
Returns:
317+
str: Stack name that manages the service role, or None if not found
318+
"""
319+
try:
320+
# List all stacks that might contain the service role
321+
list_stacks_cmd = [
322+
'aws', 'cloudformation', 'list-stacks',
323+
'--region', self.region,
324+
'--stack-status-filter', 'CREATE_COMPLETE', 'UPDATE_COMPLETE',
325+
'--query', 'StackSummaries[?contains(StackName, `cloudformation-service-role`)].StackName',
326+
'--output', 'text'
327+
]
328+
329+
process = subprocess.run(
330+
list_stacks_cmd,
331+
check=True,
332+
text=True,
333+
stdout=subprocess.PIPE,
334+
stderr=subprocess.PIPE
335+
)
336+
337+
stack_names = process.stdout.strip().split() if process.stdout.strip() else []
338+
339+
# Check each stack to see if it has the ServiceRoleArn output
340+
for stack_name in stack_names:
341+
try:
342+
service_role_arn = self._get_service_role_arn_from_stack(stack_name)
343+
if service_role_arn:
344+
logger.debug(f"Found service role managed by stack: {stack_name}")
345+
return stack_name
346+
347+
except Exception:
348+
continue
349+
350+
return None
351+
352+
except Exception as e:
353+
logger.error(f"Error finding managing stack: {e}")
354+
return None
355+
356+
def _get_service_role_arn_from_stack(self, stack_name):
357+
"""
358+
Get service role ARN from a specific stack.
359+
360+
Returns:
361+
str: The ARN of the service role, or None if not found
362+
"""
363+
try:
364+
describe_cmd = [
365+
'aws', 'cloudformation', 'describe-stacks',
366+
'--region', self.region,
367+
'--stack-name', stack_name,
368+
'--query', 'Stacks[0].Outputs[?OutputKey==`ServiceRoleArn`].OutputValue',
369+
'--output', 'text'
370+
]
371+
372+
process = subprocess.run(
373+
describe_cmd,
374+
check=True,
375+
text=True,
376+
stdout=subprocess.PIPE,
377+
stderr=subprocess.PIPE
378+
)
379+
380+
service_role_arn = process.stdout.strip()
381+
if service_role_arn and service_role_arn != "None":
382+
return service_role_arn
383+
else:
384+
return None
385+
386+
except subprocess.CalledProcessError:
387+
return None
388+
except Exception as e:
389+
logger.error(f"Error getting service role ARN from stack {stack_name}: {e}")
390+
return None
391+
308392
def create_permission_boundary_policy(self):
309393
"""Create an 'allow everything' permission boundary policy if it doesn't exist"""
310394

0 commit comments

Comments
 (0)