Question:
I am trying to streamline the process for creating VPC/EC2 environments without using the gui. I also want to automate it by telling a script what I want created with what properties.
I decided that the best place to start is to create a VPC and create an EC2 instance with in it.
I am using
1 2 |
aws ec2 create-vpc --cidr-block 10.0.0.0/16 |
But I wanted to name it something like myVPC. Is there a way to do things like this? I am very new to this so if you have any documentation regarding this please send it my way.
Thank you!
Answer:
If you really want a one liner:
1 2 |
aws ec2 create-vpc --cidr-block 10.0.0.0/16 --output text | awk '{print $NF}' | xargs aws ec2 create-tags --tags Key=Name,Value=MyVPC --resources |
It is a concatenation of two commands explained below.
Adding a tag while creating a VPC is not supported yet. Create a VPC like the following. The last value is VPC ID.
1 2 3 |
aws ec2 create-vpc --cidr-block 10.3.0.0/16 --output text VPC 10.3.0.0/16 dopt-a54153c7 default False pending vpc-f13d7295 |
Use
create-tags
to add a tag to the created VPC
1 2 |
aws ec2 create-tags --resources vpc-f13d7295 --tags Key=Name,Value=MyVPC |