Azure Terraform Basics

# Initialise Project

mkdir ./project
cd ./project
ccargo init

# Configure Source

Inside of the ccargo.hcl configure a Source:


source "native" "essential" {
  name = "essential"
  version = "latest"
}

This Source will allow CloudCargo to generate artifacts based on the constraints defined in the latest version of our Native Essential Library.

# Configure Target

Below the Source configure a Target for your Cargo:


// ...

target "filesystem" "terraform" {
  path = "./terraform"
}

This Target specifies where CloudCargo will output your Cargo once applied.

# Configure a Cargo

Below the Target configure your Cargo:


// ...

cargo "terraform" "infrastructure" {
  source = native.essential
  target = filesystem.terraform

  option = "azure"
}

This Cargo will use our Native Essential Library and our predefined Azure Option to generate your Terraform.

# Configure a Linux Web App

Inside of the Cargo define a module for a Linux Web App.


// ...

cargo "terraform" "infrastructure" {
  // ...
  module "linux_web_app" "my_webapp" {
    stack = "development"
  }
}

This will generate a "linux_web_app" Terraform module with all of it's dependencies automatically resolved within the "development" Stack. The "development" Stack is defined in our Native Essential Library.

# Apply Cargo

Run the following command to apply your Cargo:


ccargo cargo apply --name=infrastructure

Applying your cargo will generate your Terraform in the "terraform" directory. You can inspect the content of the directory to see what your Cargo has generated.

# Update Linux Web App

Make an adjustment to the variables used by your "linux_web_app" module:


// ...
cargo "terraform" "infrastructure" {
  // ...
  module "linux_web_app" "my_webapp" {
    // ... 
    variables {
      name = "my-webapp-dev"
    }
  }
}

# Apply Cargo

Run the following command to apply your Cargo:


ccargo cargo apply --name=infrastructure

You will note that the resulting Terraform has been updated with your new "name" variable.

# Manage Dependencies

Make an adjustment to the "resource_group" dependency used by your "linux_web_app" module:


// ...
cargo "terraform" "infrastructure" {
  // ...
  module "linux_web_app" "my_webapp" {
    // ... 
	dependency "resource_group" "my_resource_group" {
	  variables {
	    name = "rg-myapp-dev"
	  }
	}
  }
}

# Apply Cargo

Run the following command to apply your Cargo:


ccargo cargo apply --name=infrastructure

You will note that the resulting Terraform now has two "resource_group" modules defined. One that is used by your "linux_web_app" and another that is used by the default "dev_service_plan", this is because you only updated the dependency of your "linux_web_app".

# Manage Stack Dependency

In order to change the name of the "resource_group" dependency within the "development" Stack without creating a new "resource_group", you can change the name for that "resource_group" as below:


cargo "terraform" "infrastructure" {
  source = native.library
  target = filesystem.terraform

  option = "azure"

  module "resource_group" "dev_resource_group" {
    stack = "development"
    variables {
      name = "rg-myapp-dev"
    }
  }

  module "linux_web_app" "my_app" {
    stack = "development"
  }
}

# Apply Cargo

Run the following command to apply your Cargo:


ccargo cargo apply --name=infrastructure

You will note that the resulting Terraform now only has one "resource_group" module defined.

# Explore CloudCargo

Run the following command to explore the built-in documentation further, and define your own modules according to the documentation:


ccargo serve