def get_session()

in Onboarding/AWS/GrantAccessToEksClusters.py [0:0]


def get_session(profile: str, credentials: Dict[str, str]) -> boto3.Session:
    if profile == DEFAULT_PROFILE_NAME and not env_credentials_is_empty(credentials):
        default_region = credentials["DefaultRegion"] if credentials["DefaultRegion"] != "" else DEFAULT_REGION
        print("Starting session...")
        try:
            return boto3.Session(aws_access_key_id=credentials["AccessKeyId"], aws_secret_access_key=credentials["SecretAccessKey"], region_name=default_region)
        except Exception as ex:
            sys.exit(str(ex))

    try:
        print(f"Starting session for profile {profile}...")
        return boto3.Session(profile_name=profile, region_name=DEFAULT_REGION)
    except botocore.exceptions.ProfileNotFound:
        if profile == DEFAULT_PROFILE_NAME:
            sys.exit(f"You didn't configure a {DEFAULT_PROFILE_NAME} profile\n"
                     f"Either configure a default profile (see ReadMe) or set environment variables with the account credentials you wish to onboard.")
        sys.exit(f"The provided profile ({profile}) could not be found")
    except Exception as ex:
        print(DEFAULT_REGION)