def create_client_with_profile(profile_name, region, resource_name='ec2'):
""" Create a new boto3 client with a boto3 profile in ~/.aws/credentials
Args:
profile_name (str): The name of the profile that you have set in your
~/.aws/credentials profile.
region (str): The aws region you want to connect to.
resource_name (str): Valid aws resource.
default=ec2
Basic Usage:
>>> client, err_msg = create_client_with_profile('lab01', 'us-west-2')
Returns:
Tuple (botocore.client.EC2, str)
"""
client = None
err_msg = ''
try:
session = (
boto3.session.Session(
profile_name=profile_name, region_name=region
)
)
client = session.client(resource_name)
except Exception as e:
err_msg = str(e)
return client, err_msg
评论列表
文章目录