[Draft] Module 1:Infrastructure as Code (IaC) with Terraform
Explain what IaC is
Create Terraform Infrastructure with Docker
To deploy infrastructure with Terraform:
Scope - Identify the infrastructure for your project.
Author - Write the configuration for your infrastructure.
Initialize - Install the plugins Terraform needs to manage the infrastructure.
Plan - Preview the changes Terraform will make to match your configuration.
Apply - Make the planned changes.
In the "Code Editor" tab, open the terraform.tf file.
This file includes the terraform block, which defines the provider and Terraform versions you will use with this project.
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.2"
}
}
required_version = "~> 1.7"
}
Next, open main.tf and copy and paste the following configuration.
copy
``` provider "docker" {}
resource "docker_image" "nginx" {
name = "docker.mirror.hashicorp.services/nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.image_id
name = "tutorial"
ports {
internal = 80
external = 8000
}
}```
In the "Terminal" tab, initialize the project, which downloads a plugin that allows Terraform to interact with Docker.
copy
terraform init
Provision the NGINX server container with apply. When Terraform asks you to confirm, type yes and press ENTER.
copy
terraform apply
Verify NGINX instance
Run docker ps to view the NGINX container running in Docker via Terraform.
copy
docker ps
Destroy resources
To stop the container and destroy the resources created in this tutorial, run terraform destroy. When Terraform asks you to confirm, type yes and press ENTER.
copy
terraform destroy
You have now provisioned and destroyed an NGINX webserver with Terraform.



