How to use Terraform Dynamic Blocks for creating App Service Auto Scaling resource
Date published:
Dynamic Block in Terraform is a function that allows you to create multiple nested blocks. A typical use case for a Dynamic Block is using a for_each functionality within the block to reduce and remove duplicate resources in your terraform code. It is also easier to read and understand code written because it will have less repetitive code.
Some of the Best Practices for dynamic Blocks are
- Overusing dynamic blocks can make it hard to read and maintain because it will involve overhead when trying to understand all the code.
- Always use nested blocks and modules instead
In Azure Portal, the autoscaling will be under the heading Scale Out. This autoscaling on the app service will reference the App Service Plan Scale Out.
Below, I will show you how to use Terraform to manage and create autoscaling for your App Service. The Terraform code in this example is here. However, you can also apply it to other resources such as Virtual Machine Scale Set.
Deployment to Azure
Once you have the deployed resources using Terraform, apply after a successful plan. It will create seven resources in Azure Portal in your infrastructure. It should look like the below image.
As you can see above, the autoscaling of the web app metric is alerting on the following.
- CpuPercentage greater than 90
- MemoryPercentage greater than 70
Conclusion
In the post, I used terraform functions like for_each and toset to create multiple Azure resources. This example can use the for_each, but I wanted to show you how to use the toset function.
I hope you enjoy it and thanks for reading. :)