iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😸

Run Claude Code on EC2 Without an API Key and Launch from GitHub Actions

に公開

I usually carry a MacBook around to work. So, even if I want to leave a task that takes several hours to an AI agent, running it locally continuously is a bit painful.

Moreover, running it via the API makes me worry about usage-based charges. Since I already use Claude Max / Pro, I wondered if I could use the authentication from my subscription plan directly in the cloud instead of using an API Key. This was the idea behind the configuration I tried this time.

By the way, as a side note, I ended up paying over 10,000 yen on Claude Code last weekend...

A tool that is too convenient feels like magic when you're using it, but the bill brings you back to reality abruptly.
It's unheard of to have financial hardship from overusing Claude Code, so I decided to experiment a bit 😮‍💨

What I did is simple. I ran Claude Code on an EC2 instance, logged in using my Claude subscription plan, and registered that EC2 as a self-hosted runner for GitHub Actions.

This way, I can use my own node as a runner in the cloud.

Why I wanted to run Claude Code on EC2

What I wanted to do was to use GitHub as the entry point for an AI agent development workflow.

Specifically, I wanted to trigger a Ralph Loop when an issue is created or PRD / specifications are updated, and have implementation and code review proceed on the cloud side.

For this, Claude Code needs to be running on the cloud, not locally, and be launchable from GitHub Actions.

However, running it via the API every time makes me concerned about usage-based fees. Since I already use Claude Max / Pro, the starting point of this attempt was to see if I could use the authentication from that subscription plan on the cloud side as well.

The goals were the following two:

  • Run Claude Code on the cloud without additional API charges
  • Use GitHub as the entry point to run implementation and code review via Ralph Loop

Difference between API Key and subscription plan authentication

Here's a rough summary.

Usage Authentication Pricing concept Position in this attempt
Use Anthropic API ANTHROPIC_API_KEY Usage-based charges according to API usage I wanted to avoid this
Use Claude Code with subscription plan OAuth login with Claude account Within the already-subscribed Claude Max / Pro plan This is what I wanted to do

What I wanted to do this time was the latter. In other words, use Claude with a subscription plan without using an API Key.

The configuration this time

The configuration is quite straightforward.

  • Set up one EC2 instance
  • Create a runner user on the EC2
  • Log into Claude Code with OAuth as that user
  • Register the same user as a GitHub Actions self-hosted runner

The EC2 setup is minimal: Ubuntu 24.04, t3.medium, 30GB, Public IP enabled, Session Manager connection.

The key point is not to separate the user that uses Claude Code from the user that runs the self-hosted runner. The authentication state of Claude Code is held per user, so if these differ, the runner won't see the authenticated state.

Actual Setup Procedure

1. Create the EC2 instance and set up the runner user

First, I launched an EC2 instance, connected via Session Manager, and created a user for the task.

sudo adduser runner
sudo usermod -aG sudo runner
sudo su - runner

This runner user is the one that logs into Claude Code and also runs the self-hosted runner.

2. Install required tools with setup.sh

Next, I ran the setup script.
Below is the repository used for verification.

git clone https://github.com/peintangos/ai-agents-usage-examples.git
cd ai-agents-usage-examples/examples/11-ec2-claude-code-oauth
chmod +x setup.sh
./setup.sh

This script installs the following:

  • Node.js
  • Git
  • tmux
  • GitHub CLI
  • Claude Code CLI

Verification is straightforward:

claude --version
node --version
tmux -V
gh --version

3. Log in to Claude Code with the subscription plan

On the EC2 instance, launch claude, then open the displayed URL in your local browser to authenticate.

claude

After authentication, you can verify with a simple command:

claude -p "Answer 1+1 with just the number."

4. Register the self-hosted runner under the same user

Next, add a self-hosted runner on the GitHub side and register it with the runner user.

mkdir -p ~/actions-runner
cd ~/actions-runner
curl -o actions-runner-linux-x64-<version>.tar.gz -L <URL from GitHub>
tar xzf actions-runner-linux-x64-<version>.tar.gz
./config.sh --url https://github.com/peintangos/ai-agents-usage-examples --token <token from GitHub>

For startup, I made it a service from the start.

sudo ./svc.sh install runner
sudo ./svc.sh start
sudo ./svc.sh status

Once you get to this point, you have a Claude Code authenticated with the subscription plan on EC2, and a runner that receives GitHub Actions jobs under the same user.

What This Configuration Achieved

The Claude Code on EC2 worked normally with the subscription plan authentication, not an API Key.

Furthermore, when registered as a self-hosted runner, the authenticated Claude Code could be used from GitHub Actions as well. In other words, even from a job that doesn't pass an API Key, we could use the Claude Code on EC2 as the entry point to run processes.

The advantages of this configuration are quite clear:

  • No need to occupy your local PC for long periods
  • You can let processing run in the cloud overnight and check the results in the morning
  • Since it leverages Claude Max / Pro, there's no need to issue a new API Key and pay usage fees
  • Using GitHub Actions as the entry point makes operations easy to organize

Where I Got Stuck

Claude Code authentication is done in your local browser

Even if you launch claude on EC2, the authentication is not completed there. The actual authentication is performed in your local browser.

It's better to assume the self-hosted runner should be service-ized

If you intend to use it as an execution platform for GitHub Actions, it's more manageable to make it a service with svc.sh from the beginning. In my environment as well, this was more stable.

Behavior changes if an API Key remains

When running directly on EC2 there was no problem, but running heavier processes via GitHub Actions failed with "Credit balance is too low". When I investigated, I found that an ANTHROPIC_API_KEY was still set as a Repository Secret.

In this experiment, it seemed that when an API Key is visible, the system uses that instead of the subscription plan authentication. At least, the behavior stabilized after removing ANTHROPIC_API_KEY.

If you're experimenting with OAuth, it's better not to mix in an API Key.

OAuth authentication might expire

I haven't verified this yet, so it's uncertain information, but when you log in, the authentication information is persisted in ~/.claude.json. It's unclear how long this authentication remains valid, so there is a possibility that re-authentication may be needed at regular intervals. If such an event occurs during actual use, I plan to consider a countermeasure.

Can the Same Thing Be Done with Codex?

In conclusion, the same configuration can be achieved with Claude, but the design philosophy for GitHub Actions integration is slightly different.

Item Claude Code Codex
Run CLI on Linux / EC2 Possible Possible
Log into CLI with subscription plan authentication Possible: browser authentication with Claude account Possible: can log in with ChatGPT account
Use CLI without API Key Possible Possible
Use from self-hosted runner under the same user Possible Configurably possible
Use official GitHub Actions integration without API Key Easier via self-hosted runner as in this setup As of March 10, 2026, official Action assumes OPENAI_API_KEY
Drive review and development flow from GitHub Works well with self-hosted runner Works well with Codex Cloud / GitHub integration

As a supplement, Codex CLI itself can log in with a ChatGPT account, so it's possible to have authentication for the same user on EC2 and use the CLI.
On the other hand, if you want to include GitHub-based operations, it's more natural to use Codex Action or Codex Cloud / GitHub integration.

Summary

The configuration of EC2 + Claude Code + subscription plan authentication + self-hosted runner was sufficiently viable.

In practice, the following benefits were confirmed:

  • Claude Code can run on EC2
  • By using the same user for the self-hosted runner, it can also be used from GitHub Actions
  • Making it a service with svc.sh makes it easy to run continuously
  • No need to keep heavy tasks running on your local machine

On the other hand, there are also some caveats:

  • Claude Code authentication must be done in a local browser
  • It's better to assume the self-hosted runner should be service-ized
  • Mixing in an API Key can cause unstable behavior

Also, I must admit I committed a bit of title deception: I specified "t3.medium" as the EC2 instance type. Please note that this is not a free tier instance type. Of course, if you use the free tier, there are no costs.

It's not a perfectly clean configuration, but as a way to leverage an already-subscribed plan to run agents in the cloud, it was quite practical.

In Closing

The GitHub-driven development flow I tried this time is based on the following template repository:

https://github.com/peintangos/ralph-matsuo

ralph-matsuo is a template for treating Markdown documents such as PRD, specifications, dependencies, progress, and todo as execution contracts, rather than running Claude Code with ad-hoc prompts.

It can serve as a foundation for driving implementation and code review through the Ralph Loop, with issue creation or PRD/specifications updates as the entry point. If you're interested in a docs-first agent development flow with GitHub as the starting point, like the one I tried in this article, the README should give you a good overview.

References

Discussion