This repository demonstrates how to deploy an AWS EC2 instance (free tier) using GitHub Actions, Terraform for infrastructure provisioning, and Ansible for configuration management..
To use this repository, you'll need:
- An AWS account with free tier access
- AWS access credentials with permissions to create EC2 instances
- A GitHub account
- SSH key pair for accessing the EC2 instance
Start by forking or cloning this repository to your GitHub account.
In your repository on GitHub, navigate to Settings > Secrets and variables > Actions and add the following secrets:
AWS_ACCESS_KEY_ID
: Your AWS access keyAWS_SECRET_ACCESS_KEY
: Your AWS secret keySSH_PRIVATE_KEY
: Your private SSH key (the entire key content including BEGIN and END lines)
You may want to update the following in the Terraform files:
- In
variables.tf
: Change default AWS region, instance type, or AMI ID - In
main.tf
: Update the security group to restrict SSH access to your IP address
When you push changes to the main branch, GitHub Actions will automatically run the workflow to deploy your infrastructure.
You can also manually trigger the workflow by:
- Going to the "Actions" tab in your repository
- Selecting the "Terraform and Ansible Deploy" workflow
- Clicking on "Run workflow"
This setup deploys:
- A t2.micro EC2 instance (free tier eligible)
- A security group allowing SSH access
- Apache web server with a simple "Hello" page
To avoid ongoing charges, remember to destroy the resources when you're done:
- You can add a destroy workflow or run
terraform destroy
locally - Alternatively, delete the resources manually from the AWS Console
This example includes some basic security practices:
- Using GitHub Secrets for credential management
- Creating a restrictive security group
- Setting up firewall rules with Ansible
For a production environment, consider additional measures:
- Use AWS IAM roles with least privilege
- Implement AWS Config rules for compliance
- Set up CloudTrail for auditing
- Use VPC with private subnets where possible