
Using EC2 Instance Connect to clone a GitHub repository from an EC2 in a private subnet
Hello! I'm Kobayashi from the Manufacturing Business Technology Department.
In a previous blog, I introduced how to connect to EC2 instances in private subnets using EC2 Instance Connect.
This time, I'll try cloning a GitHub repository from a connected instance!
What I Want to Do
Connect to an EC2 instance using a private IP via the EC2 Instance Connect feature in the AWS console, and clone a GitHub repository on that instance.
Architecture Diagram
The architecture for this setup is shown below.
Prerequisites
- Using an EC2 instance with Amazon Linux 2023
- EC2 Instance Connect connection has been established
- Environment with internet access (via NAT Gateway, etc.)
Let's Try It
Let's start by installing the necessary tools.
Installing Git
First, install Git for version control.
# System update
sudo dnf update -y
# Installing Git
sudo dnf install git -y
# Confirming Git installation
git --version
# Git configuration
git config --global user.name "XXXXXXXXXXXX"
git config --global user.email "XXXXXXXXXXXX"
Installing GitHub CLI
To simplify authentication with GitHub, install GitHub CLI (gh).
## Installing Github CLI
type -p yum-config-manager >/dev/null || sudo yum install yum-utils
sudo yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
sudo yum install gh
## Confirming GitHub CLI installation
gh --version
Authenticating with GitHub
To access GitHub private repositories from EC2, use GitHub CLI for authentication.
## Starting GitHub CLI authentication
gh auth login
Selecting Authentication Options
During authentication, proceed by selecting the following options:
? Where do you use GitHub?
> GitHub.com
? What is your preferred protocol for Git operations on this host?
> HTTPS
? Authenticate Git with your GitHub credentials?
> Yes
? How would you like to authenticate GitHub CLI?
> Login with a web browser
```### Browser Authentication Procedure
Since the EC2 instance does not have a GUI browser installed, perform manual authentication with the following steps.
```sh
! First copy your one-time code: 6324-1C66
Press Enter to open https://github.com/login/device in your browser...
! Failed opening a web browser at https://github.com/login/device
Make note of the one-time code
Note the displayed code (example: 6324-1C66).
! First copy your one-time code: 6324-1C66
Press Enter to open https://github.com/login/device in your browser...
! Failed opening a web browser at https://github.com/login/device
Authentication in Local PC Browser
-
Open https://github.com/login/device in your browser
-
Enter the displayed one-time code
-
Authorize the authentication
-
Authentication completed
Verify Authentication Status
# Check if authentication was successful
gh auth status
When successful, you'll see output like this:
github.com
✓ Logged in to github.com account xxxxxxxx (/home/ec2-user/.config/gh/hosts.yml)
- Active account: true
- Git operations protocol: https
- Token: gho_************************************
- Token scopes: 'gist', 'read:org', 'repo', 'workflow'
Verification Test
Once authentication is complete, let's try cloning a GitHub repository (private)!
# Create a working directory
mkdir workspace
cd workspace
# Clone GitHub repository
git clone <GitHub repository URL>
When cloning is successful, you'll see content like this:
Cloning into 'prisma-aws-migration'...
remote: Enumerating objects: 45, done.
remote: Counting objects: 100% (45/45), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 45 (delta 8), reused 45 (delta 8), pack-reused 0
Receiving objects: 100% (45/45), done.
Resolving deltas: 100% (8/8), done.
```## Conclusion
I've explained how to connect to an EC2 instance in a private subnet using EC2 Instance Connect, and from there, how to clone a GitHub repository.
Using GitHub CLI (gh command) is convenient as you can easily log into GitHub without troublesome authentication settings or token management!
I hope this article is helpful for you all.
## Reference Articles
https://dev.classmethod.jp/articles/aws-cloudshell-github-cli/