
Steps to create a fine-grained personal access token
- In the top-right of GitHub, click your profile image and choose Settings.
- In the Settings sidebar, scroll down and click Developer settings.
- Under Developer settings, select Personal access tokens → Fine-grained tokens.

- Name: a descriptive label (for example,
Terraform training). - Resource owner: your account (or the organization you manage).
- Expiration: choose an appropriate duration (for short labs use 7 days; for automation choose longer but rotate regularly).
- Repository access: choose the repositories this token may access (for hands-on labs you can select
All repositories). - Repository permissions: grant the minimum permissions Terraform needs. At minimum, enable:
Administration= Read & write (for creating/deleting repositories and changing settings)Contents= Read & write (for modifying files in repositories)

Copy the token immediately after generation — GitHub will not show it again after you leave this page.
Recommended repository permissions for common Terraform tasks
Grant only the permissions you need and restrict repository access where practical.
Add the token to your environment
Set the token as theGITHUB_TOKEN environment variable so Terraform’s GitHub provider can authenticate automatically.
- macOS / Linux (temporary for current shell session):
- Windows PowerShell (temporary for current session):
- Windows PowerShell (persist across future sessions using setx):
github_pat_<YOUR_TOKEN_HERE> with the token value you copied from GitHub.
Fine-grained tokens can expire or be revoked. If the token expires, generate a new one and update your environment variable. Keep tokens secret — do not commit them to source control or reveal them in logs.
How Terraform uses the token
OnceGITHUB_TOKEN is set, the Terraform GitHub provider will read it automatically. You do not need to hard-code the token in your Terraform configuration.
Example provider block (no token value shown — provider uses the environment variable):
Troubleshooting & best practices
- If Terraform reports authentication errors, verify
GITHUB_TOKENis exported in the same shell/session running Terraform. - If permission errors occur, confirm the token’s repository permissions and repository access settings.
- For CI systems, store the token in the CI secret store and inject it as
GITHUB_TOKENat runtime. - Rotate tokens regularly and use short expirations for temporary training or lab environments.
Links and references
- GitHub: Create a personal access token
- Terraform GitHub provider documentation
- GitHub fine-grained personal access tokens overview