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!
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
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.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With