AWS Hands-On Lab -> Terraform Set Up Apache Web Server

AWS Hands-On Lab -> Terraform Set Up Apache Web Server

AWS Learning Path for Cloud and DevOps Engineers

📝Introduction

This post demonstrates using a Terraform provisioner to custom bootstrap a VM in AWS, install a webserver on it, and then test that the webserver is working as expected.

📝Log in to the AWS Management Console

Using your credentials, make sure you're using the right Region. In my case, I chose us-east-1.

Note: You must create the AWS Access Key and AWS Secret Access Key and configure the AWS CLI in the terminal to use it.

You can use link1 and link2 for it.

📝GitHub repository

Go to the GitHub repo to fork the files used in this Lab on this link.

📝Configure the AWS CLI and generate an RSA key

  1. Configure your AWS CLI: aws configure

  2. When prompted for your AWS Access Key ID, copy and paste in the Access Key created previously by you.

  3. When prompted for your AWS Secret Access Key, copy and paste in the Secret Access Key created previously by you.

  4. Add your default region and press Enter, then press Enter again to accept the default output.

  5. To generate an RSA key, run the default command in your terminal:

    ssh-keygen

📝Review configuration files from Git repo Terraform_Set_Up_Apache_Web_Server

  1. View the contents of the directory: ls

  2. Go into the Terraform_Set_Up_Apache_Web_Server directory: cd Terraform_Set_Up_Apache_Web_Server

  3. View the contents of the Terraform_Set_Up_Apache_Web_Serverdirectory:

    ls

  4. Open the main.tf file: vim main.tf

  5. Review the contents of the file, and then enter :q! to exit the file.

  6. Open the setup.tf file: vim setup.tf

  7. Review the contents of the file, and then enter :q! to exit the file.

📝Deploy the Code and Access the Bootstrapped Webserver

  1. Initialize the Terraform working directory, and download the required providers: terraform init

  2. 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

    Note: You should receive a notification that the configuration is valid.

  3. Review the actions that will be performed when you deploy the Terraform code:

    terraform plan

    Note: In this case, it will create 7 resources as configured in the Terraform code.

  4. Deploy the code:

    terraform apply

  5. When prompted, type yes, and press Enter.

  6. As the code is being deployed, you will notice that the Terraform provisioner tries to connect to the EC2 instance, and will run the bootstrapping.

  7. When complete, it will output the public IP for the Apache webserver as the Webserver-Public-IP value.

  8. Copy the IP address, paste it into a new browser window or tab, and press Enter.

  9. Verify that the web page displays the index.html info, validating that the provisioner worked as intended.

📝Destroy deployment

  1. To destroy all the resources created in this Hands-On Lab, run the command:

    terraform destroy

    Note: Do not forget to do it, otherwise, you will receive a considerable charge.

📝Video Step-by-Step of Hands-On Lab

Congratulations — you have completed this hands-on lab covering the basic use of Terraform provisioner to custom bootstrap a VM in AWS, install a webserver on it, and then test that the webserver is working as expected.

Thank you for reading. I hope you were able to understand and learn something helpful from my blog.

Please follow me on CloudDevOpsToLearn and LinkedIn franciscojblsouza