I'm trying to deploy Azure Function infrastructure along with HttpTrigger using Terraform. I know that using Terraform I'll create the infrastructure and the triggering part is code responsibility. But still do not understand how to create HttpTrigger. Could you please advise?
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.26"
}
}
}
###############################
# Configure the Azure provider
provider "azurerm" {
features {}
}
###############################
# Data
data "archive_file" "file_function_app" {
type = "zip"
source_dir = "./function-app"
output_path = "./function-app.zip"
}
###############################
# Resource Group
resource "azurerm_resource_group" "rg" {
name = "${var.project}-rg"
location = var.location
}
###############################
# App service
resource "azurerm_app_service_plan" "app_service_plan" {
name = "${var.project}-app-service-plan"
resource_group_name = azurerm_resource_group.rg.name
location = var.location
kind = "FunctionApp"
reserved = true
sku {
tier = "Dynamic"
size = "Y1"
}
}
###############################
# Function
resource "azurerm_function_app" "function_app" {
name = "${var.project}-function-app"
resource_group_name = azurerm_resource_group.rg.name
location = var.location
app_service_plan_id = azurerm_app_service_plan.app_service_plan.id
app_settings = {
"WEBSITE_RUN_FROM_PACKAGE" = "https://${azurerm_storage_account.storage_account.name}.blob.core.windows.net/${azurerm_storage_container.storage_container.name}/${azurerm_storage_blob.storage_blob.name}${data.azurerm_storage_account_blob_container_sas.storage_account_blob_container_sas.sas}",
"FUNCTIONS_WORKER_RUNTIME" = "python",
"APPINSIGHTS_INSTRUMENTATIONKEY" = ""
}
os_type = "linux"
site_config {
linux_fx_version = "python|3.7"
}
storage_account_name = azurerm_storage_account.storage_account.name
storage_account_access_key = azurerm_storage_account.storage_account.primary_access_key
version = "~3"
}
###############################
# Storage account
resource "azurerm_storage_account" "storage_account" {
name = "${var.project}storage"
resource_group_name = azurerm_resource_group.rg.name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_storage_container" "storage_container" {
name = "function-scm"
storage_account_name = azurerm_storage_account.storage_account.name
container_access_type = "private"
}
resource "azurerm_storage_blob" "storage_blob" {
name = "${filesha256(data.archive_file.file_function_app.output_path)}.zip"
storage_account_name = azurerm_storage_account.storage_account.name
storage_container_name = azurerm_storage_container.storage_container.name
type = "Block"
source = data.archive_file.file_function_app.output_path
}
data "azurerm_storage_account_blob_container_sas" "storage_account_blob_container_sas" {
connection_string = azurerm_storage_account.storage_account.primary_connection_string
container_name = azurerm_storage_container.storage_container.name
start = "2021-01-01T00:00:00Z"
expiry = "2022-04-04T00:00:00Z"
permissions {
read = true
add = false
create = false
write = false
delete = false
list = false
}
Function-app folder with python code has two files: 1. init .py
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello {name}!")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
2. function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
Deployment process completed successfully through Azure pipeline. On Azure portal, when I'm trying to open AzureFunction -> Functions, I do not see anything.
But, in App Files I do see previously created python function files
To create a HTTP Trigger function app using terraform.
3.Here is the example code to enter in terraform modules folder for azure function resources
variable "LOCATION" {}
variable "RESOURCE_GROUP" {}
variable "STORAGE_ACC_NAME" {}
variable "STORAGE_ACC_KEY" {}
variable "STORAGE_CONNECTION_STRING" {}
resource "azurerm_application_insights" "func_application_insights" {
name = "func-application-insights"
location = var.LOCATION
resource_group_name = var.RESOURCE_GROUP
application_type = "Node.JS"
}
resource "azurerm_app_service_plan" "func_app_service_plan" {
name = "func-app-service-plan"
location = var.LOCATION
resource_group_name = var.RESOURCE_GROUP
kind = "FunctionApp"
reserved = true
sku {
tier = "Dynamic"
size = "Y1"
}
}
resource "azurerm_function_app" "func_function_app" {
name = "func-function-app"
location = var.LOCATION
resource_group_name = var.RESOURCE_GROUP
app_service_plan_id = azurerm_app_service_plan.func_app_service_plan.id
app_settings = {
FUNCTIONS_WORKER_RUNTIME = "node",
AzureWebJobsStorage = var.STORAGE_CONNECTION_STRING,
APPINSIGHTS_INSTRUMENTATIONKEY = azurerm_application_insights.func_application_insights.instrumentation_key,
WEBSITE_RUN_FROM_PACKAGE = "1"
}
os_type = "linux"
storage_account_name = var.STORAGE_ACC_NAME
storage_account_access_key = var.STORAGE_ACC_KEY
version = "~3"
lifecycle {
ignore_changes = [
app_settings["WEBSITE_RUN_FROM_PACKAGE"]
]
}
# FIXME: Use DNS names instead of enabling CORS
site_config {
cors {
allowed_origins = ["*"]
}
}
}
For complete details on creating azure functions using Terraform please check this document . and here is the complete source code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.