ABOUT TERRAFORM PROVISIONERS
Provisioners are specific built-in components that allow you to execute scripts on a local or remote machine as part of the resource creation, destruction process or cleanup process before destruction.
- AWS Free tier account
- Linux server with terraform installed.
steps / instructions
- Login to the AWS linux server and create directory with name mkdir TFProvisioners
- Change to the directory TFProvisoners
- Run the git clone command
git clone https://github.com/devops81/TFProvisoners.git
- Once clone is completed we will get below three files
- If we examine the code under main.tf file
- under the resource block we are creating AWS VM named webserver
- We are passing a number of parameters for the resource, such as the AMI that the VM will be spun up as, the instance type, the private key that the instance will be using, the public IP attached to the instance, the security group applied to the instance, and the subnet ID where the VM will be spun up
- If we examine the provisioner block
remote-execkeyword tells us that this is a remote provisioner, which invokes a script on a remote resource after it is created.
- The provisioner is using the parameters configured in the embedded
connectionblock to connect to the AWS EC2 instance being created.
- The provisioner will then issue the commands configured in the
inlineblock to install Apache webserver on CentOS through the
yumpackage manager, start up the Apache server, create a single web page called My Test Website With Help From Terraform Provisioner as an
index.htmlfile, and move that file into the data directory of the webserver to be served out globally.
STEPS TO DEPLOY THE CODE AND ACCESS THE DEPLOYED WEB SERVER
- Initialize the Terraform working directory, and download the required providers:
- Validate the code to look for any errors in syntax, parameters, or attributes within Terraform resources that may prevent it from deploying correctly:
terraform validateYou should receive a notification that the configuration is valid.
- Review the actions that will be performed when you deploy the Terraform code:
terraform planIn this case, it will create
7resources as configured in the Terraform code.
- Deploy the code:
- When prompted, type yes, and press Enter.
- As the code is being deployed, you will notice that the Terraform provisioner tries to connect to the EC2 instance, and once that connection is established, it will run the bootstrapping that was configured in the
provisionerblock against the instance.
- When complete, it will output the public IP for the Apache webserver as the
- Copy the IP address, paste it in a new browser window or tab, and press Enter.
- Verify that the web page displays as My Test Website With Help From Terraform Provisioner, validating that the provisioner within your code worked as intended. The commands configured in the
provisionercode were issued and executed successfully on the EC2 instance that was created.