Check KodeKloud’s Terraform Challenge #2!

Note - if you really must see the solutions directly, find them on my GitHub page; okbobm!

What’s the scenario? In this challenge we will implement a simple LAMP stack using terraform and docker. Utilize /root/code/terraform-challenges/challenge2 directory to store your Terraform configuration files.

Challenge2

What are the general requirements for this challenge?

  1. Install terraform binary version=1.1.5 on iac-server
  2. Create Docker image resource (i.e. mariadb-custom-image)
  3. Create Docker image resource (i.e. php-httpd-image)
  4. Create Docker network resource (i.e. private_network)
  5. Create Docker container in private_network (i.e. db)
  6. Create Docker container in private_network(i.e. db_dashboard)
  7. Create Docker container in private_network(i.e. webserver)
  8. Create Docker container (i.e. mariadb-volume)
  9. Deploy

Step 1: Verify Terraform is installed on ControlPlan, if not install

Requirements:

  • Terraform version: 1.1.5 installed on controlplane?

Solution:

  • Is Terraform installed?
    • Try to find terraform on the controlplane
      • terraform –version
      • which terraform
  • Install directory
    • Run update and install unzip library
      • apt update
      • apt install unzip
    • Download terraform, unzip, and move files
      • wget https://releases.hashicorp.com/terraform/1.1.5/terraform_1.1.5_linux_amd64.zip
      • unzip terraform_1.1.5_linux_amd64.zip
      • mv terraform /usr/local/bin/
    • Verify install and version successfull
      • terraform –version
      • which terraform
  • Review provider.tf file
    • Note from file that the provider is “kreuzwerker/docker”
  • Initialize terraform in this directory
    • terraform init

Step 2: Create Docker image resource (i.e. mariadb-custom-image)

Requirements:

  • Create a terraform resource named mariadb-image for building docker image with following specifications: Image name: mariadb:challenge
  • Build context: lamp_stack/custom_db
  • Labels: challenge: second

Solution:

  • Create docker image resource in terraform_challenge directory
    • Create the mariadb-custom-image.tf file based on the Terraform Kreuzwerker/Docker image example, modifying for parameters in the above requirements.
  • Optional check to verify everything is correct
    • terraform plan
    • terraform apply

Step 3: Create Docker image resource (i.e. php-httpd-image)

Requirements:

  • Create a terraform resource named php-httpd-image for building docker image with following specifications: Image name: php-httpd:challenge
  • Build context: lamp_stack/php_httpd
  • Labels: challenge: second

Solution:

  • Create docker image resource in terraform_challenge directory
    • Create the php-httpd-image.tf file based on the Terraform Kreuzwerker/Docker image resource example, modifying for parameters in the above requirements.
  • Optional check to verify everything is correct
    • terraform plan
    • terraform apply

Step 4: Create Docker container (i.e. mariadb-volume)

Requirements:

  • Create a terraform resource named mariadb_volume creating a docker volume with name=mariadb-volume

Solution:

  • Create docker container resource in terraform_challenge directory
    • Create the mariadb-volume.tf file based on the Terraform Kreuzwerker/Docker container resource example, modifying for parameters in the above requirements.
  • Optional check to verify everything is correct
    • terraform plan
    • terraform apply

Step 5: Create Docker network resource (i.e. private-network)

Requirements:

  • Create a terraform resource named private_network and configure the following: Create a Docker network with name=my_network
  • Enable manual container attachment to the network.
  • User-defined key/value metadata: challenge: second

Solution:

  • Create docker network resource in terraform_challenge directory
    • Create the private_network.tf file based on the Terraform Kreuzwerker/Docker network resource example, modifying for parameters in the above requirements.
  • Optional check to verify everything is correct
    • terraform plan
    • terraform apply

Step 6: Create Docker container in private_network (i.e. db)

Requirements:

  • Define a terraform resource mariadb for creating docker container with following specification: Container Name: db
  • Image Used: mariadb:challenge
  • Hostname: db
  • Attach the container to network my_network
  • Publish a container’s port(s) to the host:
    • Hostport: 0.0.0.0:3306
    • containerPort: 3306
    • Labels: challenge: second
  • Define environment variables inside mariadb resource:
    • MYSQL_ROOT_PASSWORD=1234
    • MYSQL_DATABASE=simple-website
  • Attach volume mariadb-volume to /var/lib/mysql directory within db container.

Solution:

  • Create docker network resource in terraform_challenge directory
    • Create the db.tf file based on the Terraform Kreuzwerker/Docker container resource example, modifying for parameters in the above requirements.
  • Optional check to verify everything is correct
    • terraform plan
    • terraform apply

Step 7: Create Docker container in private_network (i.e. webserver)

Requirements:

  • Define a terraform resource php-httpd for creating docker container with following specification: Container Name: webserver
  • Hostname: php-httpd
  • Image used: php-httpd:challenge
  • Attach the container to network my_network
  • Publish a container’s port(s) to the host:
    • Hostport: 0.0.0.0:80
    • containerPort: 80
  • Labels: challenge: second
  • Create a volume with host_path /root/code/terraform-challenges/challenge2/lamp_stack/website_content/ and container_path var/www/html within webserver container.

Solution:

  • Create docker container resource in terraform_challenge directory
    • Create the webserver.tf file based on the Terraform Kreuzwerker/Docker container resource example, modifying for parameters in the above requirements.
  • Optional check to verify everything is correct
    • terraform plan
    • terraform apply

Step 8: Create Docker container in private_network (i.e. db_dashboard)

Requirements:

  • Define a terraform resource phpmyadmin for docker container with following configurations: Container Name: db_dashboard
  • Image Used: phpmyadmin/phpmyadmin
  • Hostname: phpmyadmin
  • Attach the container to network my_network
  • Publish a container’s port(s) to the host:
    • Hostport: 0.0.0.0:8081
    • containerPort: 80
  • Labels: challenge: second
  • Establish link based connectivity between db and db_dashboard containers (Deprecated)
  • Explicitly specify a dependency on mariadb terraform resource

Solution:

  • Create docker container resource in terraform_challenge directory
    • Create the db_dashboard file based on the Terraform Kreuzwerker/Docker container resource example, modifying for parameters in the above requirements.
  • Optional check to verify everything is correct
    • terraform plan
    • terraform apply

Step 9: Deploy

Solution:

  • Run commands to apply the plan to deploy resources
    • terraform plan
    • terraform apply

Finally - completed!

You should see everything in green as in the diagram below:

Challenge2_completed