Uly.me

cloud engineer

  • Home
  • About
  • Archives
Home/2017/Archives for May 2017

Archives for May 2017

May 24, 2017

SSH To A Docker Container

Docker containers are awesome. Docker allows you to quickly create development environments in a matter of minutes. Docker gives you the ability to package, ship and share your docker image to anyone. Once your docker image is on the Docker repository, anyone can pull it down and run it on their own operating system.

To keep track of running containers on your system, you can type ‘docker ps -a’ on your terminal.

$ docker ps -a
# gives a result similar to this ...
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
f322d2370415        moul/icecast        "/start.sh"              2 hours ago         Up 2 hours          0.0.0.0:8000->8000/tcp   icecast_icecast_1

$ docker ps -a # gives a result similar to this ... CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f322d2370415 moul/icecast "/start.sh" 2 hours ago Up 2 hours 0.0.0.0:8000->8000/tcp icecast_icecast_1

If you would like to SSH to a running Docker container, just run the following from the terminal.

docker exec -t -i f322d2370415 /bin/bash
# it will take you to this ... 
root@f322d2370415:/#

docker exec -t -i f322d2370415 /bin/bash # it will take you to this ... root@f322d2370415:/#

f322d2370415 is the Container ID, while /bin/bash is the shell that you would like to use.

Filed Under: Cloud Tagged With: docker, ssh

May 24, 2017

Start EC2 AMI from AWS CLI

Amazon Web Services has an API via a CLI (command line interface) which give users the ability to manage servers from a remote host. The AWS CLI must be installed and authenticated to AWS on the host computer. Once a user is logged in to AWS, they can perform certain management tasks such as starting and stopping EC2 instances.

How to start EC2 instance from AWS CLI

Requires an image id, instance count, instance type, key and security group.

aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type c4.2xlarge --key-name your-key  —security-group-ids sg-xxxxxxxx

aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type c4.2xlarge --key-name your-key —security-group-ids sg-xxxxxxxx

How to Associate an Elastic IP Address to an Instance

Requires an instance id and Elastic IP address.

aws ec2 associate-address --instance-id i-xxxxxxxxxxxxxxxx --public-ip xxx.xxx.xxx.xxx

aws ec2 associate-address --instance-id i-xxxxxxxxxxxxxxxx --public-ip xxx.xxx.xxx.xxx

How to Terminate an Instance

Requires an instance id(s).

aws ec2 terminate-instances --instance-ids i-xxxxxxxxxxxxxxxx

aws ec2 terminate-instances --instance-ids i-xxxxxxxxxxxxxxxx

Filed Under: Cloud Tagged With: aws, cli, ec2

May 19, 2017

JQ Proccessor

The AWS CLI spits out a JSON output after each successful execution. If you need to grab the result, assign it to a variable, and use it for your subsequent scripts, you need some kind of JSON parser. You can use a tool like jq which will process or filter out the result for you. From jq’s website,

jq is a tool for processing JSON inputs, applying the given filter to its JSON text inputs and producing the filter’s results as JSON on standard output. The simplest filter is ., which is the identity filter, copying jq’s input to its output unmodified (except for formatting).

In this example, we will use the AWS CLI to give us a list of running EC2 instances. We will then dump the output into a file called output.json. We will then filter out the “InstanceId” by running it through cat and the jq processor. We will then assign the result to a variable called INSTANCE, and then finally use that variable to associate our instance to an Elastic IP Address.

aws ec2 describe-instances --filters Name=instance-state-name,Values=running > output.json
INSTANCEID=$(cat output.json | jq '.Reservations[].Instances[] | {InstanceId} | .InstanceId' --raw-output)
aws ec2 associate-address --instance-id $INSTANCEID --public-ip xxx.xxx.xxx.xxx

aws ec2 describe-instances --filters Name=instance-state-name,Values=running > output.json INSTANCEID=$(cat output.json | jq '.Reservations[].Instances[] | {InstanceId} | .InstanceId' --raw-output) aws ec2 associate-address --instance-id $INSTANCEID --public-ip xxx.xxx.xxx.xxx

Filtering a nested JSON can be a bit tricky. In this particular case, we are using a filter you’ll find inside the single quote right after the jq command. To remove quotes from our result, I’m using –raw-output switch. Finally, I then associate our instance to an elastic public IP address.

jq is a very handy tool.

Filed Under: Cloud, Linux Tagged With: aws cli, jq

  • 1
  • 2
  • 3
  • Next Page »
  • Cloud
  • Linux
  • Git

Copyright © 2012–2021