dpzc/lab4/aws/boto3_helper.py

40 lines
1.3 KiB
Python
Raw Normal View History

2023-01-10 21:44:53 +01:00
import os
from urllib import response
import boto3
import pprint
from dotenv import load_dotenv
load_dotenv(dotenv_path="lab4/.env")
def get_aws_keys():
return os.getenv('aws_access_key_id'), os.getenv('aws_secret_access_key')
def init_aws_session():
aws_access_key_id, aws_secret_access_key = get_aws_keys()
return boto3.Session(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, aws_session_token=os.getenv("aws_session_token"), region_name=os.getenv('AWS_REGION'))
def ec2_get_vpc_list():
session = init_aws_session()
ec2 = session.client('ec2')
response = ec2.describe_vpcs()
return response['Vpcs']
def elb_create_target_group(target_group_name, vpc_id):
session = init_aws_session()
elb = session.client('elbv2')
response = elb.create_target_group(Name=target_group_name, Protocol='HTTP', Port=80, VpcId=vpc_id)
return response
def ec2_get_subnet_list():
session = init_aws_session()
ec2 = session.client('ec2')
response = ec2.describe_subnets()
return response['Subnets']
def ec2_get_security_group_list():
session = init_aws_session()
ec2 = session.client('ec2')
response = ec2.describe_security_groups()
return response['SecurityGroups']
# src: https://unbiased-coder.com/boto3-load-balancer-guide/