Terraform On Azure – Creating A Azure Perform Working On Consumption Plan
Introduction
Azure Capabilities operating on Consumption Plan are serverless choices on Azure Capabilities. You possibly can host code that runs for shorter time intervals utilizing Azure Capabilities. You get billed solely when the code executes, and you needn’t fear concerning the scaling elements because the underlying Azure platform manages that. Azure Capabilities are highly effective sufficient to cater to purposes based mostly on trendy architectures like Microservices. On this article, we’ll discover ways to create a Azure Perform operating on Consumption Plan utilizing Terraform.
Within the earlier articles, we discovered the fundamentals of Terraform. We created an Azure WebApp utilizing Terraform after which we created Azure Digital Machine and a Storage Account. The next are hyperlinks to the earlier articles.
Create Azure Perform utilizing Terraform
Allow us to create a Useful resource Group and inside it, we will create an Azure Perform.
Log in to the Azure portal at https://portal.azure.com. Allow us to use Azure Cloud Shell to create assets utilizing Terraform. Azure Cloud Shell has Terraform put in and you needn’t do any set up or configuration to work with Terraform.
As soon as the Azure Cloud Shell opens up, choose Bash. Allow us to begin creating scripts to create an Azure Perform. We will use nano editor to create the Infrastructure as Code script for the Azure Perform utilizing Terraform.
Execute the next command to open a nano editor and create a file named myterraformscript.tf.
nano myterraformscript.tf
Add the next code to the nano editor. This may create a Useful resource Group. Exchange {ResourceGroup} with the title of your Useful resource Group that you’re planning to create.
terraform {
required_providers {
azurerm = {
supply = "hashicorp/azurerm"
}
}
}
supplier "azurerm" {
options {}
}
useful resource "azurerm_resource_group"
"{ResourceGroup}" {
title = "{ResourceGroup}"
location = "eastus"
}
Add the next code in nano editor to create a Storage Account. Exchange {ResourceGroup} with the title of your Useful resource Group and {StorageAccount} with the title of your Storage Account.
useful resource "azurerm_storage_account"
"{StorageAccount}" {
title = "{StorageAccount}"
resource_group_name = azurerm_resource_group. {
ResourceGroup
}.title
location = azurerm_resource_group. {
ResourceGroup
}.location
account_tier = "Normal"
account_replication_type = "LRS"
}
Add the next code to create a Consumption Plan. Exchange {ResourceGroup} with the title of your Useful resource Group, {Plan} with the title of your Consumption Plan.
useful resource "azurerm_app_service_plan"
"{Plan}" {
title = "{Plan}"
location = azurerm_resource_group. {
ResourceGroup
}.location
resource_group_name = azurerm_resource_group. {
ResourceGroup
}.title
form = "FunctionApp"
sku {
tier = "Dynamic"
dimension = "Y1"
}
}
Add the next code to create the Azure Perform. Exchange {ResourceGroup} with the title of your Useful resource Group, {StorageAccount} with the title of your Storage Account and {Plan} with the title of your Consumption Plan.
useful resource "azurerm_function_app"
"{Perform}" {
title = "{Perform}"
location = azurerm_resource_group. {
ResourceGroup
}.location
resource_group_name = azurerm_resource_group. {
ResourceGroup
}.title
app_service_plan_id = azurerm_app_service_plan. {
Plan
}.id
storage_account_name = azurerm_storage_account. {
StorageAccount
}.title
storage_account_access_key = azurerm_storage_account. {
StorageAccount
}.primary_access_key
}
The next is the script file that you simply created. You possibly can check with the hooked up script file and check out the pattern.
terraform {
required_providers {
azurerm = {
supply = "hashicorp/azurerm"
}
}
}
supplier "azurerm" {
options {}
}
useful resource "azurerm_resource_group"
"{ResourceGroup}" {
title = "{ResourceGroup}"
location = "eastus"
}
useful resource "azurerm_storage_account"
"{StorageAccount}" {
title = "{StorageAccount}"
resource_group_name = azurerm_resource_group. {
ResourceGroup
}.title
location = azurerm_resource_group. {
ResourceGroup
}.location
account_tier = "Normal"
account_replication_type = "LRS"
}
useful resource "azurerm_app_service_plan"
"{Plan}" {
title = "{Plan}"
location = azurerm_resource_group. {
ResourceGroup
}.location
resource_group_name = azurerm_resource_group. {
ResourceGroup
}.title
form = "FunctionApp"
sku {
tier = "Dynamic"
dimension = "Y1"
}
}
useful resource "azurerm_function_app"
"{Perform}" {
title = "{Perform}"
location = azurerm_resource_group. {
ResourceGroup
}.location
resource_group_name = azurerm_resource_group. {
ResourceGroup
}.title
app_service_plan_id = azurerm_app_service_plan. {
Plan
}.id
storage_account_name = azurerm_storage_account. {
StorageAccount
}.title
storage_account_access_key = azurerm_storage_account. {
StorageAccount
}.primary_access_key
}
Run the next command to provoke Terraform. This may fetch all dependencies wanted to execute the Terraform script.
terraform init
Now allow us to create an execution plan for Terraform. Allow us to present the title of the execution plan within the out parameter.
terraform plan -out myfunc.tfplan
Execute the execution plan utilizing the next command. The Azure Perform will get created.
terraform apply "myfunc.tfplan"
Conclusion
On this article, we discovered learn how to create an Azure Perform with Consumption Plan. Within the subsequent article, we’ll discover ways to create an Azure Perform utilizing App Service Plan.