This bundle contains two separate repositories and CI workflows that demonstrate a modern SSH-less flow:
terraform-repo/— Terraform code to create EC2 instances with SSM enabled and tags.ansible-repo/— Ansible playbook + dynamic inventory; workflow listens for repository_dispatch to run playbooks.
Default AWS region: ap-south-1 (Mumbai). Default AMI: Amazon Linux 2 (most recent).
- Terraform code that provisions instances and tags them:
terraform-repo/ - GitHub Actions in Terraform repo that runs
terraform applyon push and triggers arepository_dispatchevent against the Ansible repo. - Ansible repo that runs the playbook when it receives the
run-ansiblerepository_dispatch event. - README instructions below for secrets and PAT setup.
- Clone and push each folder into separate GitHub repositories (see steps below).
- In Terraform repo: edit
terraform.tfvars, runterraform initandterraform applylocally OR push to GitHub main to trigger the workflow. - Ensure Ansible repo secrets are set (see next section). When Terraform workflow finishes it will dispatch to Ansible repo to run playbook.
Set these repository secrets:
AWS_ACCESS_KEY_ID(for terraform apply in workflow)AWS_SECRET_ACCESS_KEYANSIBLE_REPO_PAT— a GitHub Personal Access Token withreposcope (for private repos) or minimal scopes to allow repository_dispatch.ANSIBLE_REPO— owner/repo of the Ansible repository, e.g.myorg/ansible-repo
Why: the Terraform workflow uses ANSIBLE_REPO_PAT to POST the repository_dispatch event into the target repo.
Set these repository secrets:
AWS_ACCESS_KEY_ID(for Ansible run)AWS_SECRET_ACCESS_KEY
Why: the Ansible workflow needs AWS credentials to reach EC2/SSM during the playbook run.
- Push to Terraform repo
main→ workflow runsterraform apply(uses AWS creds from TF repo secrets). - On success, workflow calls GitHub API with
ANSIBLE_REPO_PATand postsrepository_dispatchto theANSIBLE_REPOwith event typerun-ansible. - Ansible repo receives the repository_dispatch event and triggers its workflow which checks out code, installs Python/Ansible, and runs the playbook using AWS creds from Ansible repo secrets.
- Store credentials and PAT in GitHub repository secrets, not in code.
- Limit PAT scopes to minimum required for
repository_dispatch(repo scope might be required for private repos). - Prefer using organization or machine accounts for automation rather than personal PATs.