Boto3: Extend ec2.create_subnet to assign SubnetName

Created on 13 Apr 2016  路  4Comments  路  Source: boto/boto3

Issues

Subnet is one of the most important part of VPC design. However, one cannot give Subnet a name to the subnet, for subsequent resource creation usage.

Current boto3 does not allow one to assign a name nor tag name during subnet creations.

response = client.create_subnet(
    DryRun=True|False,
    VpcId='string',
    CidrBlock='string',
    AvailabilityZone='string'
)

It is very useful if given a subnet name , e.g.

response = client.create_subnet(
    DryRun=True|False,
    SubnetName='string',
    VpcId='string',
    CidrBlock='string',
    AvailabilityZone='string'
)

### in fact , create_security_group make GroupName as compulsory
response = client.create_security_group(
    DryRun=True|False,
    GroupName='string',
    Description='string',
    VpcId='string'
)

Reasons

A subnet Name or a enforce tag name is very useful for admin to launch AWS services and attach to proper VPC Subnet and security group, using implicit naming than explicit ID.

For example , the current create_db_subnet_group() , one must enter explicit SubnetIDs to create a db_subnet_group.

response = client.create_db_subnet_group(
    DBSubnetGroupName='string',
    DBSubnetGroupDescription='string',
    SubnetIds=[
        'string',
    ],
    Tags=[
        {
            'Key': 'string',
            'Value': 'string'
        },
    ]
)

IMHO, it will make life easier extend to use subnet name if the features is extended.

response = client.create_db_subnet_group(
    DBSubnetGroupName='string',
    DBSubnetGroupDescription='string',
    SubnetIds=[
        'string',
    ],
    SubnetName=[
        'string',
    ],
    Tags=[
        {
            'Key': 'string',
            'Value': 'string'
        },
    ]
)

Proposed SubnetName features usage example

create_subnet(
    SubnetName='Web-subnet-us-1a',
    VpcId= "vpc-12345678",  
    CidrBlock='172.16.10.0/24',
    AvailabilityZone = "us-east-1a")

create_subnet(
    SubnetName='Web-subnet-us-1b',
    VpcId= "vpc-12345678",  
    CidrBlock='172.16.11.0/24',
    AvailabilityZone = "us-east-1b")

#### subsequent script don't need to quest the subnet-id, i.e. 
create_db_subnet_group(
  DBSubnetGroupName='web-mysql-sbgn',
  SubnetName=['Web-subnet-us-1a', 'Web-subnet-us-1b'] 
)

Current workaround

To insert subnet-id into boto3 /aws cli resource creation script, it is a tedious script workflow in boto3

improt boto3
ec2=boto3.client("ec2")
#1. get response ASAP from create_subnet 
subnet_response = ec2.create_subnet( ....) 

#2. Attach the tag 
tag_list= [{ 'Key' : 'Name',
    'Value' : 'Web-subnet-us-1a'}] 

ec2.create_tags(
  Resources=[subnet_response["Subnet"]["SubnetId"]],
  Tags= tag_list
)

#3. All resources using the subnet now may use describe_subnets() to find it

subnet_filter= [{'Name':'tag:Name', 'Values': [ "Web-subnet-us-1a" ]}]
required_subnet_id =  ec2.describe_subnet(Filters=subnet_filter) 

closing-soon guidance

Most helpful comment

I can see the usefulness of such a feature. Unfortunately, there is not much we can do from boto3's side because EC2's API would need to support the passing of a subnet name to operations like CreateSubnet: http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateSubnet.html.

I would recommend starting a conversation on the ec2 developer forums to see if they could add this API: https://forums.aws.amazon.com/forum.jspa?forumID=30 because if they add this to their API, then boto3 will automatically pick up the feature.

Let us know if you have any follow-up questions.

All 4 comments

I can see the usefulness of such a feature. Unfortunately, there is not much we can do from boto3's side because EC2's API would need to support the passing of a subnet name to operations like CreateSubnet: http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateSubnet.html.

I would recommend starting a conversation on the ec2 developer forums to see if they could add this API: https://forums.aws.amazon.com/forum.jspa?forumID=30 because if they add this to their API, then boto3 will automatically pick up the feature.

Let us know if you have any follow-up questions.

a few years later the workaround works

i am facing same issue and i used your workaround thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chesstrian picture chesstrian  路  3Comments

arijitArusan picture arijitArusan  路  3Comments

jparismorgan picture jparismorgan  路  3Comments

hwine picture hwine  路  3Comments

luistm picture luistm  路  3Comments