Terraform is an infrastructure as code (IaC) tool that lets you build, change, and version infrastructure safely and efficiently. This includes low-level components like compute instances, storage, and networking; and high-level components like DNS entries and SaaS features.
Infrastructure as Code (IaC) tools allow you to manage infrastructure - servers, networks, databases - with configuration files rather than through a graphical user interface. IaC allows you to build, change, and manage your infrastructure in a safe, consistent, and repeatable way by defining resource configurations that you can version, reuse, and share.
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.1"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.image_id
name = "tutorial"
ports {
internal = 80
external = 8000
}
}
The terraform block contains Terraform settings, including the required providers Terraform will use to provision the infrastructure.
The source attribute defines an optional hostname, a namespace, and the provider type. Terraform installs providers from the Terraform Registry by default. You can set a version constraint for each provider in the required_providers block.
terraform init
terraform fmt
terraform validate
terraform apply