SETTING UP APACHE WEB SERVER USING TERRAFORM PROVISIONERS

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.

requirement

  • 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
    • Main.tf
    • Setup.tf
    • Readme.md
  • 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
    • The remote-exec keyword 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 connection block to connect to the AWS EC2 instance being created.
    • The provisioner will then issue the commands configured in the inline block to install Apache webserver on CentOS through the yum package manager, start up the Apache server, create a single web page called My Test Website With Help From Terraform Provisioner as an index.html file, and move that file into the data directory of the webserver to be served out globally.

  • Initialize the Terraform working directory, and download the required providers:terraform init
  • Validate the code to look for any errors in syntax, parameters, or attributes within Terraform resources that may prevent it from deploying correctly:terraform validate You should receive a notification that the configuration is valid.
  • Review the actions that will be performed when you deploy the Terraform code:terraform plan In this case, it will create 7 resources as configured in the Terraform code.
  • Deploy the code:terraform apply
  • 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 provisioner block against the instance.
  • When complete, it will output the public IP for the Apache webserver as the Webserver-Public-IP value.
  • 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 provisioner code were issued and executed successfully on the EC2 instance that was created.

Leave a Reply