Normally you can easily create an Amazon EC2 server through the AWS Console. This way is simple and easy, but often in large scale need to create a large number or repeat, you need to create scripts or write code to create quickly. AWS supports 02 methods for this need through AWS CLI or AWS SDK.
The AWS CLI helps you work with AWS services through the terminal itself from your computer or from an EC2 server on the AWS Cloud. This way, normally, you will be very familiar with Ops.
This article will show you how to use aws cli to create a simple ec2 instance in the AWS CLI series.
2 useful commands:
- aws ec2 describe-instances – lists all EC2 instances present in the previously configured Region.
- aws ec2 run-instances – create an ec2 . server
The following is the command to initialize an EC2 server from the AWS CLI
aws ec2 run-instances --image-id xxx --instance-type t2.micro --subnet-id yyy --security-group-ids zzz
- xxx is the AMI Image ID
- yyy is the Subnet ID in VPC
- zzz is the security group ID
Since Kevin is configuring the output to be YAML, you will see the output as a new EC2 server with the Instance-ID of i-039eb1fb0c0bfcfcd. This value is randomly generated by AWS.
Let’s test on AWS Console. Look at the 3rd line.
Ok, so I created a simple EC2 server using the aws cli. There are many other options you can flexibly adjust.
So what does the dry-run option when running the AWS CLI command mean?
With the option of adding the –dry-run parameter to the end of the command you can simulate running the emulator to see if it succeeds, but not actually create an EC2 server in this case.
aws ec2 run-instances --image-id xxx --instance-type t2.micro --subnet-id yyy --security-group-ids zzz --dry-run
Hope this article has had some fun using the AWS CLI to create an ec2 server. See you in the next posts.