Giter Club home page Giter Club logo

iac-for-automation's Introduction

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)

iac-for-automation's People

Contributors

kevjimenez avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.