Securely Accessing Private AWS Resources from GitHub Actions with TailScale
Tags: aws, devops, cicd, githubactions
Published: 2023-12-14 09:49:49


## The Problem
One of the common problems of CI/CD automation is how to handle private resources without exposing these resources directly to the public or relying on complex VPN setups may not be the ideal solution.

Typically, you go with something like OpenVPN, but OpenVPN can be complicated, with something like OpenVPN,you have to set up your own server, and configure, maintain, etc. On the other hand, it takes less than 2 minutes to set up TailScale, Another thing, if you live in a country where OpenVPN is blocked, you don't have many options.


![diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94mvc3o7hfov0jkgypju.png)

Here is the diagram that we are going to be implanted in this blog, the blog post will guide you with step-by-step through its implementation. The initial steps involve setting up a GitHub Actions workflow that installs the TailScale client and authorizes it into a TailScale network. Subsequently, the blog post will cover the creation and configuration of the TailScale router, ensuring proper traffic routing and security group settings on AWS.

## Connecting GitHub Actions to the Tailscale Network

Before starting this guide, you'll need to have a Tailscale network set up and configured with at least one existing device, If you do not know how, follow this [guide](https://tailscale.com/kb/1017/install/) and of course you're going to need an AWS account as well.

Now the next thing that I'm going to do is go out to GitHub and create a new repository,You can find the code for the end result in this [repository](https://github.com/Fadyio/Accessing-Private-AWS-Resources).

You will then need to create an OAuth client to use with the Github action via this [link](https://login.tailscale.com/admin/settings/oauth), copy the Client ID and Client Secret to a safe place for now.

![Image1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rwkzb6stw7ra2j2u501c.png)

Go to the GitHub repo you created earlier and go to Settings > in the Security section of the sidebar, select Secrets and Variables, then click Actions, Create a GitHub secret with the name `TS_OAUTH_CLIENT_ID` and assign your OAuth client ID as the secret value and name `TS_OAUTH_SECRET` and assign your OAuth client secret as the secret value.


![Image2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uaifdh4hrozg3cv1m5wm.png)

You will then need to create a server role account using ACL tags in Tailscale, basically ACL tags allow you to assign an identity to a device that is separate from human users, and use that identity as part of an ACL to restrict access. This should be used when adding servers to your Tailscale network, to learn more about ACL tags, [click here](https://tailscale.com/kb/1068/acl-tags/)

Open your favorite editor and create a .github directory and underneath .github we'll create a Workflows directory, this is the special directory that GitHub Actions is going to look for workflow files in, create a new workflow, I'm going to call it tailscale.yaml.


![Image3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fddybie3esuh1aciwmb3.png)

And inside of this file, we're going to go ahead and set up our basic workflow structure.

```yaml
on:
  workflow_dispatch:
  push:

jobs:
  deploy-via-tailscale:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Tailscale
      uses: tailscale/github-action@v2
      with:
        oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
        oauth-secret: ${{ secrets.TS_OAUTH_SECRET }}
        tags: tag:ci

```
Let me break it down for you. 

- the workflow dispatch event. Allows us to trigger workflows manually, which is great for debugging.
- The push event means that whenever we push a change, it goes ahead and triggers a build.
- Define the job and name it deploy-via-tailscale. It will run on the latest release of Ubuntu.
- connect to your tailnet by using the [Tailscale GitHub Action](https://github.com/tailscale/github-action)

Go ahead and push the code to Github, you should see an ephemeral node. The node can access nodes in your Tailnet, subject to the access rules that apply to the specified ACL tag(s).


![Image4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opyp675a1hq6r3heokce.png)

## Set Up Amazon VPC and Tailscale Router

What we're going to do is we're going to create a VPC, and then we're going to have this public subnet, and we're going to put an EC2 instance on that public subnet, and we're going to have that EC2 instance connect out to our Tailscale network as well, and then we're going to set that EC2 instance up as a router.


![Image5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7y65ubbh3j6g49l7ouf.png)

Let's go to the AWS Management Console. First, we'll create  an elastic IP and EC2 instance on the public subnet, which will act as a subnet router.


![Image6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95c7setyxrh1ltrjeowc.png)

After the instance has passed the checks we need to download and install Tailscale onto your subnet router machine.

![Image8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4m2330omx4vjc9j1xsl.png)

Step 1: Install the Tailscale client

ssh into the instance and follow the instructions

```
curl -fsSL https://tailscale.com/install.sh | sh
```

![Image9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7d5spmja299ocw94thuc.png)

Step 2: Enable IP forwarding and Advertise subnet routes
```
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.d/99-tailscale.conf
echo 'net.ipv6.conf.all.forwarding = 1' | sudo tee -a /etc/sysctl.d/99-tailscale.conf
sudo sysctl -p /etc/sysctl.d/99-tailscale.conf
```

![Image10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hoyyq612mnac8f8p6e6c.png)

```
sudo tailscale up --advertise-routes==10.0.16.0/20 --accept-dns=false
```
Replace the subnets in the example above with the correct ones for your network, it will give you a link that you will need to go to in order to authenticate.

![Image11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vyrd2i0ewunfgtrkwwwd.png)

The EC2 instance should appear in the Tailscale console.


![Image12](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lk1pqm5f7pna5rbn0fbf.png)

Step 3: Enable subnet routes from the tailscale console

Locate the device that advertised subnet routes, from the ellipsis menu at the bottom of the table, select Edit Route Settings. This opens the Edit Route Settings panel.


![Image13](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vobe2qop3pmivulamk8v.png)

Everything should be working to test connectivity, we will add test connectivity to the Github action, commit and push the code.

```yaml
on:
  workflow_dispatch:
  push:

jobs:
  deploy-via-tailscale:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Tailscale
      uses: tailscale/github-action@v2
      with:
        oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
        oauth-secret: ${{ secrets.TS_OAUTH_SECRET }}
        tags: tag:ci
    - name: test-connectivity
      run: |
        ping -c 10 100.73.32.96
```
Replace the IP address with the IP address of the subnet router.


![Image14](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04n926wxvd7v2d3a9pn4.png)

## Create Private Amazon RDS Instance and Test Connectivity

After reviewing and verifying, we will provision an Amazon RDS instance in our VPC and test connectivity from our GitHub Actions workflow.

First, we will create a security group that will allow connection from the subnet router, I'm going to call it the database security group.


![Image15](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dj4p1vezeqm79osb5da7.png)


In a production environment, it is bad practice to allow all connections from a security group, Only allow access from trusted sources for specific ports and protocols, following the principle of least privilege.


I will not cover how to create an RDS database here. I assume you have already created an RDS database, please refer to the [AWS documentation](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_CreateDBInstance.html) if you don't know how to create one.


![Image16](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qh71vq96tdd8gww8xfys.png)

If you want to test connectivity between your machine and the private Amazon RDS instance type the following command. This action allows you to connect to the MySQL DB instance using the MySQL client, Replace endpoint with the DB instance endpoint (DNS name) and replace admin with the master username you used. Enter the master password that you used when prompted for a password.
```
mysql -h endpoint -P 3306 -u admin -p
```

![Image17](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zihq2sdr7gwd7jv5x6dw.png)

If everything is configured correctly, you should be able to see this massage.

![Image18](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k643vxv20e1h5g77rad9.png)

We will return to the Github action workflow to test connectivity, we will create three secrets for the database endpoint, password, and username.


![Image19](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k03ou9o7r2b3yrz6pnlc.png)

Update the workflow with this to test connectivity. We can now connect to RDS from any node in the Tailnet, using the same DNS name used inside AWS.


```yaml
on:
  workflow_dispatch:
  push:

jobs:
  deploy-via-tailscale:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Tailscale
      uses: tailscale/github-action@v2
      with:
        oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
        oauth-secret: ${{ secrets.TS_OAUTH_SECRET }}
        tags: tag:ci
    - name: Test-Connectivity-to-AWS-RDS
      run: |
        mysql -h ${{ secrets.ENDPOINT }} -u ${{ secrets.USER }} -p"${{ secrets.PASSWORD }}" -e "show databases;"
```

![Image20](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tb6ovnsipi1xtzq5jibi.png)

## Conclusion

In this blog, we've demonstrated how to create a secure and seamless connection between GitHub Actions workflows and private AWS resources. This approach not only improves the overall security of your CI/CD pipeline, but also ensures that sensitive data and interactions remain within your network, TailScale is easy to use, and has robust encryption mechanisms.

This is not sponsored or endorsed by TailScale, I use the TailScale at work and in my home lab, I am just a happy customer.

One more thing, you can host [Tailscale Control Server](https://github.com/juanfont/headscale) yourself if you want, which is a plus.