def __init__(self, fn, service_name): self.fn = fn sess = Session() self.regions = list(sess.get_available_regions(service_name)) self.regions.extend( sess.get_available_regions(service_name, partition_name="aws-us-gov")) self.regions.extend( sess.get_available_regions(service_name, partition_name="aws-cn"))
def __init__(self, service_name, account_id, backend, use_boto3_regions, additional_regions): self.service_name = service_name self.account_id = account_id self.backend = backend self.regions = [] if use_boto3_regions: sess = Session() self.regions.extend(sess.get_available_regions(service_name)) self.regions.extend( sess.get_available_regions(service_name, partition_name="aws-us-gov")) self.regions.extend( sess.get_available_regions(service_name, partition_name="aws-cn")) self.regions.extend(additional_regions or [])
def get_regions(session: boto3.Session, user_regions: str) -> typing.List[str]: """ Get AWS regions to enable Detective from. Args: session: boto3 session. args_region: User specificied regions. Returns: A list of the region names to enable Detective from. """ detective_regions = [] if user_regions: detective_regions = user_regions.split(',') logging.info(f'Enabling members in these regions: {detective_regions}') else: detective_regions = session.get_available_regions('detective') logging.info( f'Enabling members in all available Detective regions {detective_regions}') return detective_regions
def get_aws_configuration_choice(): """ Allow user to select their AWS Connection configuration (profile/region) :return: AWS profile and region dictionary """ session = Session() profile = session.profile_name region = session.region_name message = "\nDo you want to use the default AWS profile [%s] and region [%s]?" % (profile, region) choice = click.confirm(message, default=True) schemas_available_regions_name = session.get_available_regions("schemas") if not choice: available_profiles = session.available_profiles profile = _get_aws_profile_choice(available_profiles) region = _get_aws_region_choice(schemas_available_regions_name, region) else: # session.profile_name will return 'default' if no profile is found, # but botocore itself will fail if you pass it in, when one is not configured profile = None return {"profile": profile, "region": region}
def main(): s = Session() # Returns list of regions where SES is available. regions = s.get_available_regions('ses') print("This is the list of available regions:") print(regions) # Source region. while True: src_region = input("Which region do you want to replicate from? ") if src_region in regions: # Create SES client for source region. ses_source_client = s.client('ses', region_name=src_region) region_email_identities = [] region_dom_identities = [] # Added pagination for listing SES identities. paginator = ses_source_client.get_paginator('list_identities') response_iterator = paginator.paginate(PaginationConfig={ 'MaxItems': 20, 'PageSize': 20 }) for entry in response_iterator: for element in entry['Identities']: if '@' in element: region_email_identities.append(element) else: region_dom_identities.append(element) print("Email addresses in source region:") print(region_email_identities) print("Domains in source region:") print(region_dom_identities) break else: print("Region entered invalid. ", "Please enter a region where SES is available.") # Destination region. while True: dst_region = input("Which region do you want to replicate to? ") if dst_region in regions: # Create SES client for destination region. ses_dest_client = s.client('ses', region_name=dst_region) email_call = email_verify(region_email_identities, ses_dest_client) verification_table = domain_verify(region_dom_identities, ses_dest_client) # Route53 subroutine. Based on user-input, used if # domains being verified are in Route53. r53dom = input("Are your domains hosted in Route53?(yes/no) ") if r53dom == 'yes': # Prints domain names and their verification tokens. print(verification_table) print("") r53_client = s.client('route53') add_route_53_record(table=verification_table, rec_type='domainVerify', r53=r53_client) elif r53dom == 'no': print("Use the verification tokens returned to ", "create TXT records through your DNS provider.") # Prints domain names and their verification tokens. print(verification_table) print("") # SNS topic addition sns = input( "Do you want to add SNS notifications for the identities? (yes/no) " ) if sns == 'yes': for addr in region_email_identities: sns_topics(addr, ses_dest_client) for dom in region_dom_identities: sns_topics(dom, ses_dest_client) elif sns == 'no': pass # DKIM verification dkim = input( "Do you want to configure DKIM for the identities? (yes/no) ") if dkim == 'yes': for addr in region_email_identities: generate_dkim(addr, ses_dest_client, r53dom, r53_client) for dom in region_dom_identities: generate_dkim(dom, ses_dest_client, r53dom, r53_client) break else: print("Region entered invalid. ", "Please enter a region where SES is available.")
"""Module to provide simple, consistent access to AWS Dynamo DB""" import boto3 from boto3 import Session from .constants import Constants _SESSION = Session() _REGIONS = _SESSION.get_available_regions(Constants.ddb()) class DynamoDB: """Provides connection method for AWS DynamoDB""" @staticmethod def _validate_region(region): """Validates the region input for DynamoDB""" if region in _REGIONS: _region = region else: raise ValueError('Region "%s" is invalid' % region) return _region def connect(self, region): """Returns a Dynamo DB Service Resource in a valid region""" return boto3.resource(Constants.ddb(), self._validate_region(region))
# We only care about a filter's Region, Resource Name, and Resource ID: resource_region = filters.get('Region') resource_id = [filters['ResourceId'] ] if filters.get('ResourceId') else None resource_name = filters.get('ResourceName') identifiers, new_token = \ RESOURCE_MAP[resource_type].list_config_service_resources(resource_id, resource_name, limit, next_token, resource_region=resource_region) result = { 'ResourceIdentifiers': [{ 'SourceAccountId': DEFAULT_ACCOUNT_ID, 'SourceRegion': identifier['region'], 'ResourceType': identifier['type'], 'ResourceId': identifier['id'], 'ResourceName': identifier['name'] } for identifier in identifiers] } if new_token: result['NextToken'] = new_token return result config_backends = {} boto3_session = Session() for region in boto3_session.get_available_regions('config'): config_backends[region] = ConfigBackend()
def stop_configuration_recorder(self, recorder_name): if not self.recorders.get(recorder_name): raise NoSuchConfigurationRecorderException(recorder_name) # Stop recording: self.recorders[recorder_name].status.stop() def delete_configuration_recorder(self, recorder_name): if not self.recorders.get(recorder_name): raise NoSuchConfigurationRecorderException(recorder_name) del self.recorders[recorder_name] def delete_delivery_channel(self, channel_name): if not self.delivery_channels.get(channel_name): raise NoSuchDeliveryChannelException(channel_name) # Check if a channel is recording -- if so, bad -- (there can only be 1 recorder): for recorder in self.recorders.values(): if recorder.status.recording: raise LastDeliveryChannelDeleteFailedException(channel_name) del self.delivery_channels[channel_name] config_backends = {} boto3_session = Session() for region in boto3_session.get_available_regions('config'): config_backends[region] = ConfigBackend()
def get_aws_regions(profile): session = Session(profile_name=profile) return session.get_available_regions('rds')
def _get_regions(self): s = Session() return s.get_available_regions("ec2")
def _get_regions(self): s = Session() return s.get_available_regions("ec2")
not_found.append(identifier) continue # Get the item: item = RESOURCE_MAP[resource_type].get_config_resource( resource_id, resource_name=resource_name, resource_region=resource_region, ) if not item: not_found.append(identifier) continue item["accountId"] = DEFAULT_ACCOUNT_ID # The 'tags' field is not included in aggregate results for some reason... item.pop("tags", None) found.append(item) return { "BaseConfigurationItems": found, "UnprocessedResourceIdentifiers": not_found, } config_backends = {} boto3_session = Session() for region in boto3_session.get_available_regions("config"): config_backends[region] = ConfigBackend()