terraform-ansible-setup

Setting up your complete infrastructure on cloud premises using Infrastructure as a Code

This project is maintained by ramitsurana

Terraform-ansible-setup

Setting up your complete infrastructure on cloud premises using Infrastructure as a Code

Prerequisites

Cloud Requirements Operating System Region
AWS Nil Ubuntu 14.04 (ami-21766642) ap-southeast-2
GCP gcloud cli, Apache-Libcloud(==1.2.0) Ubuntu 14.04 (ubuntu-1404-trusty-v20170703) us-east1-b
Azure Coming Soon    

Design

The idea of this project is to make easy deployment of our infrastructure using the concept of Infrastructure as a Code.For this purpose,I have used 2 tier architecture setup consisting of two different tools, Terraform and Ansible.Terraform is used in order to provision the required instances on Cloud.Whereas, ansible is used to configure our application.

arch2

Setup

Terraform

For getting started with ansible,choose your cloud premise:

#### AWS-Terraform

For AWS usage you can create a universal Access and Secret Key ID at

IAM Console -> Access Keys (Access Key ID and Secret Access Key)

Replace your Access ID and Secret Key ID here

The main.tf file contains the provisioning of ec2 instance including creation of security group.

##### GCP-Terraform

  1. Like in AWS,we have security credentials for accessing unlimited resources,in gcp we can use a security key in JSON format in order to use the resources.This can be generated by visiting

Google Cloud Dashboard -> IAM & Admin -> Service Accounts -> Choose a Service Account -> Options -> Create Key

  1. Download this json key and keep it under

terraform-ansible-setup -> GCP -> YOUR-ACCOUNT-ID.JSON

For my reference,I have name it as account.json in my variables.tf file

  1. Connect your terminal with gcp via ssh using the following command:
$ sudo cat ~/.ssh/id_rsa.pub
  1. Copy and Paste the above output at

Google Cloud Dashboard -> Compute Engine -> Metadata -> SSH Keys -> Add New Key

  1. Install the gcloud cli using :
$ curl https://sdk.cloud.google.com | bash
  1. Make sure to authorize permissions:
$ sudo chown -R ${USER} /home/${USER}/.config/gcloud
  1. Use the below command to verify gcloud cli with your account:
$ sudo gcloud auth login

You can now check gcloud cli working by running any of the gcloud available commands such as gcloud compute machine-types list etc.

  1. Now you can run your commands to kickstart 3 vm instaces (sample1,sample2,sample3) using
$ terraform plan
$ terraform apply

Connecting the Two

One common thing that we need in order to run our playbooks is the ip’s and tags associated with it in our dynamic inverntory (hosts file)

Ansible

You can start by setting up your aws enviornment EC2 instance using ec2-configure.yml playbook present in playbooks directory,using the below command:

  $ sudo ansible-playbook ec2-configure.yml -vv --private-key  <path-to-keypair>

In order to start using ansible we have to export gcloud variables to our local Bash profile.Using this shell script we can easily export variables gcloud variables and gcloud cli.This script is originally available here.

$ source export.sh

License

MIT License