This doc will take you to setting up two webservers, classic loadbalancer and bastion host using Terraform and Ansible.
here I have used Ubuntu linux flavour for webs and bastion server.
Lets assume already we have a server with Terraform and Ansible. It should configured with aws access and secret keys.
This environment will be created on us-east-2 region. the region has beed mentioned at variables.tf.
The instance.tf file is a main file which is responsible to creating all the resources(webservers and bastion server), ansible dynamic inventory, dynamic ssh configuration file and triggering ansible playbook once all the resources has been created.
before triggering instance.tf file we need a public and private key to craete a keypair in aws. it's available in the start.sh file.
there is a playbook(nginx_install.yml) in ansible folder which is used to install nginx server, staging index.html and starting nginx service.
I have kept 2 templating file to handle dynamic configurations.
- Enter into the template directory $ cd Alation_Demo/tf_templates/
- enable execute permision for start.sh $ chmod +s start.sh
- run the script $ ./start.sh
- there will be few prompt to answer. while creating public/private key enter with empty passphrase, while ansible playbook execution give yes for handshake.
Pros
- Dynamic inventory
- Dynamic ssh configuration file which will help to ansible navigate the private subnet instance using bastion host.
Future improvements
- we can improve lot in ansible playbook when we have real time example.
- formation of terraform is not good. for time being I have kept like this. we can split into multiple file or we can use modules if necessary
Output Following details will be shown in the output,
- bastion host's public ip
- webserver's private ips
- classic load balancer URL
Thank You ! For Reading.