Manage Systemd Units in Tagged AWS EC2 Instances using Fabric and boto from your local machine.

Ravindu Nirmal Fernando
DevOps SriLanka
Published in
2 min readDec 18, 2017

--

Image Source - https://www.fullstackpython.com/blog/choose-right-devops-tools.html

In this post, I will introduce you on how to interact with AWS EC2 instances using Fabric. Fabric is a Python package for streamlining the use of ssh on managing and automating deployments on a large number of servers. Using Fabric we can run shell commands on local or remote servers. In this post, we will use Fabric to automate the process of restarting a systemd unit within a CoreOS EC2 instance (Please note this can be used within any server type you manage within AWS). Importantly we will be writing this generically so that you can pass in the AWS instance tagged Name and the service file to be restarted as arguments in the final command that you need to execute.

For this, we will also have to use a library called boto, which is a Python-based AWS client.

As the initial step, download fabric and boto on to your local machines. Also, make sure Python is installed within your system before installing these.

pip install boto

pip install fabric

or

sudo apt-get install fabric

sudo apt-get install boto

Up next, since you are connecting with the AWS EC2 instances, you will need AWS private key file (.pem). Once you download it move it to a secure location. Ideally, it should be within /.ssh folder.

mv ~/Downloads/key_file.pem ~/.ssh/key_file.pem

chmod 400 ~/.sssh/key_file.pem

Once you do that, the next step would be to export AWS region, AWS access key and AWS secret access id environmental variables in your system. You can add it within your ~/.bashrc file for it to be permanently available You can obtain this key and secret key from your AWS console.

export AWS_EC2_REGION=us-west-1

export AWS_ACCESS_KEY_ID=<your-access-key>

export AWS_SECRET_ACCESS_KEY=<your-secret-key>

Up next we can write the Fabric script. Fabric script will be available within a file called fabfile.py. The true power of Fabric can be seen here, because we can do all the remote server managing configuration by just using Python language which is easily understandable and simple. Each fabric function defined can be identified as a fabric task, but private methods cannot be listed as its only accessible within the other functions.

Given below is a sample file for restarting a particular Systemd Unit within a CoreOS EC2 Instance. But you can extend this script and use it to perform any kind of command on AWS EC2 remote server right from your local machine without doing SSH into the servers.

After that what you have to do is just run the script with the desired method you want to execute. According to the above script given below is the sample command to restart a Systemd unit.

--

--