Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EC2 CLI. Help creating VPCs with name

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

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!

like image 966
Bobby Baboon Avatar asked Aug 28 '17 23:08

Bobby Baboon


4 Answers

Generally, AWS resources don't have names. Instead they have IDs. What passes for a 'name' of Production is actually a tag with the key/value pair Name=Production.

To set a name tag for a VPC, use the CLI's ec2 create-tags command. For example:

aws ec2 create-tags --resources vpc-1a2b3c4d --tags Key=Name,Value=Production
like image 97
jarmod Avatar answered Oct 07 '22 16:10

jarmod


If you really want a one liner:

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.
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

aws ec2 create-tags --resources vpc-f13d7295 --tags Key=Name,Value=MyVPC
like image 37
helloV Avatar answered Oct 07 '22 15:10

helloV


You could use --tag-specifications:

aws ec2 create-vpc --cidr-block 10.0.0.0/24 --tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=MyVPC}]"

https://docs.aws.amazon.com/cli/latest/reference/ec2/create-vpc.html#options

like image 41
Gabriel Arellano Avatar answered Oct 07 '22 16:10

Gabriel Arellano


Unrelated to your specific question, but allow me to highly recommend AWS CloudFormation for managing these resources. It's a nicer method of definition that just the CLI, allows you to group resources or delete a stack. I use the CLI to call the Cloudformation, specifying a template.

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html

like image 35
ferrants Avatar answered Oct 07 '22 16:10

ferrants