? How to create an EC2 from CLI?

? How to create an EC2 from CLI?

Don't be scared of CLI, let's do it:

? First, let's see what we need first:

?an image id (can be taken from UI or if you use terraform, you can use the data resource type. Not the case now)

?the instance type (we will use the t2.micro

?key-name (you need to create it)

?security-group-id (we will use default one, it's only for learning)


  • Create the KeyPair

aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial'        

  • Get the security-group-id

aws ec2 describe-security-groups        


  • Create the instance (assuming you've used same naming like myself. AMI belongs to us-east-one, security-group-id is different in your case. You can take the values from the UI but I encourage you to use the CLI

aws ec2 run-instances  --image-id ami-0de02246788e4a354 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-0868ea57c075e6db1        
EC2 instance created

  • Let's attach some tags to the instance. (make sure to pay attention to the ec2 instance id. If you want to get the id from CLI you can use aws ec2 describe-instances

aws ec2 create-tags --resources i-0184fbaa8ba5aacdb --tags Key=Name,Value=devops-ec2        
Create tags for Ec2
See the tags in the AWS Console


要查看或添加评论,请登录

社区洞察

其他会员也浏览了