GitHub and EC2 manual deployment with Deploy keys
Tags: aws, ec2, github, deployment
Published: 2024-05-17 05:39:03

For those looking to quickly deploy a project, whether it’s a prototype or a solo endeavor, manual deployment with GitHub and AWS EC2 is a reliable and efficient method. Here’s a comprehensive guide to setting up your deployment using deploy keys.

### Setting Up Your EC2 Instance

Begin by launching an on-demand EC2 instance on AWS. Access this instance via SSH, and use Git to clone or pull your repository. This setup will be similar to your local development environment, except you’ll need to configure environment variables specific to your server.

To ensure your server is secure, configure nginx with SSL certificates. This adds a layer of security and professionalism to your deployment.

### Using GitHub Deploy Keys

Deploy keys provide a secure, read-only connection between your EC2 instance and your GitHub repository. Here’s how to set them up:

#### Step 1: Generate SSH Keys

First, generate an SSH key pair on your EC2 instance:

```sh
ssh-keygen -t ed25519 -f ~/.ssh/my_project_deploy_key -C "your_email@example.com"
```

Using `ed25519` instead of the more common RSA provides better security. The `-C` flag is optional but useful if you plan to use the key for commit signing in addition to deployment.

#### Step 2: Add the Public Key to GitHub

In your GitHub repository, navigate to **Settings > Deploy keys**. Click **Add Deploy Key**, provide a descriptive title like "EC2 Deployment Key", and paste the contents of your public key file (`~/.ssh/my_project_deploy_key.pub`). For most deployment scenarios, you won’t need to enable write access.

#### Step 3: Configure SSH for Git

To allow your EC2 instance to access multiple repositories without using the default `id_rsa` key name, configure your SSH client:

```sh
vim ~/.ssh/config
```

Add the following entries to the file:

```plaintext
Host github.com-my_project
  HostName github.com
  IdentityFile ~/.ssh/my_project_deploy_key
  User git

Host github.com-other_project
  HostName github.com
  IdentityFile ~/.ssh/other_deploy_key
  User git
```

This configuration allows you to manage multiple repositories with different keys.

To clone your repository, use the configured host:

```sh
git clone git@github.com-my_project:your_github_org/your_repo.git
```

You can then use `git pull` as needed to update your project.

### Improving Your Deployment Process

While this manual setup is quick and effective, it does have some limitations, such as downtime during updates. To minimize downtime, consider using a process manager like [pm2](https://futurestud.io/tutorials/pm2-cluster-mode-and-zero-downtime-restarts), which supports zero-downtime restarts.

As your project grows, SSH-based manual deployments might become cumbersome. Automating your deployment process can save time and reduce errors. You can use GitHub webhooks to trigger automatic deployments or configure your server as a Git remote to push updates directly. This approach can streamline your workflow and enhance efficiency.

### Conclusion

Deploying with GitHub and EC2 using deploy keys is a straightforward and effective way to manage your projects, especially for quick prototypes and solo projects. This method allows you to leverage the power of AWS and GitHub without the overhead of more complex deployment strategies. Stay tuned for future posts where we’ll explore advanced deployment techniques and best practices.