Bash Script To Get AWS EC2 Tag Value For A Running Instance

Bash Script To Get AWS EC2 Tag Value For A Running Instance

Tags :

Category : How-to

Get Social!

Here is a Bash script for getting a tag value from within a running EC2 Instance.

For more information on AWS EC2, please see: https://aws.amazon.com/ec2/

If you’re using one of the standard AWS EC2 images, such as Ubuntu, then you’ll have everything you need already installed. Thankfully, Amazon installs some tooling on your host that’ll help you interact with the AWS fabric.

Create a bash file with your favourite text editor.

vi get-tag.sh

Paste the following script into the bash file. You’ll need to change test-tag in the below script to the tag Key name you’ve defined for the EC2 instance.

TAG_NAME=tag-name
INSTANCE_ID=$(ec2metadata --instance-id)
REGION=$(ec2metadata --availability-zone | sed 's/.$//')
TAG_VALUE=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=$TAG_NAME" --region=$REGION --output=text | cut -f5)

The value of the tag is now available to use in the variable $TAG_VALUE. Add an echo to the end of your script for now to see it in action.

echo $TAG_VALUE

Make the file executable and run it to see the output.

chmod +x get-tag.sh
./get-tag.sh

Tag Value!

Docker images Filter Options

Get Social!

The below is an excerpt from docker.com listing the –filter options available for docker images.

danglingboolean – true or false – will show dangling images.
label
label=<key> or label=<key>=<value>
before
<image-name>[:<tag>]<image id> or <image@digest> – filter images created before given id or references
since
<image-name>[:<tag>]<image id> or <image@digest> – filter images created since given id or references
reference
(pattern of an image reference) – filter images whose reference matches the specified pattern

Docker ps Filter Options

Get Social!

The below is an excerpt from docker.com listing the –filter options available with docker ps.

idContainer’s ID
nameContainer’s name
labelAn arbitrary string representing either a key or a key-value pair. Expressed as <key> or <key>=<value>
exitedAn integer representing the container’s exit code. Only useful with --all.
statusOne of createdrestartingrunningremovingpausedexited, or dead
ancestorFilters containers which share a given image as an ancestor. Expressed as <image-name>[:<tag>],<image id>, or <image@digest>
before or sinceFilters containers created before or after a given container ID or name
volumeFilters running containers which have mounted a given volume or bind mount.
networkFilters running containers connected to a given network.
publish or exposeFilters containers which publish or expose a given port. Expressed as <port>[/<proto>] or <startport-endport>/[<proto>]
healthFilters containers based on their healthcheck status. One of startinghealthyunhealthy or none.
isolationWindows daemon only. One of defaultprocess, or hyperv.
is-taskFilters containers that are a “task” for a service. Boolean option (true or false)

Remove Docker Container Based On Regex

Get Social!

This simple one-liner will take a regular expression (regex) and remove any Docker containers matching the pattern based on the name field. You can change the name match to be any other field accepted by the –filter switch.

Run the following docker ps command and substitute NAMEHERE* with the pattern you’d like to match. Careful, this command will delete any containers it finds.

docker ps --filter name=NAMEHERE* -aq | xargs docker stop | xargs docker rm

You can also filter on various other keys, such as status and volume using exactly the same method. Just replace the –filter element with the key from the below table, and the expression you want to match. 

idContainer’s ID
nameContainer’s name
labelAn arbitrary string representing either a key or a key-value pair. Expressed as <key> or <key>=<value>
exitedAn integer representing the container’s exit code. Only useful with --all.
statusOne of createdrestartingrunningremovingpausedexited, or dead
ancestorFilters containers which share a given image as an ancestor. Expressed as <image-name>[:<tag>],<image id>, or <image@digest>
before or sinceFilters containers created before or after a given container ID or name
volumeFilters running containers which have mounted a given volume or bind mount.
networkFilters running containers connected to a given network.
publish or exposeFilters containers which publish or expose a given port. Expressed as <port>[/<proto>] or <startport-endport>/[<proto>]
healthFilters containers based on their healthcheck status. One of startinghealthyunhealthy or none.
isolationWindows daemon only. One of defaultprocess, or hyperv.
is-taskFilters containers that are a “task” for a service. Boolean option (true or false)

See Docker PS Filter Options.

You can also filter for multiple conditions by passing the –filter switch multiple times. For example, name=webserver and status=running would look like this:

docker ps --filter name=webserver --filter status=running -aq | xargs docker stop | xargs docker rm

Run Multiple Bash Commands In Parallel

Tags :

Category : How-to

Get Social!

Bash, whilst great for simple things, can be tricky to use more advanced programming techniques that are easily exposed in things like Java, or Go.

Multithreading is one such problem. I often find myself with a series of tasks to perform that I’d like to run in parallel up to a predefined concurrency threshold.

My recent task which I’ll use as an example was to run multiple curl commands against an endpoint. These commands were standalone in the fact that they could be executed in any order and would benefit from running several API calls at once.

The first step is to create your list of commands in a file. For this, I’ll use the echo and sleep commands to demonstrate.

vi /tmp/myCommands

echo 1 && sleep 2
echo 2 && sleep 2
echo 3 && sleep 2
echo 4 && sleep 2
echo 5 && sleep 2
echo 6 && sleep 2
echo 7 && sleep 2
echo 8 && sleep 2
echo 9 && sleep 2
echo 0 && sleep 2

Once you have your list of commands, it’s time to run them!

cat /tmp/myCommands | while read n; do printf "%q\n" "$n"; done | xargs --max-procs=2 -I LC bash -c LC

The first command cat /tmp/myCommands is simply the path to your list of commands to run. The only other part to worry about is the —max-proxcs=2 attribute of xargs – this is what defines the concurrency and therefore how many ‘threads’ will run at once. xargs will do the rest – each command in your source file will be executed with 2 running at once!

So there you have it – threaded command execution in Bash!


Visit our advertisers

Quick Poll

Do you use GlusterFS in your workplace?

Visit our advertisers