Deploy an Azure Function App with Terraform

Terraform allows us to deploy and destroy infrastructure in minutes. IaC tools/technologies have become fundamental pillars for infrastructure development and management. Through the implementation of Infrastructure as Code solutions, we ca

AI-assisted translation, approved by Mateo.

Terraform allows us to deploy and destroy infrastructure in minutes. IaC tools/technologies have become fundamental pillars for infrastructure development and management.

Through the implementation of Infrastructure as Code solutions, we can guarantee the consistency, replicability, and scalability of our environments, minimizing manual errors and optimizing deployment.

In this case, we will deploy and destroy a small Azure Functions infrastructure using Terraform.

Repository used

We will use the following repo https://github.com/mdiloreto/madsblog-terraform-01 to carry out this lab.

Description of the repository code

In this repository, we will find the following files:

  • main.tf
    • Provider declaration
  • If you are configuring a collaborative Terraform environment, I recommend creating a Remote State File
    • Creation of the RG, Storage Account, and App Service Plan:
  • Creation of Azure Functions, runtime configuration, runtime version, and enabling Cors for Portal.Azure. The latter will allow us to use the Test section of "Code+Test"
  • locals.tf
    • Here we have the variables used for the names.
  • variables.tf
    • Here we will declare the "Random" provider for the numeric strings of the variables:

How to connect to Azure CLI

We must log in to our Azure tenant and subscription. To do this, we must enter the following commands in Azure CLI:

  • az login
  • az account list –output table
  • az account set –subscription <SubscriptionId>

Deploy infrastructure with Terraform

There are 3 stages for deploying infrastructure with Terraform: Plan + Apply + Destroy.

Planning (Plan): Before making real changes to the infrastructure, Terraform creates an execution plan that shows which actions will be carried out. This allows us to review the proposed changes and ensure that the result will be the expected one.

Applying (Apply): Once the plan has been reviewed and approved, Terraform proceeds to make the changes needed to reach the desired state described in the code. During this phase, resources are created, modified, or deleted as needed. Running this stage includes Plan.

Destroying (Destroy): If at any point it is necessary to remove all deployed infrastructure (for example, to reduce costs or because the project has ended), Terraform can destroy all the resources it created. This is done in an orderly and controlled manner, avoiding manual errors and ensuring that no orphaned resources remain.

Terraform Apply

When we run <terraform apply>, it will show us the execution plan for all resources in our repository's .tf files.

  • In this case, we will generate all the resources for our Function App.
  • As we can see in the next image, an RG that was being managed by Terraform and was removed from the .tf files will be destroyed.
  • Once we have carefully verified our execution plan, we can complete the deployment.
  • To do this, we must enter the value: "yes".
  • Now we can see the details of the resources to be created:

Verification of the created resources

Terraform destroy

Once we have used our infrastructure for the desired purpose, we can proceed to destroy it: we will do this in a matter of seconds. Terraform controls the entire infrastructure and knows the configurations, dependencies, and interrelationships. We do not need to take care of anything.

  • We verify that there are no changes through a Terraform Plan.
  • Now we will proceed to run <terraform destroy>
  • We will be able to see the "destruction" plan immediately, just as with <terraform apply>.
  • In this case, we will be able to see in detail all the resources that will be destroyed.
  • To run the operation, we enter the value: "yes".
  • At the end, we will see the number of destroyed resources.

Thanks for reading. The learning continues in the next article.