Am2eJdv5VJFGEgA7YLmqVerify whether the CloudFormation stack has been created
Verify whether the CloudFormation stack has been created
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
Check if the specified CloudFormation stack is already present to avoid redundancy, ensuring that resources are managed efficiently without duplication.
inputs
outputs
import boto3
import time
from botocore.exceptions import ClientError, EndpointConnectionError, NoCredentialsError
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
def check_stack_creation_status(stack_name, region='us-east-1'):
"""
Polls the CloudFormation stack status until it is in a completed state or failed.
:param stack_name: The name of the CloudFormation stack to check.
:param region: AWS region where the stack is located. Default is 'us-east-1'.
:return: None
"""
try:
cf_client = boto3.client('cloudformation', aws_access_key_id=access_key,aws_secret_access_key=secret_key,region_name=region)
except NoCredentialsError:
print("Error: No valid AWS credentials found.")
return
except EndpointConnectionError:
print(f"Error: Unable to connect to the CloudFormation service endpoint in {region}.")
return
while True:
try:
response = cf_client.describe_stacks(StackName=stack_name)
stack_status = response['Stacks'][0]['StackStatus']
print(f"Current status of the stack '{stack_name}': {stack_status}")
if stack_status == 'CREATE_COMPLETE':
print(f"Stack {stack_name} has been created successfully.")
break
elif 'FAILED' in stack_status or 'ROLLBACK' in stack_status or stack_status == 'DELETE_COMPLETE':
print(f"Stack {stack_name} creation failed with status: {stack_status}")
break
except ClientError as e:
if e.response['Error']['Code'] == 'ValidationError':
print("Error: The specified stack name does not exist.")
else:
print(f"Failed to get stack status: {e.response['Error']['Message']}")
break
except Exception as e:
print(f"An unexpected error occurred: {e}")
break
time.sleep(10) # Wait for 10 seconds before checking the stack status again
#stack_name = 'MyCURReportAthenaStack' # Replace with your stack name
#region_name = "us-east-1" # Specify the AWS region
check_stack_creation_status(stack_name, region_name)
copied