def manage_stack(profile, region, stack_name, template_body): try: if profile: session = boto3.Session(profile_name=profile, region_name=region if region else None) cloudformation_client = session.client("cloudformation") else: cloudformation_client = boto3.client( "cloudformation", config=Config(region_name=region if region else None) ) except ProfileNotFound as ex: raise CredentialsError( f"Error Setting Up Managed Stack Client: the provided AWS name profile '{profile}' is not found. " "please check the documentation for setting up a named profile: " "https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html" ) from ex except NoCredentialsError as ex: raise CredentialsError( "Error Setting Up Managed Stack Client: Unable to resolve credentials for the AWS SDK for Python client. " "Please see their documentation for options to pass in credentials: " "https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html" ) from ex except NoRegionError as ex: raise RegionError( "Error Setting Up Managed Stack Client: Unable to resolve a region. " "Please provide a region via the --region parameter or by the AWS_REGION environment variable." ) from ex return _create_or_get_stack(cloudformation_client, stack_name, template_body)
def _refresh_session(self): """ Update boto3's default session by creating a new session based on values set in the context. Some properties of the Boto3's session object are read-only. Therefore when Click parses new AWS session related properties (like region & profile), it will call this method to create a new session with latest values for these properties. """ try: boto3.setup_default_session(region_name=self._aws_region, profile_name=self._aws_profile) except botocore.exceptions.ProfileNotFound as ex: raise CredentialsError(str(ex))
def manage_stack(profile, region): try: session = boto3.Session(profile_name=profile if profile else None) cloudformation_client = session.client("cloudformation", config=Config(region_name=region if region else None)) except NoCredentialsError: raise CredentialsError( "Error Setting Up Managed Stack Client: Unable to resolve credentials for the AWS SDK for Python client. Please see their documentation for options to pass in credentials: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html" ) except NoRegionError: raise RegionError( "Error Setting Up Managed Stack Client: Unable to resolve a region. Please provide a region via the --region parameter or by the AWS_REGION environment variable." ) return _create_or_get_stack(cloudformation_client)
def _refresh_session(self): """ Update boto3's default session by creating a new session based on values set in the context. Some properties of the Boto3's session object are read-only. Therefore when Click parses new AWS session related properties (like region & profile), it will call this method to create a new session with latest values for these properties. """ try: botocore_session = botocore.session.get_session() boto3.setup_default_session(botocore_session=botocore_session, region_name=self._aws_region, profile_name=self._aws_profile) # get botocore session and setup caching for MFA based credentials botocore_session.get_component("credential_provider").get_provider( "assume-role").cache = credentials.JSONFileCache() except botocore.exceptions.ProfileNotFound as ex: raise CredentialsError(str(ex)) from ex