Last active
January 23, 2020 15:17
-
-
Save eduzen/7f86873cb19b149da329d46a1c4d6943 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
from configparser import ConfigParser | |
from pathlib import Path | |
def _get_profiles(): | |
credentials_file = Path.home() / ".aws" / "credentials" | |
config = ConfigParser() | |
config.read(credentials_file) | |
return config.sections() | |
def show_profiles(profiles): | |
for position, profile in enumerate(profiles): | |
print(f"{position+1}) {profile}") | |
def select_profile(): | |
profiles = _get_profiles() | |
show_profiles(profiles) | |
profile = input( | |
"Please enter the number. If you skip default option is 'teleclinic-alpha'>>> " | |
) | |
if not profile: | |
return "teleclinic-alpha" | |
try: | |
profile = profiles[int(profile) - 1] | |
except (ValueError, TypeError, IndexError): | |
print("\nOnly numbers in the list...") | |
select_profile() | |
print(f"\n {profile} profile selected!") | |
return profile | |
def main(): | |
profile = select_profile() | |
session = boto3.session.Session(profile_name=profile) | |
ecs = session.client("ecs") | |
ec2 = session.client("ec2") | |
list_of_tasks = ecs.list_tasks(cluster="core-cluster", family="api") | |
describe_tasks = ecs.describe_tasks( | |
cluster="core-cluster", tasks=list_of_tasks["taskArns"] | |
) | |
instance = describe_tasks["tasks"][0]["containerInstanceArn"] | |
container = ecs.describe_container_instances( | |
cluster="core-cluster", containerInstances=[instance] | |
) | |
ec2_id = container["containerInstances"][0]["ec2InstanceId"] | |
ec2_instances = ec2.describe_instances( | |
Filters=[{"Name": "instance-id", "Values": [ec2_id]}] | |
) | |
container_ip = ec2_instances["Reservations"][0]["Instances"][0][ | |
"PrivateIpAddress" | |
] | |
print(f"ssh -i .ssh/alpha.private_key ec2-user@{container_ip}") | |
if __name__ == "__main__": | |
try: | |
main() | |
print( | |
'docker exec -ti $(docker ps -aqf "publish=8080") bash' | |
) | |
except: | |
print("Bye!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment