Introduction:
Hey Dev.to community! 👋
Are you tired of editing files on your server, then forgetting to back them up or version control them?
I was too — until I built a simple DevOps pipeline that runs on my EC2 instance and automatically pushes code to GitHub. No more “forgot to commit” or lost files. Just clean, trackable deployments.
In this blog, I’ll show you how I created a shell script that:
Organizes and builds my project locally on EC2.
Syncs the latest files.
Commits and pushes them to a GitHub repository.
All with one command 🚀
✨ Let’s dive in, shall we?✨
📚 Table of Contents
- Step 1: Spin Up an EC2 Instance in AWS
- Step 2: The Deployment Script (Let’s Automate!)
- Step 3: Secure SSH Access (No More Worries!)
- Step 4: Run the Deployment Script
- Step 5: See It in Action (The Magic Unfolds!)
- Step 6: The Results
- Step 7: Bonus Tips
- Step 8: Conclusion
🌬️ The Problem (And the Solution)
You know that feeling when you push changes to GitHub, SSH into your EC2 instance, and manually copy files over? 🙄 It's time-consuming and error-prone. But automation is the hero we need. 🦸♂️
🌬️ Here’s the magic I set up:
GitHub: Hosting my code.
AWS EC2: Running the application.
Bash Script: Making everything automated and seamless.
🌬️ Prerequisites:
Before jumping into the magic sauce, make sure you have:
A GitHub account and a project to deploy.
An EC2 instance up and running in AWS (Amazon Linux or Ubuntu works great).
SSH access to your EC2 instance (you’ll use a .pem key).
Basic knowledge of bash scripts and Linux commands (don’t worry, I’ll guide you through it!).
Step 1: Spin Up an EC2 Instance in AWS
The first thing we need is an EC2 instance. You can easily do this from the AWS console. Choose an Amazon Linux or Ubuntu AMI, and make sure:
Security Group allows SSH access on port 22 (for remote connections).
Assign an Elastic IP or use the public IP for easy access.
Once the instance is running, grab the public IP address and SSH into it using your .pem file:
Step 2: The Deployment Script (Let’s Automate!)
Now, let’s talk about the heart of the automation: the bash script! This script will handle:
Pulling the latest code from GitHub.
Building your project (if necessary).
Deploying the project to your EC2 instance using rsync.
Here’s what my script looks like:
🌬️ How it Works:
Git Pull: This pulls the latest changes from GitHub (so your server is always up to date).
Build Script: If you need to compile or prepare your project, this step handles it.
rsync: The magic happens here! It transfers the files from your local machine to the EC2 instance.
Step 3: Secure SSH Access (No More Worries!)
Before using SSH, you need to make sure your private key has the correct permissions:
chmod 400 ~/Downloads/devops.pemverify that your security group allows SSH (port 22) access. Once everything is in place, you can test the connection.
ssh -i ~/Downloads/devops.pem ec2-user@your-ec2-ip
Step 4: Run the Deployment Script
Once the script is ready and SSH access is set up, running the deployment is a breeze. Simply execute:
This does everything for you:
Pull the latest changes from GitHub.
Run the build script (if any).
Deploy the project to the EC2 instance using rsync.
Step 5: See It in Action (The Magic Unfolds!)
Once you’ve written your code, just follow this simple two-step magic combo:
Push to GitHub and Run the Deployment Script.
This pulls the latest code to GitHub and syncs it to your server with rsync.
🌬️ The Results:
Automation: Every time you push new changes to GitHub, just run the script, and poof, your EC2 instance is updated. No more manual file copying!
Speed: The rsync command only copies changed files, so the process is super fast.
Reproducibility: You can use the same script across multiple instances, ensuring consistency in your deployments.
🌬️ Bonus Tips:
CI/CD Integration: Want to go even further? Integrate this setup with GitHub Actions or Jenkins to trigger deployments automatically every time you push to GitHub.
Security: Use SSH key management to secure your EC2 instances and ensure only authorized users can deploy.
🌬️ Conclusion:
And there you have it! With just a few tools, I’ve automated my deployment pipeline using AWS EC2 and GitHub. What once took hours of manual deployment now takes seconds. The beauty of DevOps is in its automation and speed. 💨
By following these steps, you’ll have a robust and scalable pipeline that will save you time and ensure smooth deployments. Whether you're a beginner or a seasoned pro, this is an awesome project to get started with in the world of DevOps. 🌍
#30DaysLinuxChallenge #CloudWhisler
DevOps #Linux #RHCSA #Opensource #AWS #CloudComputing
Catch out by My LinkedIn profile
https://www.linkedin.com/in/rajpreet-gill-4569b4161/