How to Manage EC2 Instances from the CLI
Nowadays, it doesn't make sense to run your own servers unless you have a specific reason to do so. Cloud computing vendors have a lot to offer with their products. The total cost of ownership to run servers in the cloud can be much more affordable than purchasing and managing physical servers yourself.
So, it makes sense to run virtualized servers on a platform like AWS's EC2 service. EC2 only becomes more powerful when you understand how to autonomously deploy or remove your virtual infrastructure on the fly. With that said, let's look at how you would control EC2 with the command line.
What is EC2?
EC2 is a cloud computing product offered by Amazon's AWS services. Specifically, EC2 is a virtual machine service.
Traditionally businesses would purchase a piece of hardware, like a server, to run various applications on. At one point each physical server would only run a single OS with a bunch of different applications running within that OS. As information technologies advanced, though, it became a best practice to run virtualized servers on top of that physical server hardware.
By using a hypervisor (the software that runs virtualized servers) on a physical server, organizations can run multiple OSes, and therefore applications, on one piece of equipment. This has multiple benefits like:
Sandboxing operating systems and applications from each other
Increased security
Reduced total cost of ownership
Reduced maintenance
Using physical hardware still requires businesses to maintain that physical hardware, networking infrastructure, and internet connections, though. Operational costs may be reduced but are not eliminated.
EC2 takes this a step further. By moving the virtualized environment to the cloud, organizations don't need to maintain physical hardware, the network that powers that virtualized environment, or connectivity. Likewise, because of how EC2 works in Amazon's data centers, there's a much smaller chance a downtime issue will ever occur. The monthly cost of using EC2 can be much more affordable than purchasing and maintaining physical servers, too.
What is AWS-CLI?
The AWS CLI is a utility, or library, to interact with Amazon's AWS resources through various shell environments. The AWS CLI can be used with Linux, Mac OS, or Windows. Amazon also provides a Docker container for working with the AWS CLI as well.
If you are new to AWS, you might think that the AWS-CLI is a utility inside of the AWS dashboard. This can be a common confusion for new cloud engineers. Command-line utilities aren't typically referred to as a command-line interface themselves. In this case, the AWS-CLI is nothing more than an executable that runs inside of other shell environments like how PHP, NodeJS, or MySQL would operate.
The AWS-CLI can be used with the Windows Command Prompt, Windows PowerShell, Bash, Mac OS Terminal, and other shell environments. Each command is called with the 'aws' command followed by the service name (Eg. EC2) and then various flags to tell that command what to do.
More information about using the AWS-CLI can be found here.
How do I install the AWS CLI?
Before you can use the AWS-CLI, you need to install it first. Amazon offers AWS-CLI packages for Windows (Command Prompt and PowerShell), Linux (Bash, Zsh, tcsh), and Mac OS (Terminal). Amazon offers a Docker container for the AWS-CLI as well.
Though the Docker version of the AWS-CLI is portable and easy to use, you will need to pass the configuration and credentials to the Docker container hosting the AWS-CLI application before it can be used with your Amazon account. Because of this, it's typically just as easy to install the native version of the AWS-CLI for your computer's OS.
The AWS-CLI is also available in two versions (V1 and V2). V1 of the AWS-CLI has since been deprecated. Unless you have a specific reason to use AWS-CLI V1, Amazon recommends using V2. V1 of the AWS-CLI is only offered for legacy reasons.
More information about installing the AWS-CLI can be found here.
How to Use AWS-CLI to Create an EC2 instance
AWS resources, like EC2 instances or MediaConvert jobs, are controlled by submitting a JSON object with various parameters in it. These parameters tell AWS what to do. Using the AWS dashboard also uses these JSON objects, though those objects are created for you automatically.
Pro Tip: Whenever you are working with an AWS product in the AWS dashboard, you have the option to view the JSON object from that product's toolbar on the left side of the website. This can be handy if you need to see examples of how to create a JSON object file programmatically to use with the AWS-CLI or AWS SDK.
Before we can create an EC2 instance using the AWS-CLI, we need to create that JSON object, first. Thankfully, the AWS-CLI makes this easy. We can use this tool to create a skeleton of an instance and then edit it. To do that use the command below:
Aws ec2 run-instances —generate-cli-skeleton > inputFile.json
That will generate a JSON object file with tons of parameters in it for provisioning an EC2 instance. Let's open that file with your preferred text editor and get this JSON file ready to create a new EC2 instance. For the moment, we are going to remove all the parameters in that JSON file except for the lines below:
{
DryRun: true,
"ImageID":"",
"KeyName:""",
"SecurityGroups":[""],
"InstanceType":"",
"Monitoring": { "Enabled:true}
}
Let's explain each of these parameters.
ImageID | This is the ID of the AWS image you want to provision this EC2 instance with. This could be an image that you have already created, an image provided by the community, or an image provided by Amazon directly. For instance, if you wanted to use a bare Ubuntu 16 image from the US-East-1 data center, your imageID would be ami-0ee02acd56a52998e. |
KeyName | This is your EC2 key pair for your AWS account. If you have not created an EC2 key pair, reference this article for more information. |
SecurityGroups | This is your EC2 security group. AWS does provide default security groups that you can use. You will need to locate these or create a new security group in your AWS EC2 account. For more information about EC2 security groups, reference this page. |
InstanceType | This parameter is the instance type. EG. T2.micro, t2.nano, etc… |
When you are doing configuring your JSON object file, it should look like this:
{
DryRun: true,
"ImageID":" ami-0ee02acd56a52998e",
"KeyName:"CBTNuggets"",
"SecurityGroups":["SSHFFromBart"],
"InstanceType":"t2.micro",
"Monitoring": { "Enabled:true}
}
Now that we have our JSON object file ready to be used with the AWS-CLI, let's give it a try. Type in the command below:
Aws ec2 run-instances –cli-input-json file://inputFile.json
First, let's explain the two added command flags. The '—cli-input-json’ tells the AWS-CLI that we are going to pass a JSON object file to the application to use to provision an EC2 instance. The 'file://inputFile.json' flag tells the first command where to find that JSON object file.
After running that command above, you should receive an error message. By default, any command that doesn't complete returns an error message. That error message should state that everything would have worked properly except the DryRun parameter was used. Indeed, we left the DryRun parameter marked as true in our inputFile.json file.
Attempting to create a new EC2 instance with the AWS-CLI with that DryRun flag is a great way to ensure that your JSON object is correct before attempting to create a new instance. The last thing you want to do is provision a bunch of EC2 instances with errors.
So, if you received that error, edit the inputFile.json file again and mark the DryRun parameter as false. Then run the same command above again:
Aws ec2 run-instances –cli-input-json file://inputFile.json
If everything worked properly, the AWS-CLI should return the JSON object of the EC2 instance that was just created. If you look at the 'Name' key under the 'State' key, you should see 'pending'. Go ahead and check your AWS Dashboard for that new instance, too.
Why Should You Use AWS-CLI Qith EC2?
The AWS-CLI’s real power comes from its ability to be used with scripts. Scripts can be created to create new Ec2 instances, restart them, edit them, or terminate them. These scripts can be called programmatically as certain events happen. By doing this, you can automate your IT environment, reduce costs, and hopefully reduce your workload.
delivered to your inbox.
By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.