Filter AWS EC2 instances using tags with AWS CLI + Screenshots

This is tutorial on how to filter/search for AWS EC2 instances using AWS CLI by filtering with their tag and values.

For instructions on how to install AWS CLI in your machine, you can refer to on my previous blog posts. [Link ].

Below is a screenshot of an EC2 instance that two tags and their corresponding values.

aws ec2 instance tags screenshot

To search for instances using their tags and the values , you can use the following syntax:

aws ec2 describe-instances --filters Name=tag:Owner,Values=TechAntidote

Here, “Owner” is the key and the Value is “TechAntidote”. Here is a screnshot of the output:

aws cli ec2 searching with tags screenshot

You can search for other tags as well by providing the respective Key/value pairs.  [Note: The strings are case-sensitive.]

aws ec2 describe-instances --filters Name=tag:Name,Values=DevB0x

aws cli tag search results

Hope this helps. Cheers! ūüôā

Source/References: AWS CLI Command Reference

How to fix “mysqld dead but subsys locked” EC2

My webserver’s¬†mysqld service went down with¬†the error message “mysqld dead but subsys locked”. The wordpress website was displaying¬†the¬†error “error establishing a database connection”. This is how I fixed it.

Upon visiting my website, I got the error message as shown below.

error establishing database connection ec2

Verify Issue¬†–¬†Check mysqld status:

To check the status of the mysqld database service, connect to your Amazon EC2 instance and then type the following command.

sudo service mysqld status

mysqld dead but subsys locked ec2

Investigate log files:

I checked my mysqld log files and found that the mysqld service was malfunctioning due to low memory.

sudo cat /var/log/messages | grep mysqld | less

check mysqld events in /var/log/messages
To check further events of mysqld , you can check mysqld.log using the following command:

sudo cat /var/log/mysqld.log | less

In my case, I found the mysqld service was panicking and got shutdown due to memory problems. [Note: The above outputs would differ from mine.]

Check if swap is present or not:

Run the below command to check if you have swap on your EC2 instance.

free -m

check if swap is present in ec2 using 'free -m' command

You can see here that Swap is 0. [Note: Swap is by default not created in your Amazon EC2 micro instance (free tier).]

Solution:

To resolve this issue, we need to create a swap file, mount it and make it persistent on boot.

Create swap file (1GB):

Type the following commands to create and activate the swap.

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024

create swap file ec2

sudo mkswap /swapfile

mkswap command

Secure the swapfile:

To secure the swapfile from unauthorized access, type the following commands:

sudo chown root:root /swapfile

sudo chmod 600 /swapfile

chmod command to set swap file permission ec2

 

Finally, type the following command to load the swapfile and hit Enter.

sudo swapon /swapfile

Make swap persistent on boot:

To make the swap persistent on boot, edit the file /etc/fstab using the following command:

sudo vi /etc/fstab

Add the following at the end of the fstab file:

/swapfile swap swap defaults 0 0

Save and exit the file. Press ESC and then type :wq and hit Enter.

Check if swap is active:

You can run the command below to check if swap is active or not.

free -m

check if swap is active free -m

As you can see highlighted section, the swap of size 1023MB is activated. It would use the swap file whenever necessary.

Restart / Start mysqld service:

You can type the following command to start the mysql service.

sudo service mysqld start

service mysqld start

Voila! You should now be able to access your website.

Change swappiness (Optional):

This step is purely optional. I would like my EC2 to use the RAM majority of the time and use the swap file when necessary. As you may know, its faster to access data from in the RAM rather than switching to the swap file which is stored in a mechanical drive in cloud.

sudo sysctl vm.swappiness=10

change swappiness

And that’s it folks, this should resolve the problem with the mysqld service crashing in your EC2 instance.

If this article helped you, do leave a comment below and follow us on Twitter, Facebook and Tumbler. You can also subscribe to our email subscription list to get latest updates right to your email. Cheers!

References: isitablog