User Tools

Site Tools


terraform:basic

Terraform up and running

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:

  • author
  • init
  • plan
  • apply

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

resources

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"
        }
    }
 } 
 

providers

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.tf

variables.ts

outputs.tf

holds output values from your resources

terraform.tfstate

terraform saves the state of resources it manages in a state file, stored locally or remotely

trying with docker

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

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
terraform/basic.txt · Last modified: 2024/11/10 08:01 by 127.0.0.1