Translating the AWS Console to the Command Line

AWS provides a clean and efficient method for using the available services in a browser interface. This is fantastic when completing administration tasks by hand, but doesn't allow for any form of automation or scripting. The Command Line suite is made available for this purpose, but without the graphical interface it can be confusing to follow and difficult to translate. In this article, we look at taking some common actions from the graphical interface and applying them to the command, and then how Manageacloud can help with ensuring these commands produce expected results.

Amazon Web Services provides a grand number of services that provide a vast array of functionality many websites require to function, with a smooth and usable browser-based interface. This interface is clean, but doesn't provide the automation that would be required to deliver these websites at any form of scale. A Command Line utility is available for this purpose, and installable using pip within a standard python environment. The command installed is simply aws and will require some configuration, just run aws configure to set this up. You will need your AWS details for the command line access, available through the Identity & Access Management section of the console.

Automation has become a prominent topic in recent years, especially with the advent of cheaper hardware and volatile infrastructure. Having to maintain large sets of infrastructure that isn't completely known would be impossible without tooling to provide meta storage and association. Using a command line tool has been standard practice, though browser interfaces still provide a source of truth feeling to display configuration and general overview. The AWS Console is fantastic for this, displaying any deployed infrastructure within locations, breaking down types of infrastructure and allowing for the display of any meta detail associated.

To begin with, quite possibly the most common service used would be EC2. This is essentially a service providing virtual machines, an operating system and hardware profile being all that's needed to create. Of course, this basic VM wouldn't actually be doing anything once it was created. Within the console, it is trivial to create this. Just visit the EC2 link within the Compute section, then click Launch Instance and follow the prompts. Once automation is being taken into account however, this process can be further simplified and scripted using the command line, and doesn't require the several steps of button pushing to accomplish. A quick example of a command to create an instance is as follows.

aws ec2 run-instances --image-id ami-69631053 --count 1 --instance-type t2.micro --security-groups default --region ap-southeast-2

Using the aws command directly, the service is specified followed by the action or subcommand. In this case, we're working with EC2 and we wish to Run an Instance. More options are added, including the AMI and Security group, along with the Instance Type and Region. By default, the region is also set via the aws configure command that was set at the beginning, but can be overruled on a command basis.

Once this command has run, detail will be displayed about the instance, including the instance ID. This ID can then be used in further commands, such as adding a load balancer or destroying it. Within the Manageacloud environment, this ID is kept as a part of the loaded Infrastructure and allows for scripting capabilities and associations without any manual interaction.

When the instance is ready to be removed, it can be stopped with the following command.

aws ec2 stop-instances --instance-ids [instance-id] --region ap-southeast-2

In this command, [instance-id] is the returned identifier from the first command. The subcommand used here is stop-instances, which simply powers down the instance specifically. A similar command is terminate-instances that will accomplish the same task, but also destroy the instance once powered down. This can be extremely useful for dynamic infrastructrue deployments, but for any instance that will be useful in the future unnecessary. One good use-case for stop would be development environments. When working on a project, an instance can be started, worked on, and then stopped ready for the next time.

These are just 3 of the EC2 subcommands available, with many more for a wide variety of uses. To see a list of the available commands, just run aws ec2 help and scroll to Available Commands. For a list of the commands available within the aws application, run aws help and scroll to Available Services. Each service available within the console will have a counterpart within the command line interface. Each of these services use their abbreviated form to help with command condensation. Simple Storage Service is S3, Elastic Load Balancer is ELB, and so on. For each service, to see subcommands and other options, just run aws [service] help and scroll to the Available Commands header.

Furthering the topic of automation, Manageacloud makes re-use of commands and their scripting extremely simple. Check out the Quick start guide to see how infrastructure and instances can be defined and built, then automated for full flexibility and control. 

Written by Allan Shone on Thursday November 5, 2015
Permalink - Tags: aws, cli, devops, sysadmin

« Working safely with the AWS Command Line tool - Zero Downtime using Blue Green Deployments »