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'
)
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'
},
]
)
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']
)
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)
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.
Just file this. Keep my finger crossed.
https://forums.aws.amazon.com/thread.jspa?messageID=714975򮣟
a few years later the workaround works
i am facing same issue and i used your workaround thank you
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.