Running Azure Functions in Container Apps with Terraform was released on May 24, 2024, and that allows you to run Azure Functions in Container Apps using Terraform. In this article, I’ll guide you through the steps to set up and deploy Azure Functions in Container Apps with Terraform. Additionally, for those new to Container Apps, I’ve included a brief overview of their benefits.
Terraform script for Azure Function in Container App Environment
As is tradition on my blog, the source code comes first! You can access the full code [here]. On GitHub, I’ve set up a GitHub Action to run weekly, ensuring the build status is always up-to-date. You can check the current build status below:
To run an Azure Function in Container Apps, all you need is the Container App itself! While you won’t be deploying an Azure Function as a traditional resource, rest assured that all triggers will work seamlessly when the Azure Function runs within the Container App. Currently, there isn’t an option to create an Azure Function resource specifically for use in the Container Apps Environment. However, I don’t see this as a major issue—do you? Below, you’ll find the Terraform script for creating the Azure Function:
Why use Azure Container Apps for Azure Function?
One of the main reasons to run Azure Functions in Azure Container Apps (ACA) is cost efficiency. If this is the only workload you’re hosting, you can take advantage of 180,000 vCPU-seconds and 360,000 GiB-seconds per month at no cost! Additionally, you can integrate your environment with an Azure Virtual Network, providing seamless connectivity.
If you’re adding this as another workload to your existing ACA setup, there won’t be any extra hosting costs, and your environment remains consistent.
However, keep in mind that only the triggers listed below can scale from zero. If you plan to use a trigger not included in this list, you’ll need to set min_replicas
to at least 1:
- HTTP
- Azure Queue Storage
- Azure Service Bus
- Azure Event Hubs
- Kafka
- Timer
Summary
In this post, I explore how to run Azure Functions in Container Apps using Terraform, a new feature released in May 2024. I’ll guide you through the setup, offer the source code on GitHub, and discuss cost benefits, including free usage limits. Plus, I highlight key triggers and provide a Terraform script to get you started.
I truly hope you found it enjoyable, and if that’s the case, I would be grateful for a Like or a Comment on my LinkedIn profile.
Nice repo! Do you know if is possible to get this working with application insight and event grid topic subscriptions?
Scaling using Event Grid topic should be similar to the Azure Container Apps Service Bus scaling
If you need some more assistance, feel free to reach me by the LinkedIn
Great post! How can you use Event Grid triggers and Azure Appliction Insights for this?
Hi, I’m glad that you like it!
In general container apps are the same as the rest of the web services ie. App Service. You just need to add Application Insights connection string as environment variable.
Regarding the Event Grid triggers I haven’t used that one yet, do you need it to run Azure Function or scale application?
See below cod for python app hosted in the Azure Container Apps:
from azure.monitor.opentelemetry import configure_azure_monitor
configure_azure_monitor()
# This tracks HTTP requests made by aiohttp:
AioHttpClientInstrumentor().instrument()
# This tracks HTTP requests made by httpx:
HTTPXClientInstrumentor().instrument()
# This tracks OpenAI SDK requests:
OpenAIInstrumentor().instrument()
# This middleware tracks app route requests:
app.asgi_app = OpenTelemetryMiddleware(app.asgi_app) # type: ignore[assignment]