How to Connect GitHub to Your EC2 Instance: Easy-to-Follow Step-by-Step Guide

Content Roadmap

Connecting GitHub with AWS EC2 Instance

Are you looking to seamlessly integrate your GitHub repository with an Amazon EC2 instance? Connecting GitHub to your EC2 instance allows you to easily deploy your code, automate workflows, and streamline your development process.

In this comprehensive guide, we’ll walk you through the step-by-step process of setting up this connection, from creating an EC2 instance to configuring webhooks and deploying your code.

By the end of this article, you’ll have a fully functional GitHub-EC2 integration, enabling you to focus on writing great code and delivering your projects efficiently.

Github + ec2 integracion process
A cartoon of a man with glasses and a bow tie

Before you begin

Before we dive into the process of connecting GitHub to your EC2 instance, make sure you have the following prerequisites in place:

1️⃣ AWS Account:

An AWS account with access to the EC2 service

2️⃣ GitHub Account:

A GitHub account with a repository you want to connect to your EC2 instance

3️⃣ Basic KNowledge:

Basic knowledge of AWS EC2 and GitHub

With these prerequisites in hand, let’s get started with the process of creating an EC2 instance.

Discover the Benefits of Connecting GitHub to Your EC2 Instance

Illustration of a website on development

1. Automation:

Connecting your GitHub repository to your EC2 instance enables you to automate code deployments. Changes pushed to your repo can trigger automatic updates on the EC2 instance, making the development and release process much smoother.

Icon of centralization with github

2. Centralized Code:

GitHub acts as a central hub for your project code. This allows multiple developers to work on the same codebase simultaneously, improving collaboration and code sharing.

Secure github access

3. Controlled Access: pen_spark:

GitHub's access control mechanisms let you manage who can view, modify, and deploy your code. This helps in maintaining the security and integrity of your application.

Creating an EC2 Instance

The first step in connecting GitHub to your EC2 instance is to create an EC2 instance. Follow these steps to create a new instance:

  1. Login to your AWS Management Console. and navigate to the EC2 dashboard.
  2. Click on the “Launch Instance” button to start the instance creation wizard.
  3. Choose an Amazon Machine Image (AMI) that suits your requirements. For this guide, we’ll use the Amazon Linux 2 AMI.
  4. Select an instance type based on your computational needs and budget. A t2.micro instance is sufficient for most basic applications.
  5. Configure the instance details, such as the number of instances, network settings, and IAM role (if required).
  6. Add storage to your instance. The default settings are usually sufficient for most use cases.
  7. Add tags to your instance for better organization and management.
  8. Configure the security group to control inbound and outbound traffic to your instance. We’ll dive deeper into this in the next section.
  9. Review your instance configuration and click on the “Launch” button.
  10. Choose an existing key pair or create a new one. This key pair will be used to securely connect to your EC2 instance via SSH.
  11. Launch your instance and wait for it to be in the “Running” state.

Congratulations! You have successfully created an EC2 instance. Let’s move on to configuring the security group to allow necessary traffic.

Configuring Security Groups on AWS

Security groups act as virtual firewalls for your EC2 instances, controlling inbound and outbound traffic. To connect GitHub to your EC2 instance, you need to configure the security group to allow SSH and HTTP/HTTPS traffic. Follow these steps:

  1. In the EC2 dashboard, navigate to the “Security Groups” section under “Network & Security.”
  2. Select the security group associated with your EC2 instance.
  3. In the “Inbound Rules” tab, click on the “Edit inbound rules” button.
  4. Add a new rule for SSH (port 22) and set the source to your IP address or a specific IP range.
  5. Add another rule for HTTP (port 80) and HTTPS (port 443) and set the source to “Anywhere” or a specific IP range, depending on your requirements.
  6. Save the inbound rules.

Your security group is now configured to allow the necessary traffic for connecting GitHub to your EC2 instance.

Installing Git on the EC2 Instance

To clone your GitHub repository and manage version control on your EC2 instance, you need to install Git. Follow these steps to install Git on your Amazon Linux 2 instance:

  1. Connect to your EC2 instance using SSH. Use the key pair you specified during instance creation.
  2. Update the package manager by running the following command:
    sudo yum update -y
    
  3. Install Git by running the following command:
    sudo yum install git -y
    
  4. Verify the installation by checking the Git version:
    git --version
    

Git is now installed on your EC2 instance, and you’re ready to clone your GitHub repository.

Generating SSH Keys

To securely connect your EC2 instance to GitHub, you need to generate an SSH key pair. Follow these steps to generate SSH keys on your EC2 instance:

  1. Connect to your EC2 instance using SSH.
  2. Run the following command to generate an SSH key pair:
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

    Replace your_email@example.com with your GitHub email address.

  3. Press Enter to accept the default file location for saving the key pair.
  4. Optionally, enter a passphrase for added security. Press Enter if you don't want to set a passphrase.
  5. The SSH key pair will be generated and saved in the specified location (default: ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub).

Add SSH Key to GitHub account

To enable your EC2 instance to securely communicate with GitHub, you need to add the public SSH key to your GitHub account. Follow these steps:

  1. On your EC2 instance, run the following command to display the public key:
    cat ~/.ssh/id_rsa.pub
    
  2. Copy the entire contents of the public key.
  3. Log in to your GitHub account and navigate to the “Settings” page.
  4. Click on “SSH and GPG keys” in the left sidebar.
  5. Click on the “New SSH key” button.
  6. Enter a title for the key to identify it easily (e.g., “EC2 Instance Key”).
  7. Paste the copied public key into the “Key” field.
  8. Click on the “Add SSH key” button to save the key.

Your EC2 instance is now linked to your GitHub account using the SSH key. Let’s proceed to cloning your repository.

A logo with a key attached to a black square
A diagram of a git clone

Cloning a Repository

To clone your GitHub repository to your EC2 instance, follow these steps:

  1. Connect to your EC2 instance using SSH.
  2. Navigate to the directory where you want to clone the repository.
  3. Run the following command to clone the repository using SSH:
    git clone git@github.com:your-username/your-repository.git
    

    Replace “your-username” with your GitHub username and “your-repository” with the name of your repository.

  4. Enter the passphrase for your SSH key, if prompted.
  5. The repository will be cloned to your EC2 instance.

You have successfully cloned your GitHub repository to your EC2 instance. You can now work with the code locally on your instance.

Configure a GitHub webhook in 7 easy steps

Webhooks allow you to automate actions based on events in your GitHub repository. For example, you can configure a webhook to automatically deploy your code to your EC2 instance whenever a push is made to the repository. Follow these steps to set up a webhook:

  1. In your GitHub repository, navigate to the “Settings” page.
  2. Click on “Webhooks” in the left sidebar.
  3. Click on the “Add webhook” button.
  4. Enter the payload URL, which is the URL of your EC2 instance where you want to receive the webhook events.
  5. Select the content type as “application/json.”
  6. Choose the events that should trigger the webhook. For example, you can select “Push events” to trigger the webhook whenever a push is made to the repository.
  7. Click on the “Add webhook” button to save the webhook configuration.

Your webhook is now set up, and GitHub will send POST requests to the specified payload URL whenever the selected events occur.

Deploying to AWS EC2 from Github

With the webhook configured, you can automate the deployment of your code to your EC2 instance whenever changes are pushed to your GitHub repository. Here’s a general outline of the deployment process:

  1. Create a deployment script on your EC2 instance that will be triggered by the webhook.
  2. The deployment script should perform the following tasks:
    • Pull the latest changes from the GitHub repository.
    • Install any necessary dependencies.
    • Build and compile your application, if required.
    • Restart any services or application servers.
  3. Configure your web server (e.g., Apache or Nginx) on the EC2 instance to serve your application.
  4. Ensure that the necessary ports (e.g., 80 for HTTP, 443 for HTTPS) are open in your EC2 instance’s security group.
  5. Test your deployment by making a change to your GitHub repository and verifying that the changes are automatically deployed to your EC2 instance.

The specific steps for deploying your code will vary depending on your application’s requirements and the technologies you are using. You may need to use additional tools like AWS CodeDeploy or a continuous integration/continuous deployment (CI/CD) pipeline to streamline the deployment process.

Tips for Troubleshooting Common Technology Issues While Connecting GitHub to your EC2 Instance

1. Secure Port

Ensure that your EC2 instance's security group is configured correctly to allow incoming SSH and HTTP/HTTPS traffic.

2. SSH Verification

Verify that your SSH key pair is correctly generated and added to your GitHub account.

3. Payload URL Checking

Double-check the payload URL and the events selected for your webhook configuration.

4. Logs on EC2 Instance

Check the logs on your EC2 instance for any error messages related to the deployment process.

5. Necessary Permissions

Ensure that your deployment script has the necessary permissions to execute and modify files on your EC2 instance.

6. Check Dependencies

Verify that your application's dependencies are correctly installed and configured on the EC2 instance.

7. Test Everything Locally First

Test your application locally on the EC2 instance to rule out any application-specific issues.

If you still face issues, consult the AWS and GitHub documentation (Trobleshotting Conections) or seek assistance from the respective communities or support channels.

Conclusion

Connecting GitHub to your EC2 instance provides a seamless way to deploy your code and automate your development workflow. By following the steps outlined in this guide, you can create an EC2 instance, configure security groups, install Git, generate SSH keys, clone your repository, set up webhooks, and deploy your code to the instance.

Remember to regularly review and update your security settings, keep your EC2 instance and application dependencies up to date, and monitor your application’s performance and logs for any issues.

With GitHub and EC2 connected, you can focus on writing quality code, collaborating with your team, and delivering your applications efficiently.

Jesus Guzman

M&G Speed Marketing LTD. CEO

Jesus Guzman is the CEO and founder of M&G Speed Marketing LTD, a digital marketing agency focused on rapidly growing businesses through strategies like SEO, PPC, social media, email campaigns, and website optimization. With an MBA and over 11 years of experience, Guzman combines his marketing expertise with web design skills to create captivating online experiences. His journey as an in-house SEO expert has given him insights into effective online marketing. Guzman is passionate about helping businesses achieve impressive growth through his honed skills. He has proud case studies to share and is eager to connect to take your business to the next level.