AWS CLI – Create Amazon EC2 Instance with AWS CLI

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.

AWS SDK is in a different direction but with the same purpose, AWS SDK helps developers to manipulate AWS Services through their code. Some popular languages ​​that can be supported are Python, Javascript, C#, Java, Ruby, Go, PHP…

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

Inside:

  • xxx is the AMI Image ID
  • yyy is the Subnet ID in VPC
  • zzz is the security group ID
aws cli ec2 command

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.

aws ec2 console new ec2 instance

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 
dry run option aws cli command

Hope this article has had some fun using the AWS CLI to create an ec2 server. See you in the next posts.

Happy Clouding!

[email protected]

Leave a Reply