Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modifying events, networking #12

Merged
merged 17 commits into from
Apr 25, 2024
2 changes: 1 addition & 1 deletion function/azurefunctioncode/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"name": "trigger",
"methods": [
"get",
"post"
Expand Down
5 changes: 4 additions & 1 deletion function/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
logging
azure-functions
azure-functions
pyodbc
requests
dateparser
68 changes: 41 additions & 27 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,69 @@ module "network" {
prefix = var.prefix
}

module "database" {
/* module "database" {
source = "./modules/Database"
rg = var.rg
location = var.location
tags = var.tags
prefix = var.prefix
env = var.env
}
} */

module "function_app" {
source = "./modules/FunctionApp"
tags = var.tags
rg = var.rg
location = var.location
sa = module.storage.storage_account
env = var.env
prefix = var.prefix
instru_key = module.monitoring.instrumentation_key
conn_string = module.monitoring.conn_string
source = "./modules/FunctionApp"
tags = var.tags
rg = var.rg
location = var.location
private_storage_name = module.storage.private_storage_name
private_storage_key = module.storage.private_storage_key
private_storage_account_id = module.storage.private_storage_account_id
public_storage_account_id = module.storage.public_storage_account_id
env = var.env
prefix = var.prefix
instru_key = module.monitoring.instrumentation_key
conn_string = module.monitoring.conn_string
func_subnet_id = module.network.function_subnet_id
}

module "eventgrid" {
/* module "eventgrid" {
source = "./modules/Eventgrid"
tags = var.tags
rg = var.rg
prefix = var.prefix
location = var.location
sa = module.storage.storage_account
public_storage_account_id = module.storage.public_storage_account_id
private_storage_account_id = module.storage.private_storage_account_id
env = var.env
name = var.prefix
}
function_app_id = module.function_app.function_app_id
eventgrid_function_app = module.function_app.eventgrid_function_app.name
} */

module "storage" {
source = "./modules/Storage"
tags = var.tags
rg = var.rg
prefix = var.prefix
location = var.location
env = var.env
source = "./modules/Storage"
tags = var.tags
rg = var.rg
prefix = var.prefix
location = var.location
env = var.env
fe_subnet_id = module.network.frontend_subnet_id
be_subnet_id = module.network.backend_subnet_id
comp_subnet_id = module.network.compute_subnet_id
func_subnet_id = module.network.function_subnet_id
#private_dns_zone_blob_id = module.network.private_dns_zone_blob_id
}

module "container" {
source = "./modules/Container"
tags = var.tags
rg = var.rg
location = var.location
prefix = var.prefix
env = var.env
subnet = module.network.compute_subnet_id
source = "./modules/Container"
tags = var.tags
rg = var.rg
location = var.location
prefix = var.prefix
env = var.env
fe_subnet_id = module.network.frontend_subnet_id
be_subnet_id = module.network.backend_subnet_id
comp_subnet_id = module.network.compute_subnet_id
}

module "monitoring" {
Expand Down
2 changes: 1 addition & 1 deletion modules/Container/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resource "azurerm_container_group" "container_group" {
resource_group_name = var.rg
os_type = "Linux"
ip_address_type = "Private"
subnet_ids = toset([var.subnet])
subnet_ids = toset([var.comp_subnet_id])


container {
Expand Down
10 changes: 9 additions & 1 deletion modules/Container/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ variable "env" {
type = string
}

variable "subnet" {
variable fe_subnet_id {
type = string
}

variable be_subnet_id {
type = string
}

variable comp_subnet_id {
type = string
}
19 changes: 8 additions & 11 deletions modules/Eventgrid/main.tf
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
resource "azurerm_storage_queue" "storage_queue" {
name = "sq-${var.prefix}-${var.env}-001"
storage_account_name = var.sa.name
}


resource "azurerm_eventgrid_system_topic" "eventgrid_topic" {
name = "egt-${var.prefix}-${var.env}-001"
location = var.location
resource_group_name = var.rg
source_arm_resource_id = var.sa.id
source_arm_resource_id = var.public_storage_account_id
topic_type = "Microsoft.Storage.StorageAccounts"
tags = var.tags
}

resource "azurerm_eventgrid_system_topic_event_subscription" "event_subscription" {
name = "es-${var.prefix}-${var.env}-001"
system_topic = azurerm_eventgrid_system_topic.eventgrid_topic.name
system_topic = azurerm_eventgrid_system_topic.eventgrid_topic
resource_group_name = var.rg
storage_queue_endpoint {
storage_account_id = var.sa.id
queue_name = azurerm_storage_queue.storage_queue.name
}
azure_function_endpoint {
function_id = var.eventgrid_function_app.name
max_events_per_batch = 1
preferred_batch_size_in_kilobytes = 64
}
included_event_types = ["Microsoft.Storage.BlobCreated"]
}
16 changes: 14 additions & 2 deletions modules/Eventgrid/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ variable "tags" {
type = map(string)
}

variable "sa" {
description = "storage account"
variable "public_storage_account_id" {
type = string
}

variable "private_storage_account_id" {
type = string
}

variable "prefix" {
Expand All @@ -25,4 +29,12 @@ variable "env" {

variable "name" {
type = string
}

variable "function_app_id" {
type = string
}

variable "eventgrid_function_app" {
type = string
}
31 changes: 27 additions & 4 deletions modules/FunctionApp/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,48 @@ resource "azurerm_linux_function_app" "function_app" {
location = var.location
resource_group_name = var.rg
service_plan_id = azurerm_service_plan.service_plan.id
storage_account_name = var.sa.name
storage_account_access_key = var.sa.primary_access_key
https_only = true
storage_account_name = var.private_storage_name
storage_account_access_key = var.private_storage_key
virtual_network_subnet_id = var.func_subnet_id

site_config {
cors {
allowed_origins = ["https://portal.azure.com"]
}
always_on = true
application_insights_key = var.instru_key
application_insights_connection_string = var.conn_string
application_stack {
python_version = 3.9 #FUNCTIONS_WORKER_RUNTIME
}
}
app_settings = {
"AzureWebJobsStorage" = "DefaultEndpointsProtocol=https;AccountName=${var.sa.name};AccountKey=${var.sa.primary_access_key};EndpointSuffix=core.windows.net"
"AzureWebJobsStorage" = "DefaultEndpointsProtocol=https;AccountName=${var.private_storage_name};AccountKey=${var.private_storage_key};EndpointSuffix=core.windows.net"
"FUNCTIONS_EXTENSION_VERSION" = "~4"
"FUNCTIONS_WORKER_RUNTIME" = "python"
"FUNCTIONS_WORKER_PROCESS_COUNT" = "1"
"MICROSOFT_PROVIDER_AUTHENTICATION_SECRET" = ""
"APPLICATIONINSIGHTS_CONNECTION_STRING" = var.conn_string
}
}

resource "azurerm_eventgrid_system_topic" "eventgrid_topic" {
name = "egt-${var.prefix}-${var.env}-001"
location = var.location
resource_group_name = var.rg
source_arm_resource_id = var.public_storage_account_id
topic_type = "Microsoft.Storage.StorageAccounts"
tags = var.tags
}

resource "azurerm_eventgrid_system_topic_event_subscription" "event_subscription" {
name = "es-${var.prefix}-${var.env}-001"
system_topic = azurerm_eventgrid_system_topic.eventgrid_topic.name
resource_group_name = var.rg
azure_function_endpoint {
function_id = "${azurerm_linux_function_app.function_app.id}/functions/${azurerm_linux_function_app.function_app.name}"
max_events_per_batch = 1
preferred_batch_size_in_kilobytes = 64
}
included_event_types = ["Microsoft.Storage.BlobCreated"]
}
7 changes: 7 additions & 0 deletions modules/FunctionApp/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "function_app_id" {
value = azurerm_linux_function_app.function_app.id
}

output "function_app" {
value = azurerm_linux_function_app.function_app
}
21 changes: 19 additions & 2 deletions modules/FunctionApp/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@ variable "tags" {
type = map(string)
}

variable "public_storage_account_id" {
type = string
}


variable "private_storage_name" {
type = string
}

variable "private_storage_key" {
type = string
}

variable "sa" {
variable "private_storage_account_id" {
description = "storage account"
}

Expand All @@ -30,4 +42,9 @@ variable "instru_key" {
variable "conn_string" {
type = string
sensitive = true
}
}

variable "func_subnet_id" {
type = string
}

2 changes: 1 addition & 1 deletion modules/Monitoring/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ resource "azurerm_application_insights" "application_insights" {
location = var.location
resource_group_name = var.rg
workspace_id = azurerm_log_analytics_workspace.log_analytics_workspace.id
application_type = "web"
application_type = "other"
}
21 changes: 19 additions & 2 deletions modules/Network/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ resource "azurerm_subnet" "frontend" {
}

resource "azurerm_subnet" "compute" {
name = "com-subnet-${var.prefix}-${var.env}-001"
name = "comp-subnet-${var.prefix}-${var.env}-001"
resource_group_name = var.rg
virtual_network_name = azurerm_virtual_network.vnet1.name
address_prefixes = ["10.100.2.0/24"]
service_endpoints = ["Microsoft.Storage"]
service_endpoints = ["Microsoft.Storage", "Microsoft.Sql"]
delegation {
name = "compute-instance"
service_delegation {
Expand All @@ -29,11 +29,28 @@ resource "azurerm_subnet" "compute" {
}
}


resource "azurerm_subnet" "backend" {
name = "be-subnet-${var.prefix}-${var.env}-001"
resource_group_name = var.rg
virtual_network_name = azurerm_virtual_network.vnet1.name
address_prefixes = ["10.100.3.0/24"]
service_endpoints = ["Microsoft.Storage"]
}

resource "azurerm_subnet" "function" {
name = "func-subnet-${var.prefix}-${var.env}-001"
resource_group_name = var.rg
virtual_network_name = azurerm_virtual_network.vnet1.name
address_prefixes = ["10.100.4.0/24"]
service_endpoints = ["Microsoft.Storage", "Microsoft.Sql"]
delegation {
name = "func-delegation"
service_delegation {
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
name = "Microsoft.Web/serverFarms"
}
}
}

resource "azurerm_private_dns_zone" "zones" {
Expand Down
4 changes: 4 additions & 0 deletions modules/Network/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ output "backend_subnet_id" {

output "compute_subnet_id" {
value = azurerm_subnet.compute.id
}

output "function_subnet_id" {
value = azurerm_subnet.function.id
}
Loading
Loading