Connect GitHub to AWS with CodeConnections

Link your GitHub repo to AWS using CodeConnections for CI/CD pipelines.

Introduction

30 Second Summary

Every time you save code on your computer, it stays invisible to the cloud services that need to deploy it. Connecting your code repository to your cloud account is the first step in making deployments automatic.

In this project, you will push your Java web app to a GitHub repository and create an AWS CodeConnections resource that formally links your GitHub account to your AWS account.

What You'll Build

A fully wired source control pipeline foundation where your Java web app lives in GitHub and your AWS account can formally access it through a CodeConnection resource.

By the end of this project, you'll have:

  • A GitHub repository containing your web app source code with a full commit history.
  • A working AWS CodeConnection linking your GitHub account to AWS with an Available status.
  • A verified end-to-end flow where code changes pushed from your EC2 instance appear in GitHub and are visible to AWS services.
  • Secret Mission: Set up a Git branching workflow with a develop branch and practice the branch-merge pattern used by real DevOps teams.

Are there any prerequisites?

This project is Day 2 of the 7 Day DevOps Challenge. You need to have completed Day 1, which means you already have an EC2 instance with a Maven web app, VS Code with Remote - SSH, an AWS account with an IAM Admin User, and a GitHub account.

Reconnect to EC2 and Install Git

Every CI/CD pipeline starts with source code that lives in a central repository. Before you can push your web app code to GitHub, you need your development environment back online and a version control tool installed.

In this step, you will restart your EC2 instance, reconnect VS Code, and install Git so your project is ready for version control.

In this step, get ready to:
  • Start your EC2 instance and reconnect VS Code via Remote - SSH.
  • Install and configure Git with your identity.
  • Initialize a local Git repository for your web app.
Start your EC2 instance and reconnect VS Code

Your EC2 instance is currently stopped from the end of Day 1. You need to start it again and note its new public address, because EC2 assigns a different Public IPv4 DNS each time an instance is stopped and restarted.

  • Sign in to the AWS Management Console.
  • In the search bar at the top, type EC2 and select EC2 from the results.
  • In the left navigation panel, click Instances.
  • Select the checkbox next to your stopped instance (named nextwork-devops-yourname).
  • Click the Instance state dropdown and select Start instance.
  • Wait for the Instance state column to show Running.
  • Click on your instance ID to open the instance details page.
  • Copy the new Public IPv4 DNS value. You will need this to update your SSH connection.

Why does the Public IPv4 DNS change?

When you stop and start an EC2 instance, AWS releases its previous public IP address and assigns a new one. This means your SSH config from Day 1 points to an address that no longer exists.

You need to update your SSH config with the new address before VS Code can reconnect.

Now reconnect VS Code to your EC2 instance using the new address.

  • Press Cmd+Space (macOS) or the Windows key (Windows) to open your search bar.
  • Type VS Code and press Enter to open it.
  • Press F1 to open the Command Palette.
  • Type Remote-SSH: Open Configuration File... and select it.
  • Choose your SSH config file (usually ~/.ssh/config).
  • Find the HostName line for your EC2 host entry and replace the old address with your new Public IPv4 DNS.
  • Save the config file.
  • Press F1 again to open the Command Palette.
  • Type Remote-SSH: Connect to Host... and select your EC2 host.
  • VS Code opens a new window and connects to your instance. You will see the hostname in the bottom-left status bar once connected.
  • Click File in the top menu bar, then click Open Folder.
  • Type /home/ec2-user/nextwork-web-project and click OK.

Connection timed out?

If VS Code cannot connect, double check that your instance state is Running in the EC2 console. Also confirm that the HostName in your SSH config matches the new Public IPv4 DNS exactly.

If the instance is running but you still cannot connect, check that your security group allows inbound SSH on port 22.

Help me troubleshoot my VS Code Remote - SSH connection to EC2.

Install and configure Git

Git is the version control tool that tracks every change to your code. You need it installed on your EC2 instance so you can commit your web app files and push them to GitHub.

  • Open the terminal in VS Code by pressing Ctrl+` (backtick key, below Escape).
  • Check if Git is already installed by running this command:
git --version

✔️ I see a version number

Git is already installed from Day 1. You should see something like git version 2.47.1. Skip ahead to configuring your Git identity below.

ⓧ Command not found

Git is not installed yet. Install it by running this command:

sudo dnf install git -y

What does this command do?

`sudo` runs the command with administrator privileges. `dnf` is the package manager on Amazon Linux 2023. The -y flag automatically confirms the installation prompt.

After the install completes, verify it worked by running:

git --version

You should see git version 2.47.1 (or a similar version number).

Now configure your Git identity. This attaches your name and email to every commit you make, so your team knows who wrote what.

  • Set your Git name and email by running these commands:
git config --global user.name "[[YOURNAME="Your Name"]]"
git config --global user.email [[YOUR_EMAIL="youremail@example.com"]]

What does this configure?

The --global flag applies these settings to every Git repository on this machine. Your name and email will appear in your commit history so collaborators can identify who made each change.

  • Verify your configuration by running:
git config --list

You should see your user.name and user.email values listed in the output.

Don't see your name or email in the output?

Make sure you included the --global flag in your git config commands. Without it, the settings only apply to a specific repository (which does not exist yet).

Help me fix my Git config settings.

Initialize the local Git repository

Your web app files already exist on the EC2 instance from Day 1, but Git is not tracking them yet. Initializing a repository tells Git to start watching this folder for changes.

  • Confirm you are in the correct directory by running:
pwd

You should see /home/ec2-user/nextwork-web-project in the output. If you see a different path, navigate there by running:

cd /home/ec2-user/nextwork-web-project
  • Initialize Git tracking by running this command:
git init

What does git init do?

This command creates a hidden .git folder inside your project directory. That folder stores all of Git's internal tracking data. From this point on, Git can see every file you add, edit, or delete.

  • Check the status of your new repository by running:
git status

You should see your web app files (src/, pom.xml) listed in red as untracked files. This means Git can see them but has not started tracking changes yet.

Not seeing any files in git status?

Make sure you ran git init inside the /home/ec2-user/nextwork-web-project directory, not in your home folder. Run pwd to confirm your location.

If you accidentally initialized in the wrong folder, navigate to the correct folder and run git init again.

Help me figure out why git status shows no files.

Your development environment is back online and Git is ready to go. Next up, you will create a GitHub repository and push your web app code to it.

Create a GitHub Repository and Push Your Code

Your local Git repository is initialized and tracking your web app files. The next step is to give your code a remote home on GitHub so that AWS services can access it later.

To push code to GitHub over HTTPS, you need a personal access token for authentication. GitHub no longer allows password-based Git operations over HTTPS. In this step, you will create your repository, generate a token, and push your code.

In this step, get ready to:
  • Create a public GitHub repository for your web app.
  • Generate a personal access token for secure authentication.
  • Push your code to GitHub and verify it arrived.
Create a new repository on GitHub
  • Sign in to GitHub in your browser.
  • Click the + icon next to your profile picture (top-right corner) and select New repository.
  • Under Owner, select your GitHub username.
  • Under Repository name, enter nextwork-web-project.
  • For Description, enter Java web app connected to AWS for CI/CD.
  • Select Public visibility.
  • Do NOT check any of the initialization options (no README, no .gitignore, no license). Your code already exists locally.
  • Click Create repository.

GitHub shows a Quick setup page with commands for pushing an existing repository. You will use the HTTPS URL displayed on this page.

  • Copy the HTTPS URL shown on the Quick setup page. It looks like https://github.com/username/nextwork-web-project.git.

Why not initialize with a README?

If you initialize the remote repository with a README, GitHub creates a commit that your local repository does not have. This causes a history mismatch when you try to push, requiring extra steps to resolve. Starting with an empty remote keeps things clean.

Generate a personal access token for authentication

GitHub requires a personal access token instead of your account password for Git operations over HTTPS. You will generate a classic token with repository access.

  • In GitHub, click your profile picture (top-right), then click Settings.
  • In the left sidebar, click Developer settings.
  • Under Personal access tokens, click Tokens (classic).
  • Click Generate new token, then click Generate new token (classic).
  • In the Note field, enter EC2 DevOps Challenge access.
  • Set Expiration to 7 days.
  • Select the repo scope checkbox. This grants full repository access.
  • Click Generate token.
  • Copy your token immediately and save it somewhere safe. You will not be able to see it again after leaving this page.

Save your token now

GitHub only displays the token value once, at the moment of creation. If you navigate away without copying it, you will need to generate a new token. Paste it into a text file or password manager before continuing.

What is a personal access token?

A personal access token is a credential that replaces your GitHub password for command-line Git operations. It has a defined scope (what it can access) and an expiration date, making it more secure than a static password.

The repo scope gives the token permission to read and write to your repositories. The 7-day expiration means it will stop working automatically after this challenge is over.

Connect your local repo to GitHub and push
  • Link your local repository to GitHub by running this command:
git remote add origin [[REPO_URL="https://github.com/your-username/nextwork-web-project.git"]]

What does this command do?

The git remote add origin command tells Git where your remote repository lives. origin is the conventional name for your primary remote. The URL points to the GitHub repository you just created.

  • Stage all your files by running:
git add .
  • Commit your code by running:
git commit -m "Initial commit - NextWork web app"

What do these commands do?

The git add . command stages every file in your project directory, telling Git to include them in the next commit. The git commit command saves a snapshot of your staged files with a descriptive message.

  • Push your code to GitHub by running:
git push -u origin master

What happens during the push?

Git will prompt you for credentials. When asked for your username, enter your GitHub username. When asked for your password, paste your personal access token (not your GitHub account password).

Nothing will display as you paste the token. This is normal terminal security behavior. Press Enter after pasting.

  • Store your credentials so Git remembers them for future pushes by running:
git config --global credential.helper store

What does credential.helper store do?

This tells Git to save your username and token locally so you do not have to re-enter them on every push. This is acceptable for a short-lived learning project on a personal EC2 instance.

  • Refresh your GitHub repository page in your browser.
  • You should see your web app files (src/, pom.xml) and your commit message "Initial commit - NextWork web app".

Push failed or seeing an error?

If you see an authentication failure, double-check that you pasted your personal access token (not your GitHub password) when prompted. The token is the long string you copied earlier.

If your local branch is named main instead of master, run git branch to check your branch name. If it shows main, use git push -u origin main instead.

If you see a "remote origin already exists" error, your remote was already added. Run git remote -v to check the current URL, then continue with the git add and git commit steps.

Help me troubleshoot my git push to GitHub

Your code now lives in GitHub. Next up, you will create an AWS CodeConnection that formally links your GitHub account to your AWS account. This is the bridge that allows AWS services to detect and pull your code automatically.

Create an AWS CodeConnection to GitHub

Your web app code is now living in GitHub with a proper commit history. The next piece of the puzzle is connecting your AWS account to GitHub so AWS services can access your repository directly.

AWS CodeConnections is the formal bridge between your AWS account and external source providers like GitHub. Without this connection, services like CodePipeline have no way to detect code changes or pull your source code automatically.

In this step, get ready to:
  • Navigate to the AWS Developer Tools console.
  • Create a CodeConnection resource linked to your GitHub account.
  • Verify the connection status is Available.
Navigate to the Developer Tools console

The Connections page lives inside the AWS Developer Tools console. You can get there by pasting a direct URL or searching in the console.

  • Return to the AWS Management Console in your browser.
  • Paste the following URL into your browser address bar to go directly to the Connections page:
https://console.aws.amazon.com/codesuite/settings/connections

Alternative navigation path

You can also search for CodePipeline in the AWS console search bar, then select Settings from the left navigation and click Connections.

  • Make sure you are in the same AWS Region you used for your EC2 instance. Check the Region selector in the top-right corner of the console.

Why do we need a CodeConnection?

Pushing code to GitHub with Git is one thing. But for AWS services to automatically react to your pushes (triggering builds, deploying new versions), they need a formal, authorized link to your GitHub account.

A CodeConnection is that formal link. It installs a GitHub App on your account so AWS can listen for changes without needing your personal access token.

Create the connection

Now you will create a new connection resource that links your AWS account to your GitHub account through the AWS Connector for GitHub App.

  • Click Create connection.
  • Under Select a provider, choose GitHub.
  • In the Connection name field, enter nextwork-github-connection.
  • Click Connect to GitHub.

A new window or section appears asking you to authorize the AWS Connector for GitHub.

  • Click Authorize AWS Connector for GitHub.
  • Under GitHub Apps, click Install a new app.

What if I already have the AWS Connector installed?

If you previously installed the AWS Connector for GitHub App on your account, you will see it listed in the GitHub Apps dropdown instead. Select it and skip the installation step below.

  • On the GitHub page that opens, select your GitHub account (the one where you created the nextwork-web-project repository).
  • Leave the default setting (All repositories) selected.
  • Click Install.
  • You are returned to the AWS console. Your GitHub App installation now appears in the GitHub Apps dropdown.
  • Click Connect.

What just happened?

You installed a GitHub App called AWS Connector for GitHub on your GitHub account. This app gives AWS permission to read your repositories and listen for code changes.

Only one app is installed per GitHub account, no matter how many AWS connections you create later.

Verify the connection status

After clicking Connect, you are taken back to the Connections list page.

  • Find your new connection nextwork-github-connection in the list.
  • Confirm the Status column shows Available. This means the handshake between AWS and GitHub completed successfully.
  • Note the Connection ARN displayed in the list. This ARN is what you will reference in CodePipeline (Day 7) to tell AWS where to pull source code from.

What is a Connection ARN?

An ARN (Amazon Resource Name) is a unique identifier for any resource in AWS. Your connection ARN looks something like arn:aws:codeconnections:us-east-1:123456789012:connection/abc-123.

When you build a CI/CD pipeline later, you will paste this ARN so the pipeline knows which connection to use for pulling your source code.

Status showing Pending instead of Available?

If your status shows Pending instead of Available, the GitHub App authorization did not complete. Click on your connection name, then click Update pending connection to retry the GitHub authorization flow.

Make sure pop-ups are not blocked in your browser, as the GitHub authorization opens in a new window.

Help me fix my CodeConnection status

Your AWS account now has a formal link to your GitHub repository. Next up, you will test the entire flow by making a code change, pushing it to GitHub, and confirming everything is wired together.

Verify the End-to-End Flow

Your AWS CodeConnection is live and your code is on GitHub. The final question is: does the entire chain actually work together?

In this step, you will make a code change on your EC2 instance, push it to GitHub, and confirm that your AWS CodeConnection remains healthy. This proves your source-to-cloud link is ready for automation.

In this step, get ready to:
  • Make a code change to your web app on EC2.
  • Push the change to GitHub using Git.
  • Confirm the change is visible in GitHub and your AWS CodeConnection remains Available.
Make a code change and push it

The best way to test a pipeline foundation is to push a real change through it. You will edit your web app file and see that change flow all the way to GitHub.

  • In VS Code (connected to your EC2 instance), open src/main/webapp/index.jsp from the Explorer sidebar.
  • Add the following line below your existing content:
<p>AWS CodeConnection is live - my pipeline foundation is ready!</p>

What does this line do?

This is a simple HTML paragraph that acts as a visible marker. When you see it in GitHub, you know your full push workflow is functioning correctly.

  • Save the file by pressing Cmd+S (macOS) or Ctrl+S (Windows).
Stage, commit, and push the change
  • Stage, commit, and push your change to GitHub by running these commands in the terminal:
git add .
git commit -m "Add pipeline readiness message"
git push

What do these commands do?

git add . stages all changed files for commit.

git commit -m saves a snapshot of your staged changes with a descriptive message.

git push sends your local commits to the remote repository on GitHub. Since you stored your credentials earlier, Git pushes without asking for your token again.

  • You should see output indicating the push was successful, with a line like master -> master.

Push failed or asking for credentials?

If Git asks for your username and password, enter your GitHub username and paste your personal access token as the password. Nothing displays while you paste. This is normal.

If you see a "remote rejected" error, confirm your personal access token has the repo scope selected and has not expired.

help me troubleshoot why my git push to GitHub is failing

Confirm visibility in GitHub and AWS

Now you will verify both sides of the connection. Your code change should appear in GitHub, and your AWS CodeConnection should remain healthy.

  • Switch to your browser and refresh your GitHub repository page for nextwork-web-project.
  • Click into src/main/webapp/index.jsp.
  • Confirm your new line appears and the commit message shows Add pipeline readiness message.

What should I see?

Your index.jsp file should now contain the pipeline readiness message you just added. The commit history should show two commits: your initial commit from earlier and the new one.

  • Return to the AWS Management Console.
  • Navigate to the Developer Tools console at https://console.aws.amazon.com/codesuite/settings/connections.
  • Confirm your connection nextwork-github-connection still shows a status of Available.

Why check the connection status again?

An Available status confirms the handshake between AWS and GitHub is still intact. This connection is what CodePipeline (Day 7) will reference to automatically pull your code whenever you push changes.

The Connection ARN displayed on this page is the unique identifier that AWS services use to locate your GitHub connection.

Connection not showing Available?

Make sure you are in the same AWS Region you used when creating the connection. Connections are region-specific.

If the status shows something other than Available, try clicking the connection name to check its details. You may need to re-authorize the AWS Connector for GitHub App.

help me troubleshoot my AWS CodeConnection status

You now have a verified end-to-end flow. Code changes on your EC2 instance push to GitHub, and AWS can access that repository through your CodeConnection.

Secret mission

Set Up a Git Branching Workflow

Real DevOps teams never commit directly to the main branch. Set up a develop branch, make a feature change, push it to GitHub, and merge it back into master. This is the exact branching pattern used in professional CI/CD workflows.

Clean Up Your Resources

Clean Up Your Resources

Decide whether to keep your resources running, pause them to come back later, or delete them entirely. Your EC2 instance is the only resource with potential running costs (covered by the free tier while eligible). Your AWS CodeConnection, GitHub repository, and personal access token are all free.

Resources you used:

  • EC2 instance (t2.micro, free tier eligible, running costs apply while the instance is in a Running state).
  • AWS CodeConnection nextwork-github-connection (no cost).
  • GitHub repository nextwork-web-project (free).
  • Personal access token (free, expires in 7 days automatically).

Keep everything running

Choose this if you plan to continue to Day 3 (Store Dependencies in CodeArtifact) tomorrow. Your CodeConnection and GitHub repo persist at no cost.

  • Stop your EC2 instance to avoid running charges while you are away. In the EC2 console, select your instance, click the Instance state dropdown, and select Stop instance.
  • Click Stop when prompted for confirmation.
  • Your CodeConnection, GitHub repository, and personal access token all remain in place at no cost.

Pause - I'll come back to this later

Stop your EC2 instance to free up running charges. Everything else remains in place for free and is ready when you return.

  • In the AWS Management Console, navigate to EC2 and select Instances from the left navigation.
  • Select the checkbox next to your instance.
  • Click the Instance state dropdown and select Stop instance.
  • Click Stop when prompted for confirmation.
  • Your CodeConnection, GitHub repository, and personal access token remain intact. When you are ready to return, start your instance again and reconnect VS Code with the new Public IPv4 DNS.

Delete - I don't want to use this again

Remove all project resources and start fresh. Follow each step below to fully clean up.

  • Terminate your EC2 instance:
  • In the EC2 console, select Instances from the left navigation.
  • Select the checkbox next to your instance.
  • Click the Instance state dropdown and select Terminate (delete) instance.
  • Click Terminate (delete) when prompted for confirmation.
  • Delete your AWS CodeConnection:
  • Open the Developer Tools console at https://console.aws.amazon.com/codesuite/settings/connections.
  • Select Settings then Connections.
  • Click the connection name nextwork-github-connection.
  • Click Delete.
  • Type delete in the confirmation field and click Delete.
  • Delete your GitHub repository:
  • On GitHub, navigate to your nextwork-web-project repository.
  • Click Settings under the repository name.
  • Scroll down to the Danger Zone section and click Delete this repository.
  • Click I want to delete this repository.
  • Click I have read and understand these effects.
  • Type the repository name to confirm, then click Delete this repository.
  • Delete your personal access token:
  • On GitHub, click your profile picture and select Settings.
  • In the left sidebar, click Developer settings.
  • Under Personal access tokens, click Tokens (classic).
  • Find the token named EC2 DevOps Challenge access and click Delete.
  • Confirm the deletion when prompted.

That's a Wrap!

That's a Wrap!

Nice work! You've just wired the foundational source control layer that every CI/CD pipeline depends on.

Your Java web app now lives in GitHub, and your AWS account has a formal CodeConnection ready for automated workflows.

You've learned how to:

  • Set up Git version control on a cloud EC2 instance and pushed your Java web app to a GitHub repository with personal access token authentication.
  • Created an AWS CodeConnection using the AWS Connector for GitHub App, formally linking your GitHub account to your AWS account.
  • Verified the end-to-end source-to-cloud flow by pushing a code change from EC2 to GitHub and confirming AWS can access it through the CodeConnection.
  • (Secret Mission) Practiced a Git branching workflow with a develop branch, feature commit, and merge back to master.

Ready to quiz yourself?