Skip to content

tooniez/terraform-ansible-aws

Folders and files

NameName
Last commit message
Last commit date

Latest commit

c29c001 Β· Sep 4, 2024

History

31 Commits
Aug 27, 2024
Sep 4, 2024
Sep 3, 2024
Aug 27, 2024
Aug 27, 2024
Sep 4, 2024

Repository files navigation

Terraform Ansible AWS Example 🌐

Quick start guide for provisioning with Ansible inside Terraform πŸ‘

This project demonstrates how to use Terraform to provision AWS infrastructure and Ansible to configure the provisioned instances.

Project Structure πŸ“

  • ansible/: Playbooks, inventories, and configuration
  • terraform/: Infrastructure files and modules

Getting Started πŸ”₯

Before using this project:

  1. Install boto for dynamic inventory with AWS EC2:
    $ pip install boto
    $ chmod +x ansible/dynamic_inventory.sh
    
  2. Set up your AWS credentials (e.g., using environment variables or AWS CLI configuration)

Usage πŸ’»

  1. Set variables in terraform/terraform.tfvars (copy from terraform.tfvars.example):
    • pub_key_path
    • private_key_path
    • key_name
  2. Initialize Terraform:
    $ cd terraform && terraform init
    
  3. Plan the infrastructure:
    $ terraform plan
    
  4. Apply the Terraform configuration:
    $ terraform apply
    
  5. Run Ansible playbooks:
    $ cd ../ansible && ansible-playbook -i dynamic_inventory.sh site.yml
    
  6. Destroy the infrastructure:
    $ terraform destroy
    

Terraform Structure πŸ“

Main Components

  • main.tf: Defines the main infrastructure (AWS provider, modules)
  • variables.tf: Declares input variables
  • outputs.tf: Specifies output values
  • data.tf: Defines data sources (e.g., AMI lookup)
  • backend.tf.example: Example backend configuration for remote state

Modules 🧩

  • base: Defines base security group
  • key_pair: Manages SSH key pair
  • web: Provisions web servers
  • db: Provisions database servers
  • vpc: Creates and configures the VPC
  • alb: Sets up Application Load Balancer
  • asg: Configures Auto Scaling Group
  • rds: Provisions RDS database
  • s3: Creates and configures S3 bucket
  • cloudfront: Sets up CloudFront distribution
  • route53: Manages Route53 DNS records
  • iam: Configures IAM roles and policies
  • kafka: Provisions an Amazon MSK (Managed Streaming for Kafka) cluster
  • sqs: Creates an Amazon SQS (Simple Queue Service) queue

Each module has its own main.tf, variables.tf, and outputs.tf files.

Ansible Structure 🎭

  • ansible.cfg: Ansible configuration file
  • site.yml: Main playbook that includes other playbooks
  • dynamic_inventory.sh: Script that retrieves tfstate from S3 to generate inventory
  • playbooks/:
    • docker.yml: Playbook for Docker installation
    • mysql.yml: Playbook for MySQL installation
    • apache.yml: Playbook for Apache installation
    • portainer.yml: Playbook for Portainer installation
    • tailscale.yml: Playbook for Tailscale installation

Infrastructure Overview πŸ—οΈ

This project sets up the following AWS resources:

  • VPC and security groups
  • EC2 instances for web and database servers
  • SSH key pair for instance access

Ansible Configuration πŸ”§

Ansible is used to configure the provisioned instances:

  • Apache: Installs and configures Apache
  • Mysql: Installs and configures MySQL
  • Docker: Installs Docker and sets up containers
  • Portainer: Installs Portainer for container management
  • Tailscale: Installs Tailscale for secure networking

Important Notes πŸ“Œ

  • Ensure your AWS credentials are properly configured
  • Review and adjust security group rules as needed
  • Customise Ansible playbooks to fit your specific requirements

License πŸ“

MIT licensed by tooniez

About

Using Terraform and Ansible to provision AWS infrastructure

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published