Terraform Provisioning
- Provisioners can be used to model specific actions on the local machine or on a remote machine in order to prepare server or other infrastructure objects at scale.
- Concept of provisioning
- Provisioner types:
- file
- local-exec
- remote-exec
- chef
- salt
- puppet
- Refer Here
Activity: Lets install the following on the web vm
- Lets install apache server
sudo apt update
sudo apt install apache2 -y
- Lets do this in Azure (/AWS)
- Lets create connection
- Refer Here for the changes done to install apache2 on web vm and conditionally create app and db servers
- Situation1: To get the public ip from vm, we had to go to azure portal. Refer Here for the output documentation. Refer Here for the changes done
- Situation 2: We need to change the script and execute the installation of some other software
- The VM is already created
- Lets change the script and rexecute
- The change in script of provisioning is not considered as change
- Solution 1 to above problem:
- Whenever you change any thing in script, recreate the virtual machine
- We need to mark virtual machine for deletion during next apply this is referred as taint in terraform.
- Lets taint the vm and execute apply
- Solution 2: Execute provisioning all the times when you execute apply
- But terraform executes provisioner only when resource is created.
- Terraform has a resource called as null resource which it tries to create during apply and we run the provisioner over there
- One option is to mark the null resource for taint before apply (or when the provisioining needs to happen)
- other option is to use triggers
- Refer Here for the changes done.
- Exercise:
- For the people aware of ansible try to install apache server from ansible playbook in terraform provisioning
- Create the provisioner to install tomcat on appserver
- Made some subtle changes and we have the following output, Refer Here for the changes
Activity: Create a reusable terraform configuration to create ntier
- Terraform modules can help in creating reusable configuration Refer Here
Situation 1: Lets try to use existing modules
- A lot of opensource contributers create reusable terraform configuration as modules and host them on various sources.
- Terraform registry is the popular one
- Lets try to use a module created by someone to create a network in Azure Refer Herefor the network module
- Refer Here for the changeset
- The source code of the module which we have used is Refer Here
- It is exactly similar what we have been developing
- A Module is a collection of terraform files
- input variables become arguments
- output variables become attributes of the module
Situation 2: Lets try to create module from our configuration
- Refer Here for the module which we added
- Structure recommendations of module from terraform Refer Here
- We have used the source which is present in the same machine and we can use this source to access any drive (local/network) to gain access to reusable modules
Situation 3: How to make modules to be used by all of our team members
- One way is make them available on network file shares and access them
- We can use any of the module sources Refer Here
- Create a git repository for every module which devops team creates Refer Here
- for using module from git we have created the following changeset Refer Here
- Now execute init and apply