This Terraform module provisions an EC2 instance on AWS, configures a security group, and uses Ansible to install and configure Nginx. The module is designed to be reusable and customizable.
- EC2 Instance: Creates an EC2 instance with a specified AMI, instance type, and key pair.
- Security Group: Configures a security group to allow SSH and custom port access.
- Ansible Provisioning: Uses Ansible to install and configure Nginx & deploy static file on the EC2 instance.
- Outputs: Provides the public IP address of the EC2 instance.
Before using this module, ensure you have the following:
- Terraform Installed
- Ansible Installed
- AWS CLI Configured
- SSH Key Pair
ami_id = "The AMI ID for the EC2 instance (e.g., Ubuntu 22.04)."
aws_instance_name = "The name tag for the EC2 instance."
aws_region = "The AWS region where the resources will be created (e.g., us-east-1)."
custom_port = "The custom port to open in the security group (e.g., 80 for HTTP)."
instance_type = "The instance type for the EC2 instance (e.g., t2.micro)."
key_pair_name = "The name of the SSH key pair to associate with the EC2 instance."
security_group_name = "The name of the security group to create."
public_key_path = "The path to the public SSH key file (e.g., ~/.ssh/id_rsa.pub)."
private_key_path = "The path to the private SSH key file (e.g., ~/.ssh/id_rsa)."
playbook_file_path = "The path to the Ansible playbook file (e.g., ./ansible/playbooks/playbook.yml)."
public_ip = "The public IP address of the EC2 instance."- Clone the Module Repository
https://github.com/Dawood-Usman/terraform-ansible-example.git
cd terraform-ansible-example- Configure a Terraform
main.tffile with your input values
provider "aws" {
region = "us-east-1"
}
module "ec2_nginx" {
source = "github.com/dawood-usman/terraform-ansible-example//module"
ami_id = "ami-0e1bed4f06a3b463d" # Ubuntu 22.04
aws_instance_name = "nginx-server"
aws_region = "us-east-1"
custom_port = 80
instance_type = "t2.micro"
key_pair_name = "nginx-key"
security_group_name = "nginx-nsg"
public_key_path = "~/.ssh/id_rsa.pub"
private_key_path = "~/.ssh/id_rsa"
playbook_file_path = "./ansible/playbooks/playbook.yml"
}
output "public_ip" {
value = module.ec2_nginx.public_ip
}- Optionally, you can define the input variables in a
terraform.tfvarsfile
ami_id = "ami-0e1bed4f06a3b463d" #ubuntu 22.04
aws_instance_name = "nginx-server"
aws_region = "us-east-1"
custom_port = 80
instance_type = "t2.micro"
key_pair_name = "nginx-key"
security_group_name = "nginx-nsg"
public_key_path = "~/.ssh/id_rsa.pub"
private_key_path = "~/.ssh/id_rsa"
playbook_file_path = "./ansible/playbooks/playbook.yml"- Initializes a Terraform configuration in your working directory
terraform init- Formats Terraform configuration files to use standardized indentation
terraform fmt- Validate Terraform configuration files for syntactical and structural errors
terraform validate- Review and confirm the expected changes
terraform plan- Finally deploy resources on aws
terraform apply- On Browser
http://ec2-public-ip:80- On Terminal
ssh ubuntu@ec2-public-ipAWS bills wait for no one! If you're just practicing, make sure to destroy your resources when you're done. Otherwise, you might end up with a bill that makes you question your life choices.
terraform destroyContributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.