def get_vpc_subnets(vpc_id: str, ec2: EC2Client) \
         -> List[SubnetTypeDef]:
     subnets = ec2.describe_subnets(Filters=[{
         'Name': 'vpc-id',
         'Values': [vpc_id]
     }])
     return subnets.get('Subnets', [])
 def get_vpc_route_tables(vpc_id: str, ec2: EC2Client) \
         -> List[RouteTableTypeDef]:
     rts = ec2.describe_route_tables(Filters=[{
         'Name': 'vpc-id',
         'Values': [vpc_id]
     }])
     return rts.get('RouteTables', [])
Exemple #3
0
def get_types_info(client: EC2Client) -> Dict[str, NodeTypeInfo]:
    instances = {
        i["InstanceType"]: i
        for page in client.get_paginator("describe_instance_types").paginate()
        for i in page["InstanceTypes"]
    }

    def is_cluster_group(d):
        if "PlacementGroupInfo" not in d:
            return False
        return "cluster" in d["PlacementGroupInfo"]["SupportedStrategies"]

    return {
        s: {
            "memory": d["MemoryInfo"]["SizeInMiB"]
            - int(math.pow(d["MemoryInfo"]["SizeInMiB"], 0.7) * 0.9 + 500),
            "cores_per_socket": d["VCpuInfo"].get(
                "DefaultCores", d["VCpuInfo"]["DefaultVCpus"]
            ),
            "threads_per_core": d["VCpuInfo"].get("DefaultThreadsPerCore", 1),
            "arch": d["ProcessorInfo"]["SupportedArchitectures"][0],
            "cluster_group": is_cluster_group(d),
        }
        for s, d in instances.items()
    }
 def get_transit_gateway_vpc_attachments(tgw_id: str, ec2: EC2Client) \
         -> List[TransitGatewayVpcAttachmentTypeDef]:
     atts = ec2.describe_transit_gateway_vpc_attachments(
         Filters=[{
             'Name': 'transit-gateway-id',
             'Values': [tgw_id]
         }])
     return atts.get('TransitGatewayVpcAttachments', [])
def update_route_tables(client: EC2Client, vpc_id: str, vgw: str, cidr: str,
                        log: logging.Logger) -> None:
    try:
        log.info(f"Getting all route tables in VPC {vpc_id}")
        route_tables = {
            table["RouteTableId"]: table["Routes"]
            for page in client.get_paginator("describe_route_tables").paginate(
                Filters=[{
                    'Name': 'vpc-id',
                    'Values': [vpc_id]
                }]) for table in page["RouteTables"]
        }
        for route_table_id, routes in route_tables.items():
            log.info(
                f"Will create route to {cidr} via {vgw} in route table {route_table_id} if not present"
            )
            if not route_is_present(cidr, routes, log):
                client.create_route(DestinationCidrBlock=cidr,
                                    RouteTableId=route_table_id,
                                    GatewayId=vgw)
    except Exception as e:
        log.exception(f"Error updating VPC {vpc_id}: {e}")
Exemple #6
0
    def all(cls, client: EC2Client, nodespace: dict):
        result = client.describe_instances(
            Filters=[
                {"Name": "tag:cluster", "Values": [nodespace["cluster_id"]]},
                {"Name": "tag:type", "Values": ["compute"]},
            ]
        )
        if result["Reservations"]:
            instances = result["Reservations"][0]["Instances"]
        else:
            instances = []

        return [cls.from_response(instance) for instance in instances]
def create_vpn(client: EC2Client, name: str, cgw: str, vgw: str, cidr: str,
               log: logging.Logger) -> None:
    try:
        vpn = client.create_vpn_connection(
            CustomerGatewayId=cgw,
            Type="ipsec.1",
            VpnGatewayId=vgw,
            DryRun=False,
            Options={
                "StaticRoutesOnly": True,
                "TunnelInsideIpVersion": "ipv4",
                "TunnelOptions": [tunel_opts, tunel_opts],
            },
        )
        vpn_id = vpn["VpnConnection"]["VpnConnectionId"]
        log.info(
            f"VPN ID {vpn_id} for {name} created, but no Name tag has been created"
        )
        client.create_vpn_connection_route(DestinationCidrBlock=cidr,
                                           VpnConnectionId=vpn_id)
        log.info(
            f"VPN connection route cidr {cidr} has been associated to {vpn_id}"
        )
        client.create_tags(
            DryRun=False,
            Resources=[
                vpn_id,
            ],
            Tags=[
                {
                    "Key": "Name",
                    "Value": name
                },
            ],
        )
        log.info(f"VPN ID {vpn_id} now has Tag Name {name} ")
    except Exception as e:
        log.exception(f"Error creating VPN {name}: {e}")
    def get_vpc_default_sg_id(vpc_id: str, ec2: EC2Client) -> Optional[str]:
        vpc_security_groups = ec2.describe_security_groups(
            Filters=[{
                'Name': 'vpc-id',
                'Values': [vpc_id]
            }, {
                'Name': 'group-name',
                'Values': ['default']
            }])
        # there is only one default
        for sg in vpc_security_groups.get('SecurityGroups', []):
            return sg['GroupId']

        return None
 def get_transit_gateways(ec2: EC2Client) -> List[TransitGatewayTypeDef]:
     tgws = ec2.describe_transit_gateways()
     return tgws.get('TransitGateways', [])
 def get_account_vpcs(ec2: EC2Client) -> List[VpcTypeDef]:
     vpcs = ec2.describe_vpcs()
     return vpcs.get('Vpcs', [])