Terraform — Resource Block and Provider Block
In Terraform, a Resource is a logical abstraction of an infrastructure component that Terraform can manage, and A Provider is responsible for understanding API interactions and exposing resources.
In Terraform the resource and provider configuration files are written in Hasicorp Configuration Language(HCL).
What is a Provider?
The Terraform plugin manages the lifecycle of a particular infrastructure platform, service, or technology. Example includes AWS, Azure, Docker, etc.
Each provider offers a set of resources and data sources that can be used in Terraform configurations. Providers are responsible for creating, updating, and deleting resources on the target infrastructure.
Example of provider block for Docker:
provider "docker" {
# configuration options for the Docker provider
}
What is a Resource?
Resources are the instance of resource types provided by Terraform providers. A resource in Terraform represents a piece of infrastructure, such as a virtual machine, or network interface.
Terraform uses the resource configuration to plan and execute changes to the infrastructure.
Example resource blocks for Docker:
resource "docker_image" "example_image" {
name = "nginx:latest"
# other configuration options for the Docker image resource
}
resource "docker_container" "example_container" {
name = "web-server"
image = docker_image.example_image.name
# other configuration options for the Docker container resource
}
Installing Terraform :
To install Terraform in your OS, refer to Terraform's official documentation here
In this blog, I will be covering how you can start Nginx in Docker using Terraform, but before starting make sure that your system has the Docker installed.
Procedure:
Step 1: Create a Provider provider.tf:
Create a new file, provider.tf
:
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "3.0.2"
}
}
}
terraform
Block: This block is used for global configuration settings for Terraform.required_providers
Block: Within theterraform
block, therequired_providers
block is used to declare the providers that your configuration depends on.docker
Block Insiderequired_providers
: This specifies the provider named "docker."source
Attribute: This attribute specifies the source of the provider. In this case, the Docker provider is sourced from the "kreuzwerker/docker" namespace. The source is often in the format ofnamespace/provider-name
.version
Attribute: This attribute specifies the version of the Docker provider that your configuration requires. In this example, it's set to version "3.0.2."
Step 2: Create Resource resource.tf:
Create a new file resourse.tf:
provider "docker" {}
resource "docker_image" "resource_image" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "resource_container" {
name = "day62"
image = docker_image.resource_image.name
ports {
internal = 80
external = 80
}
}
Provider Block:
provider "docker" {}
- This block declares the Docker provider. It tells Terraform that you want to use the Docker plugin to manage Docker resources.
Docker Image Resource Block:
resource "docker_image" "resource_image" {
name = "nginx:latest"
keep_locally = false
}
- This block defines a Docker image resource named "resource_image."
name
: Specifies the name of the Docker image, in this case, "nginx:latest."keep_locally
: Indicates whether to keep a copy of the Docker image locally after it has been pulled. Here, it's set tofalse
, meaning it won't be kept locally.
Docker Container Resource Block:
resource "docker_container" "resource_container" {
name = "day62"
image = docker_image.resource_image.name
ports {
internal = 80
external = 80
}
}
- This block defines a Docker container resource named "resource_container."
name
: Specifies the name of the Docker container, which is set to "day62."image
: Refers to the Docker image resource you defined earlier (docker_image.resource_image.name
), indicating that this container should be based on that image.ports
: Defines port mapping for the container. It maps port 80 from the container to port 80 on the host (external).
Now, to apply the above configuration use the below steps:
terraform init:
This command initializes a Terraform working directory, it also downloads the required providers.
This command will create a .terraform
directory containing metadata, plugins, and dependencies. It generates .terraform.lock.hcl
to lock provider and module versions.
2. terraform plan:
This command is used to generate an execution plan that describes the changes Terraform will make to your infrastructure.
It provides information on additions, modifications, and deletions of resources based on your Terraform configuration, without actually applying those changes.
3. terraform apply:
This command is used to apply the changes described in the Terraform execution plan. After running terraform plan
and reviewing the proposed changes, you can use terraform apply
to actually make those changes to your infrastructure.
Type yes,
to apply the configuration.
4. Confirmation:
To confirm that the Docker resource has been created, let’s see the docker images and docker running container, by using the below command:
docker images
Checking docker container:
docker ps
To check that Nginx is running, open your browser and go to localhost, port number 80.
5. terrform destroy:
To destroy the infrastructure created by Terraform, use this command:
terraform destory
Type yes,
to confirm this action.
6. Confirmation:
To confirm that Terraform resources have been destroyed we, will use the above method which we used to confirm that Terraform has created resources.
- Docker image:
Here, you can see that the Nginx image has been removed.
- Docker container:
No container is up and running
- Nginx running in the browser:
Open your browser and navigate to localhost port number 80:
So the above results show that Terraform has destroyed the resources which was once created by it.