Hands-On: Run an Application Through the AWS CLI
Explore how to launch, manage, and terminate WordPress EC2 instances using the AWS CLI. Understand using AMIs and instance types, adding tags for resource management, filtering instances, and cleaning up resources efficiently. This lesson helps you gain hands-on experience replacing AWS console actions with CLI commands.
Let’s recap what we did in the last chapter when creating a WordPress EC2 instance through the AWS console.
- Find a suitable WordPress AMI on the AWS Marketplace.
- Launch an EC2 instance with this AMI.
- Connect to it through its public IP or public DNS.
Now, we will recreate these steps through the AWS CLI instead of using the AWS Console.
Decode the AWS CLI command to create an EC2 instance
Let’s take a look at the command to run an EC2 instance from the CLI:
By the way, we can format commands over multiple lines by using the backslash (\) to split up the command:
This should be pretty straightforward because this command only has three lines:
- The
aws ec2 run-instancesstatement containing theec2command and therun-instancessubcommand. - The
-image-idparameter to specify the AMI image (essentially containing the operating system and software to run). - The
--instance-typeparameter to specify the instance type. The instance type refers to the specifications of the virtual server. Here, we’ll uset2.micro.
We’ll go into more detail regarding AMIs and instance types later in this course when taking a deeper look at EC2.
These are the only required parameters to run an EC2 instance, but let’s add another one to distinguish our EC2 instance from others running in our AWS account.
A best practice on AWS is to add tags (like labels) to all resources we create so we can identify them more easily.
The parameter to add tags is --tag-specifications, and it looks like this:
It looks a bit complicated because we need to assign tags to both the EC2 instance itself as well as its volumes. The tag we are assigning is team=wordpress, and it will be our go-to tag to label all our resources.
Again, we’ll go into the details about tags later in this course, but for now, let’s just get an instance running.
The command now looks like this:
Find the AMI ID of the Bitnami WordPress image
But what is this AMI image-id actually? How can we find out the right image-id for a WordPress image?
Remember the AWS Marketplace page for the WordPress image we used in the “Getting Started: Setup and First Steps” chapter? Let’s go back to the AWS Marketplace page, so please open the AWS Console.
The steps to get there are:
- Type in “WordPress” into the search bar.
- Click “Marketplace” on the left, and click on the first entry, “WordPress Certified by Bitnami and Automattic.” We should then see the “Product Overview” page.
- Click “Continue to Subscribe” and “Continue to Configuration.”
We should already be subscribed to the image and now see this page:
On the bottom, we can see the “Ami Id” and “Ami Alias.” However, before we copy it, we first need to change the region to US East (Ohio) because this will correspond to our default region, “us-east-2.”
Notice how the “Ami Id” changed? This is because AMIs are per region, so we need to use the right image for the right region. This can be a bit tedious, but luckily AWS introduced a new feature called the Ami Alias. This alias doesn’t change when we change the region, so we can use this instead without having to worry about the region we’re in.
To use an Ami Alias with the CLI, we need to add resolve:ssm: in front of the alias to tell the CLI that this is in fact an AMI alias and not an AMI ID and that it should resolve the AMI alias to the correct AMI ID for the current region.
Now, to use it in our command, it should look like this:
Note: The Ami Alias in the command above might be different to what you will see in the AWS console. The WordPress image is regularly updated, and the latest version might not correspond to the version used in this course.
Run the command
Please run the command above in the terminal below. You can use this terminal for the following commands as well.
You should get an output similar to this:
Type q to quit the output screen.
Did it work?
Well, let’s check. Can you guess which command you can use for it? You have seen it before.
Did you guess right? It is
We could run it without any parameters, but then it would list all instances in our account. If you started from a new account that shouldn’t be a problem. If not, we’ll use tags.
We assigned the tags earlier in this lesson to be able to filter EC2 instances with them, and now is the time to do so. To filter the output of an AWS CLI command, we can always use the --filter parameter. To filter by our team=wordpress tag, we can use the following command:
Type or copy the command above into the terminal above. The output should look like this:
That looks different than before! As we can see, the instances section now has an entry!
Now, if we go back to the terminal, we can filter the output again a little bit to see only the InstanceId of the instance.
The parameters do the following:
--queryfilters the output of the command to only show theInstanceId. In the next lesson, we will learn how this--queryparameter works.--output textreformats the output as text only (instead of JSON).
Please run the command in the terminal above. The output should be like this:
If we go back to our AWS Console and type “EC2” into the search field, we should see the instance as well.
We can use this instance link to go there.
As we can see, this shows the same InstanceId as in the terminal.
Congratulations! You successfully started an EC2 instance using the AWS CLI!
Destroy the created instance
After creating an instance, it’s time to clean up again.
Using the ec2 describe-instances command from above, we can get the InstanceId of the instance we want to terminate. We can pass this ID to the terminate-instances command using a nested command. We’ll learn how nested commands work in a later lesson. For now, we just need to know that it will insert the InstanceId into the terminate-instances command.
The command then looks like this:
Please run it in the terminal below:
We should see the following output:
Great, this worked!
Now, wait a few minutes and run the command below in the terminal above to double-check. This command will check the state of the instance. We’ll learn the details of the query command in the next lesson.
We should see something like this:
This confirms that the instance was terminated and everything is cleaned up.