language: .hcl Hashicorp Configuration Language syntax:
<block type> "<block label>" "<block label>"{
#body block
<identifier> = <expression>
}
ex:
resource "google_compute_network" "default" {
#comment
name = mynetwork
auto_create_subnetworks = false
}
phases:
directories:
-- main.tf ]root module
-- servers/ _
-- main.tf |
-- providers.tf | child Module
-- variables.tf |
-- outputs.tf |
terraform plug-in calls providers
reusable terraform configurations called modules
terraform configuration languages il declarative it consists in 5 step: - scope: identify your project - author: write your code - initialize: install needed plugins, providers - plan: preview changes - apply: make changes
inside main.tf
resource "google_storage_bucket" "example_bucket"{
name = "unique-bucket-name"
location = "US"
}
resource "google_compute_instance" "examplre_compute" {
name = "test"
machine-type = "e2-medium"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
}
you can find the list of providers here: providers for google: google
if not defined assueed as default. inside providers.tf
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "4.23.0"
}
}
provider "google" {
project = "project_id"
region = "us-central1-"
}
variables.ts
holds output values from your resources
terraform saves the state of resources it manages in a state file, stored locally or remotely
copy and paste in main.tf file the following code:
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
# version = "~> 2.15.0"
version = "3.0.2"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
#image = docker_image.nginx.latest
image = docker_image.nginx.image_id #latest doesn't exist
name = "tutorial"
ports {
internal = 80
external = 8000
}
}
after this you can issue
terraform init
in the event we need to change the provider, it is mandatory to reissue terraform init –upgrade
then apply the configuration with
terraform apply
then remove it with
terraform destroy
after this pay particular attention to the state file where terraform save the status of the application