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

Install Docker on Ubuntu 18.04.1

This is a tutorial on how to install Docker on Ubuntu 18.04. [Works on Ubuntu 16.04 / 17.04 / 18.04.1 ]

Before we install docker, verify if you had installed any older versions of docker. If so, we need to remove them.

Run the following to remove any old versions of docker:

sudo apt-get remove docker docker-engine docker.io containerd runc -y

Install Prerequisites:

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y

Install docker using convenience script

curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh

Add current user to docker group

sudo usermod -aG docker $USER

Enable Docker service on boot:

To enable docker service to run on boot, run the following:

sudo systemctl enable docker

Now, we need to exit out of the terminal and open a new terminal so that we can start using docker. Else, you may get a permission error. [You could either type “exit” and re-login to your ubuntu server or open a new terminal session].

After re-logging in, we can start using docker.

To start with, you can check the version of docker installed on your system.

docker version

Here is a screenshot of the outputs for reference:

docker version

When I got this, I’ll was like…

Now, lets run a  docker container, you can use the following:

docker run -it hello-world

You should ideally see docker downloads a docker image and provide you with a hello world message as shown below:

Just kidding, you should see something outputs as below:

Thats all for this tutorial. Do Leave a comment down below if you docker installed successfully.

Do bookmark my website/follow for future docker tutorials.

#PeaceOut

References/Sources:

Docker Install

Gify

How to improve WordPress load times

Step 1. Perform a speed test

Go to https://tools.pingdom.com and enter your website to test your current load time. This is to get a idea the load time and performance score for reference.

Step 2. Configure Cloudflare CDN for your wordpress

You can configure cloudflare CDN for your wordpress website for free which will reduce latency and hence improve page load speeds. Create an account at http://www.cloudflare.com and change your name servers to the ones provided by cloudflare. For a more detailed guide with screenshots on how to configure cloudflare CDN for your website, you can find it here. (Link opens in new tab)

Step 3. Optimize your images

If your wordpress blog has a ton of images, then its best you optimize them so that your blog would load faster. You can either use plugins such as ‘WP Smush‘ (Plugin Link) or “Compress JPEG & PNG images” (Plugin Link) to optimize images. You can also manually optimize it and upload them if your an expert.

I also did notice that if the image are of .PNG format, then its best to use Tiny PNG (https://tinypng.comor use their plugin “Compress JPEG & PNG images” as it is more efficient than ‘WP Smush’.

Step 4. Remove Google Fonts

If your theme loads google fonts, you may find few requests going out to the google server which can slow down your site. Disabling it is easy and all you have to do is install plugin named ‘Remove Google Fonts References.

Step 5: Disable Gravatar Hoovercards

This step is only needed if you have installed and configured Jetpack for your wordpress blog.

Go to your wordpress dashboard, click on Settings>Discussion.  Uncheck the option “View people’s profiles when you mouse over their Gravatars” next to “Gravatar Hovercards”.

If ‘Gravator Hovercards’ section is greyed out, then click on “Shows Avatars”, uncheck “View people’s profiles when you mouse over their Gravatarsand then uncheck “Shows Gravatar” and save changes.

Step 6. Remove query strings from static resources

This have a huge impact on your performance score in pingdom and Gtmetrics. I have written an article on how to remove query strings from static resources to improve load times here. (Link opens in new tab)

Step 7. Remove unwanted plugins

Like the title suggests, if you don’t use a plugin its best to remove them.

Once done, re-run the speed tests https://tools.pingdom.com to compare before and after performance results.

Sources: Link

DIY Motion Detecting Surveillance Camera using Pi Zero W, Docker & USB Webcam

This is project build using “motion” which a software based motion detector. I have created a docker image for this project for portability reasons inside my Pi Zero W.

So what does this project do?

– If you need a surveillance system for your home you can build it yourself pretty cheap using a Raspberry PI and a webcam.

– Configure webcam it to stream and record footage full time or record only when a motion is detected.

– Moreover, this a fun project to run docker on  a Pi simply to test docker for Pi Zero.

– This could be run on Pi Zero W/Pi 3/Pi 3 Model B/Banna PI/Orange Pi as well.

What not to expect?

-Well, this is a Pi zero. So, don’t expect it to render 1080p at 60fps. 😛

Requirements:

  • Raspberry Pi Zero W connected to your local Wifi network [Raspberry Pi 3 is recommended.]
  • Docker 18.06.1-ce installed on Pi Zero.
  • USB webcam.
  • Sufficient disk space on your SD to store the footage.
  • Lots of patience 🙂

Setting up your Raspberry PI:

First, we need to create a folder where we need to store the footage. So, SSH into your Raspberry Pi.

Create a folder where you would like to store the footage. We will be using this folder later to map inside the docker container. Example, create a folder called “surv”.

mkdir ~/surv

Unplug any USB devices connected to your Pi, then type in the following:

dmesg -w

Plug in your USB webcam to your Raspberry PI Zero, and you should see that the webcam is connected. Below are some sample logs, that I got once I connected the usb webcam:

dmesg -w logs

Additionally, you could run the following to view connected video devices:

v4l2-ctl --list-devices
ls -l /dev/video0

view devices

You should ideally see the USB webcam in the dmesg logs and in the lsusb outputs.

– Pull the docker image:

docker pull techantidote/motion

To run the docker container, run the following:

docker run -dit --net=host --privileged -v /dev/video0:/dev/video0 -v ~/surv:/footage --name watchtower techantidote/motion motion -n

Here, we are passing the /dev/video0 which the camera attached to the rasbperry pi to /dev/video0.

To view the live stream, open a browser and go to:

http://ip-of-your-pi:8081

Alternative method:

If you are testing the configuration, you can try the following:

docker run -dit --net=host --privileged -v /dev/video0:/dev/video0 -v ~/surv:/footage --name watchtower techantidote/motion

Now from inside the container, you can make changes to your configuration files and then start the motion service manually using the following:

service motion start

You can make now changes on the fly and then restart the motion service:

service motion restart

[Note: You may have to restart the docker container “docker start watchtower”]

I have already added the configuration files inside the docker image itself. If you need to tweak the settings such as the resolution/frame rates, you can edit the below two configuration files inside the docker container:

/etc/motion/motion.conf
/etc/default/motion

The log files for motion detection are stored in /var/log/motion/logs. If you would to view the log files real time, you run the following command:

docker exec -it watchtower tail -f /var/log/motion/logs

motion detection docker logs

Conclusions:

-Running docker inside a Pi Zero for motion detection may not be the best idea.

-Flashing MotionEyeOS which is Video Surveillance OS designed to run on single board computers.

Want to contribute to this project?

If you would like to contribute to this project, feel free to reach out to me at twitter. @techantidote

Docker Image for Pi Zero W:

https://hub.docker.com/r/techantidote/motion

Credits/References:

Thanks to motion-project
Base image used for building the image => resin/rpi-raspbian:stretch
Accessing hardware device such as a camera from inside a container.

Check partition information in Linux

To show currently mounted partition in human readable format, use:

df -h

df -h sample output screenshot

Another way to check partition information using parted.

sudo parted /dev/sda print

parted sample output screenshot

Here, we can see that the disk size is 120GB along with the partition information.

To view the list of partitions using fdisk, use:

fdisk -l

To view the list of block devices:

lsblk

Hope this helps! Cheers 🙂

How to pass arguments to an alias in bash

Lets look at passing arguments to an alias by looking at an example.

The below command will open duckduckgo.com in a new tab in firefox.

firefox --new-window duckduckgo.com

Lets say, I want to create an alias but I want to pass the URL as a parameter to the alias.

For example, if I run the following command in the terminal, it should look at the first argument (which is the URL) and open it in a newtab in firefox.

ff duckduckgo.com

To achieve this, you can do the following:

Edit your ~/.bashrc or ~/.bash_profile using your favorite editor.

vim ~/.bashrc

Create a function inside the ~/.bashrc  with the following content. [Copy/paste the below inside your bashrc]

alias ff='function _ff()
{
firefox --new-window $1
};_ff'

Here, $1 is the first argument.

Once you save and close the bashrc file, you can source your bashrc for changes to take affect.

source ~/.bashrc

Now, if you enter the following command in the terminal, it will take “duckduckgo.com” which is the 1st parameter and open it in a new tab in firefox.

ff duckduckgo.com

Hope this helps. If you like my content, do share and subscribe for more content.

Source: StackOverFlow

A Simple Bash Script for deploying an EC2 instance in AWS

Below is a sample bash script that I created to deploy an EC2 instance in AWS.

What does this script do?

-Load the at the user defined values for VPC,Subnet, Security Policy.
-Use aws cli to interact with AWS with the IAM user configured.
-Create a new AWS key named “devenv-key” and store the corresponding key in your local machine.
-Deploy an t2.micro ubuntu EC2 instance.
-Upon deployment, it will wait for 60 seconds and SSH directly into the newly deployed ubuntu ec2 instance.

Prerequisites:

-An Amazon AWS account.(Free or Paid account)
-An IAM user with Access Key and secret access key.
Pre-configured VPC, Subnets, Routes, Internet gateways, Security policy.
-Any Linux Machine with aws cli utlity installed.
[Refer to my previous blog post on how to install AWS CLI tool.]
Link: https://techantidote.com/how-to-install-aws-cli-in-linux-auto-command-completion/

– In host machine, run “aws configure” to configure your IAM user details.

[ PS: I am not a expert in scripting. ]

Variables to be set before running the script:

-I have added the following variables in the script:

"vpc_id"
"sub_id"
"route_table"
"internet_gateway"
"sec_id"
"aws_image_id"
"i_type".

-Substitute values for these variables from that of your AWS environment in the script.

How do I run the script?

Option 1: Git clone and run it

-I have setup a public project in gitlab. You can clone and run the script (You would require git to clone the project.)

git clone https://gitlab.com/techantidote/aws-bash.git
cd aws-bash

-Edit the script getaws.sh and update values for vpc_id, sub_id and sec_id.
-Once done, run the script:

./getaws.sh

Option 2:
-Create a file (getaws.sh), make it executable, copy the below contents to this file and run the script.

Bash Script starts here:

#!/bin/bash
echo -e "\e[33m ========= AWS Automation Project =========\033[0m"

#AWS variables - Modify these as per your account
# Enter your VPC ID
vpc_id="vpc-12345"

# Enter your Subnet ID.
sub_id="subnet-12345"

#Enter your route table ID - Optional
#route_table="rtb-12345"

#Enter internet gateway - Optional
#internet_gateway="igw-12345"

# Enter your security group ID
sec_id="sg-12345"

# Enter the AWS Image ID you would like to deploy. The below image ID is for an Ubuntu EC2 instance.
aws_image_id="ami-41e9c52e"

#Set the type of instance you would like. Here, I am specifying a T2 micro instance.
i_type="t2.micro"

# Create an optional tag.
tag="Wakanda"

#Create the key name what you want
aws_key_name="devenv-key"
ssh_key="devenv-key.pem"

#Generate a random id - This is optional
uid=$RANDOM

# Generate AWS Keys and store in this local box
echo "Generating key Pairs"
aws ec2 create-key-pair --key-name devenv-key --query 'KeyMaterial' --output text 2>&1 | tee $ssh_key

#Set read only access for key
echo "Setting permissions"
chmod 400 $ssh_key

echo "Creating EC2 instance in AWS"
#echo "UID $uid"

ec2_id=$(aws ec2 run-instances --image-id $aws_image_id --count 1 --instance-type $i_type --key-name $aws_key_name --security-group-ids $sec_id --subnet-id $sub_id --associate-public-ip-address --tag-specifications 'ResourceType=instance,Tags=[{Key=WatchTower,Value="$tag"},{Key=AutomatedID,Value="$uid"}]' | grep InstanceId | cut -d":" -f2 | cut -d'"' -f2)

# Log date, time, random ID
date >> logs.txt
#pwd >> logs.txt
echo $ec2_id >> logs.txt
echo ""

echo "EC2 Instance ID: $ec2_id"
#echo "Unique ID: $uid"
elastic_ip=$(aws ec2 describe-instances --instance-ids $ec2_id --query 'Reservations[0].Instances[0].PublicIpAddress' | cut -d'"' -f2)
echo -e "Elastic IP: $elastic_ip"
echo $elastic_ip >> logs.txt
echo "=====" >> logs.txt

#echo "Copy paste the following command from this machine to SSH into the AWS EC2 instance"
#echo ""
#echo -e "\e[32m ssh -i $ssh_key [email protected]$elastic_ip\033[0m"
echo ""
countdown_timer=60
echo "Please wait while your instance is being powered on..We are trying to ssh into the EC2 instance"
echo "Copy/paste the below command to acess your EC2 instance via SSH from this machine. You may need this later"
echo ""
echo "\033[0;31m ssh -i $ssh_key [email protected]$elastic_ip\033[0m"

temp_cnt=${countdown_timer}
while [[ ${temp_cnt} -gt 0 ]];
do
printf "\rYou have %2d second(s) remaining to hit Ctrl+C to cancel that operation!" ${temp_cnt}
sleep 1
((temp_cnt--))
done
echo ""

ssh -i $ssh_key [email protected]$elastic_ip

 

Note | Disclaimer:

-I build this very basic script to learn about aws cli (Probably the hard way :P).

-There are way more easier ways to achieve the same result using templates.

-The intention of this script was to learn about AWS CLI and in the future implement methods to control to which VPC/Subnet/IG/Security policy that an EC2 instance needs to be attached on the fly.

-This is strictly to be run on test environments and not for production.

-Terminate your EC2 instance and its resources after testing so that you do not get charged. If you are using the AWS Free tier and within the trial period, you should be fine.

Do let me know your feedback (Good / Bad)in the comments section down below.

Happy Cloud computing 🙂

How to install AWS CLI in Linux + Auto command completion

This is a guide on how to install aws cli utlity in Linux along with screenshots.

Requirements:

– Linux.
– Python 2.6.5 or higher.

[Tested on Linux Mint with bash shell. should work on Ubuntu as well.]

Update your system and its packages:

sudo apt update && sudo apt upgrade -y

Install Pip:

sudo apt install python-pip -y
sudo pip install --upgrade pip

Install the following modules:

sudo pip install setuptools
sudo pip install wheel

Install AWS CLI:

sudo pip install awscli

To verify that the installation went well, you can run the following command.

aws --version

If the output shows the aws version, then you are all set.

Enable AWS commands Auto completion:

-To enable auto completion of sub commands, run the following to check where your “aws” and “aws_completer” are located.

which aws
which aws_completer

-Copy the output of “which aws_completer”. This would the path.

For example, if the output of “which aws_completer” was “/usr/local/bin/aws_completer”, then enter the following:

complete -C '/usr/local/bin/aws_completer' aws

Heres a screenshot reference:

setup aws awscli aws_completer setup for bash

Verify if AWS command auto completion works:

-Run the following command and press press TAB on your keyboard after typing “ec” and it should give you the possible options:

aws ec

aws awscli autocompletion installation on linux mint

Add path to your .bashrc or .bash_profile:

To make the changes persistant for aws command completion, you can add the following to your “.bashrc” or “.bash_profile.

echo "complete -C ‘/usr/local/bin/aws_completer’ aws" >> ~/.bashrc

Hope this helps! 🙂

Regards,
ΞXΤЯ3МΞ