Everyday we have to do many boring stuffs when it comes to DevOps. Everytime we have to deploy new version of our application, we need to run branch of codes. If we are using complex architecture like AWS ECS or GCP Kubernetes etc, deployment can be automated and it saves lots of our time. But when it comes to a small node management (AWS EC2, Linode, DigitalOcean Droplets etc) we need to connect via SSH and run our commands. That’s a boring stuff and I don’t like it. Also when we are deploying to development server, it has to be done many times a day. So, if deployment takes 5 mins and we need to do it 20 times just imagine I have to spend one hour for deployment.

Python Fabric (www.fabfile.org) is a library or package which can run shell commands remotely via SSH. It saves million people’s time just by automating the tasks we needed to run by ourselves. Today, we will see how we can connect to an AWS EC2 instance and executing our task. The amazing part of using Python Fabric is, we don’t need to install or configure anything of the server side.

Let’s say we have an EC2 instance on IP x.x.x.x or DNS ec2-x-x-x-x-region.compute.amazonaws.com. And also we have a certificate file named x-x-x-x.pem for user ec2-user which we will be needed to connect with this instance. Now let’s install python3 and fabric if you don’t have it already. Let’s create a file named fabfile.py on our project root. To connect to our EC2 instance: