Terraform

What is Terraform?

Prerequisites

Install Terraform

cd \The new added PATH
terraform init

Install Azure Terraform Visual Studio Code extension

Azure subscription

az account list

Deploy Azure infrastructure with Terraform

  • Resource Group
  • SQL Server
  • SQL Database
//Configure Azure Provider
provider "azurerm" {
subscription_id = var.subscription_idclient_id = var.client_idclient_secret = var.client_secrettenant_id = var.tenant_id}//Declare all the Variables that will be used in Terraform configurationsvariable "subscription_id" {description = "Enter Subscription ID for provisioning resources in Azure"}variable "client_id" {description = "Enter Client ID for Application created in Azure AD"}variable "client_secret" {description = "Enter Client secret for Application in Azure AD"}variable "tenant_id" {description = "Enter Tenant ID / Directory ID of your Azure AD. Run Get-AzureSubscription to know your Tenant ID"}variable "location" {description = "The default Azure region for the resource provisioning"}variable "resource_group_name" {description = "Resource group name that will contain various resources"}
subscription_id = "Your subscription_id"tenant_id = "Your tenant_id"client_id = "Your client_id"client_secret = "Your client_secret"location = "West Europe"     //Choose your locationresource_group_name     = "Terraform-Test"
//Resource Groupresource "azurerm_resource_group" "Terraform" {name = var.resource_group_namelocation = var.locationtags = {owner = "sazabou"}}
  1. Plan
    A good practice is to use Terraform Plan every time we want to deploy something. We can see this as a debugger that tells you what you’re going to do and check if it’s gonna work.
  2. Execute
    Terraform Execute is where you actually start deploying your resources after checking that everything is OK.
  3. Destroy
    Once you’re done, especially when you’re just trying stuff on Azure, make sure to hit Terraform Destroy to clean up your environment and delete all the resources you created. (You don’t wanna get charged for resources that you’re not really using).
terraform initterraform plan -var-file var_values.tfvars
terraform apply -var-file var_values.tfvarsyes
//SQL Server
resource "azurerm_sql_server" "sqlserver" {
name = "sqlserverterraformtest"
resource_group_name = var.resource_group_name
location = var.location
version = "12.0"
administrator_login = "Type your login"
administrator_login_password = "Type your password" //Use a complex one
}
//SQL Database
resource "azurerm_sql_database" "SQLDB" {
name = "SQLDB-terraform-test"
server_name = "sqlserverterraformtest"
resource_group_name = var.resource_group_name
location = var.location
edition = "Standard"
requested_service_objective_name = "S0" //Only S0 or higher supported
tags = {
environment = "Dev"
}
}
terraform destroy -var-file var_values.tfvars

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

JVM(Java Virtual Machine)

Swift Leetcode Series : Powerful Integers

Data Extraction using Form Recognizer of Cognitive Service (v2.1)

5 First-Hand Tips to Stand Out in a Technical Interview

Girl writing on a whiteboard

Big Data and Machine Learning Fundamentals using GCP for Data Professionals.

Two Sum | Interview Question

Will These Cloud Service Gotchas Doom Your App?

Upload large files to Cloudinary through REST API

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sofiene Azabou

Sofiene Azabou

More from Medium

Fraktal — A Positive Approach on Cyber Security

Quick and easy malware analysis using Windows Sandbox and Elastic Security

The Phantom Credentials of SCCM: Why the NAA Won’t Die

spring4shell detection and response — Elastic