When trying to find out about the different AWS services and how to use them, I often find myself having to pull different pieces of information from different sources and cobbling them together to run the service correctly. It was no different when I decided to find out more about the AWS Command Line Tools (CLI). In this blog post, we explain how to configure the AWS CLI and launch an EC2 Instance.
What is the AWS CLI?
AWS CLI is a unified tool for running and managing your various AWS services. Just download and install the tool and you will be able to control multiple AWS services from the command line. Obviously, this can also be done from the AWS management console, but for developers who like the command line in either Linux or Windows this is a great tool for managing your AWS services.
Most AWS instances also have command line tools installed as standard so you could also manage your infrastructure that way if you wish however for this blog I installed it on a local host machine running windows 10. The installation process is fairly straight forward and I will go through that process as well as how to carry out a simple task like spinning up one of the AWS AMIs. The first thing that is required is that the host machine has Python installed on it. If you do not have Python on your machine you can get it here.
If you have Python installed, then you can skip this paragraph. If not, go to the Python home page and find the location of the downloads. Once there find the .exe download. There are a few options available so just choose the one that corresponds to your OS type either 32 bit or 64 bit. Once you have selected the link and began downloading wait a few seconds until the download is complete which should not take too long depending on your connection. Run the .exe file when downloaded, at some stage the download window will prompt you, once this happens, at the bottom of the prompt screen tick the box that automatically adds python directories to the system path. Figure 1 below shows the box “Add Python 3.5 to PATH” selected.
Figure 1 below shows the box “Add Python 3.5 to PATH” selected.
Once installation is complete open the command prompt in windows run the command:
python -m install -u pip
Which will either update the pip installer or update to the latest version of pip. Once that is complete run the following command:
pip install awscli
This will now install the AWS CLI tools package for you.
Creating a simple AWS AMI
In this section, I am going to discuss how to carry out a simple task like creating a simple AWS AMI with the previously installed command line tools. The first thing required is that you configure your machine for use with AWS CLI. To do this we must first get the security credentials for our AWS account. Log in to your AWS account and click on the drop-down menu at the top right of the screen usually where your name is and select security credentials and then access keys to get your credentials. Once you have them copy them into a text file and open the command line and type the command AWS configure. When you run this command it will prompt you to add your credentials and once you have done this you should have access to your AWS services.
1. Creating a Security Group
The first thing I am going to do is create a security group and I order to do this we just need to run a simple command to do so.
aws ec2 create-security-group --group-name GOB --description “Arrested Development.seal group”
These commands are just telling AWS to create a security group with the name GOB and a description of “Arrested Development.seal group” but you can choose to give it any name you wish. Once this command has finishes you can go to your AWS management console to view the security group you created.
2. Create a Key Pair
In order to access the instances, we create we must have a key pair in order to do this. First of all, go to your AWS management console and check which region you are currently using and take note of it. My current region is us-west-2. You could also use the command line to do this if you wish by typing the command:
aws configure get region
Which should return your current region. One you know your region, you should be ready to create your key pair. The commands to do this is as follows:
aws ec2 create-key-pair --<your region here> –key-name awsclidemokey --query “KeyMaterial” --output text awsclidemokey.ppk.
You can now go to your AWS management console and download your key pair. Once your key pair is downloaded you can parse the contents to the screen using the command:
3. Enable SSH for Security Group.
At this point, we must enable SSH for our security group so we need some extra information about our security group. Go to your AWS console and get the group id. Once we have the group id we can run the following command:
aws ec2 authorize-security-group-ingress --group-id <your group id here> --protocol tcp --port 22 --cidr 0.0.0.0/0 --region <your region here>
Before we can create our instance we must first check if we have a VPC set up. If so make a note of the VPC id, you can get this from the AWS management console. If not, then you can set one up in the console itself. Also, we must get the subnet id associated without VPC. To do this go to the AWS management console and select VPC and then subnets, then find the subnet associated with your chosen VPC and make a note of the subnet id.
4. Creating an Instance.
In order to run an ec2 instance there are a few things we must get from the management console like the image id and the image type. Once you have retrieved these we are ready to start creating our instance.
Run the following command:
aws ec2 run-instances --image-id ami-9abea4fb --count 1 --instance type t2. micro --key-name awsclidemokey --security-group-ids <your security group id here> --subnet-id <your subnet id here> --region <your region here>
The count is just telling AWS how many instances you wish to create.
You can now go to the management console to check that your instance was created. Once your instance is up and running, you can access it via putty using your .ppk file created earlier.
5. Terminate Instances.
In order to terminate any instances that we have created just run the following command:
aws ec2 terminate-instances --instance-ids <your instance id here> --region <your region here>
6. Delete Security Groups.
In order to delete any security groups that we have created just run the following command:
aws ec2 delete-security-group --group-id <your group id here> --region <your region here>
What do we think?
This may seem like a convoluted way to carry out a task that is easily done from the AWS Management Console. However, if you have many custom instance AMIs that you wish to spin up as and when needed you could put the commands in a simple script and just run the script. It will always depend on what your preferences/needs are. However, for quick automation and management of services with AWS, the AWS CLI could prove to be a very useful for developers.
CloudRanger offer a simple DevOps automation solution to make it easy to manage backups & servers on AWS cloud. Using CloudRanger, you can easily backup your RDS and EC2 instances with snapshots and AMIs.
With an easy to use interface, managing your routine AWS tasks is simple and effective. CloudRanger saves your team time and hassle, making the day-to-day management of your AWS resources easier and more automated. CloudRanger can also help you save on your EC2 costs by starting/stopping non-production instances automatically when you need them.
Start a 14-Day Free Trial, so you can try out our range of AWS features and benefits for yourselves.