Skip to content

Terraform and Ansible Config Files for Automated Provisioning of a VPS

License

Notifications You must be signed in to change notification settings

KevJimenez/IaC-for-Automation

Repository files navigation

Linter Status

Ansible Linter Terraform Linter

Automated Virtual Private Server Guide

Overview

An automated setup of infrastracture with configuration for a web server (uses Amazon EC2 as VPS) using Terraform (IaC) and Ansible (CaC). Can be used to instantly deploy or destroy the web server and the configuration files are checked in to a GitHub repository. Used Github Actions that is run on a local Ubuntu instance for the automation of the whole process. The web server made in this project hosts my personal portfolio site.

Diagrams:

  • IaC-for-Automation

iac

whole

The Workflow

Preparation

  1. Terraform Configuration Files
    • Made 3 tf files:
      • main.tf - contains all code related to AWS, Terraform Cloud, required providers (EC2 instance provisioning, ssh http https access, ssh key pair for host pc)
      • dnset.tf - cloudflare configuration (A-NAME Record, Cloudflare Account)
      • variables.tf - variables for referencing (variable values checked in to Terraform Cloud for security purposes)
  2. Ansible Configuration Files:
    • Made 3 yaml files:
      • playbook.yml - contains code for the whole CaC in the infrastructure provisioned by Terraform (Update Instance, Installing Docker, Login to Docker, Running Docker Container Watchtower and Docker Image of Personal-Portfolio)
      • destroyinf.yml - code for the uninstallation of files present in the instance (Clean apt directories and cache, Uninstall Docker)
      • aws_ec2.yml - contains code for a dynamic inventory in my AWS account
  3. GitHub Action Workflows:
    • Made 4 workflows:
      • ansiblelint.yml - workflow for ansible-lint (triggers per push into the repository)
      • tflint.yml - workflow for terraform-lint (triggers per push into the repository)
      • deploy.yml - workflow for deploying ec2 instance (triggers on dispatch)
      • destroy.yml - workflow for destroying ec2 instance (triggers on dispatch)

Workflow Process

Note: Workflow runners for deploy and destroy is not running on a GitHub Container. Runs on my Local Ubuntu Instance with permissions from AWS. GitHub Container runners also can't detect yaml as an inventory for ansible, hence used a local runner instead.

Deploy Infrastructure (deploy.yml)

  1. Terraform Init (Checks for Terraform Files)
     terraform init
  2. Terraform Apply (Building of the Instance)
    terraform apply -auto-approve -input=false
  3. Delay for 7s (Added delay because EC2 dynamic inventory takes time to detect newly built instance for the ansible playbook)
    sleep 7s
  4. Run Ansible Playbook (referenced github secrets for docker access key)
    ansible-playbook -i aws_ec2.yml playbook.yml -e "docker_key=${{ secrets.DOCKER_KEY }}"

Output:

Destroy Infrastructure (destroy.yml)

  1. Run Ansible Playbook for Uninstallation (Removes installed programs)
     ansible-playbook -i aws_ec2.yml destroyinf.yml
  2. Terraform Init (Checks Terraform Files)
    terraform init
  3. Terraform Destroy (Destroys running instance)
    terraform destroy -auto-approve -input=false

Output:

  • Destroys instance that is made with deploy.yml

Ansible and Terraform Linter (ansiblelint.yml and tflint.yml)

About

Terraform and Ansible Config Files for Automated Provisioning of a VPS

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages