Search⌘ K
AI Features

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.

  1. Find a suitable WordPress AMI on the AWS Marketplace.
  2. Launch an EC2 instance with this AMI.
  3. 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:

Shell
aws ec2 run-instances --image-id ami-0abcdef1234567890 --instance-type t2.micro

By the way, we can format commands over multiple lines by using the backslash (\) to split up the command:

Shell
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t2.micro

This should be pretty straightforward because this command only has three lines:

  • The aws ec2 run-instances statement containing the ec2 command and the run-instances subcommand.
  • The -image-id parameter to specify the AMI image (essentially containing the operating system and software to run).
  • The --instance-type parameter to specify the instance type. The instance type refers to the specifications of the virtual server. Here, we’ll use t2.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:

Shell
--tag-specifications 'ResourceType=instance,Tags=[{Key=team,Value=wordpress}]' 'ResourceType=volume,Tags=[{Key=team,Value=wordpress}]'

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:

Shell
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t2.micro \
--tag-specifications 'ResourceType=instance,Tags=[{Key=team,Value=wordpress}]' 'ResourceType=volume,Tags=[{Key=team,Value=wordpress}]'

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:

  1. Type in “WordPress” into the search bar.
  2. 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.
  3. Click “Continue to Subscribe” and “Continue to Configuration.”

We should already be subscribed to the image and now see this page:

Notice that the image defaults to US East (N. Virginia); please change it to Ohio
Notice that the image defaults to US East (N. Virginia); please change it to Ohio

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.”

Different AMI ID for Ohio
Different AMI ID for Ohio

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:

Shell
aws ec2 run-instances \
--image-id resolve:ssm:/aws/service/marketplace/prod-tdzx6newyhgcc/5.9.2-18-r03-on-debian-10 \
--instance-type t2.micro \
--tag-specifications 'ResourceType=instance,Tags=[{Key=team,Value=wordpress}]' 'ResourceType=volume,Tags=[{Key=team,Value=wordpress}]'

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.

Terminal 1
Terminal
Loading...

You should get an output similar to this:

Javascript (babel-node)
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-0fd298c89a004ca7c",
"InstanceId": "i-0c8082f95b289cfe5",
"InstanceType": "t2.micro",
"LaunchTime": "2021-10-19T03:59:15+00:00",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "us-east-2b",
"GroupName": "",
"Tenancy": "default"
},
"PrivateDnsName": "ip-172-31-27-9.us-east-2.compute.internal",
"PrivateIpAddress": "172.31.27.9",
"ProductCodes": [],
"PublicDnsName": "",
"State": {
:

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

Shell
aws ec2 describe-instances

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:

Shell
aws ec2 describe-instances \
--filters "Name=tag:team,Values=wordpress"

Type or copy the command above into the terminal above. The output should look like this:

Javascript (babel-node)
{
"Reservations": [
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-0fd298c89a004ca7c",
"InstanceId": "i-0c8082f95b289cfe5",
"InstanceType": "t2.micro",
"LaunchTime": "2021-10-19T03:59:15+00:00",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "us-east-2b",
"GroupName": "",
"Tenancy": "default"
},
"PrivateDnsName": "ip-172-31-27-9.us-east-2.compute.internal",
"PrivateIpAddress": "172.31.27.9",
"ProductCodes": [
:

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.

Shell
aws ec2 describe-instances \
--filters "Name=tag:team,Values=wordpress" \
--query 'Reservations[*].Instances[*].InstanceId' \
--output text

The parameters do the following:

  • --query filters the output of the command to only show the InstanceId. In the next lesson, we will learn how this --query parameter works.
  • --output text reformats the output as text only (instead of JSON).

Please run the command in the terminal above. The output should be like this:

Shell
i-0c8082f95b289cfe5

If we go back to our AWS Console and type “EC2” into the search field, we should see the instance as well.

Go to the EC2 console
Go to the EC2 console

We can use this instance link to go there.

The new instance in the EC2 console
The new instance in the EC2 console

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:

Shell
aws ec2 terminate-instances \
--instance-ids $(aws ec2 describe-instances \
--filters "Name=tag:team,Values=wordpress" \
--query 'Reservations[*].Instances[*].InstanceId' \
--output text)

Please run it in the terminal below:

Terminal 1
Terminal
Loading...

We should see the following output:

Javascript (babel-node)
{
"TerminatingInstances": [
{
"CurrentState": {
"Code": 32,
"Name": "shutting-down"
},
"InstanceId": "i-0c8082f95b289cfe5",
"PreviousState": {
"Code": 16,
"Name": "running"
}
}
]
}

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.

Shell
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].State'`

We should see something like this:

Javascript (babel-node)
[
[
{
"Code": 48,
"Name": "terminated"
}
]
]

This confirms that the instance was terminated and everything is cleaned up.