Skip to content

Instantly share code, notes, and snippets.

@lAnubisl
Last active November 5, 2024 20:10
Show Gist options
  • Save lAnubisl/07bdb00d43a7a98252cdd6da7f748286 to your computer and use it in GitHub Desktop.
Save lAnubisl/07bdb00d43a7a98252cdd6da7f748286 to your computer and use it in GitHub Desktop.
Azure API Management to Azure Service Bus Integration
resource "azurerm_resource_group" "rg" {
name = "rg-apim-to-servicebus"
location = "westeurope"
}
resource "azurerm_servicebus_namespace" "sbns" {
name = "sbns-brands-test-2023"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku = "Standard"
local_auth_enabled = false
}
resource "azurerm_servicebus_topic" "sbt" {
name = "brands"
namespace_id = azurerm_servicebus_namespace.sbns.id
enable_partitioning = true
}
resource "azurerm_servicebus_subscription" "sbts_adidas" {
name = "adidas"
topic_id = azurerm_servicebus_topic.sbt.id
max_delivery_count = 1
}
resource "azurerm_servicebus_subscription_rule" "sbts_rule_adidas" {
name = "adidas"
subscription_id = azurerm_servicebus_subscription.sbts_adidas.id
filter_type = "SqlFilter"
sql_filter = "brand = 'adidas'"
}
resource "azurerm_servicebus_subscription" "sbts_nike" {
name = "nike"
topic_id = azurerm_servicebus_topic.sbt.id
max_delivery_count = 1
}
resource "azurerm_servicebus_subscription_rule" "sbts_rule_nike" {
name = "nike"
subscription_id = azurerm_servicebus_subscription.sbts_nike.id
filter_type = "SqlFilter"
sql_filter = "brand = 'nike'"
}
resource "azurerm_api_management" "apim" {
name = "apim-test-july-2023"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
publisher_name = "John Doe"
publisher_email = "[email protected]"
sku_name = "Consumption_0"
identity {
type = "SystemAssigned"
}
}
resource "azurerm_role_assignment" "apim_role_assignment" {
scope = azurerm_servicebus_namespace.sbns.id
role_definition_name = "Azure Service Bus Data Sender"
principal_id = azurerm_api_management.apim.identity[0].principal_id
}
resource "azurerm_api_management_named_value" "nv_sb_base_url" {
name = "sb-base-url"
resource_group_name = azurerm_resource_group.rg.name
api_management_name = azurerm_api_management.apim.name
display_name = "sb-base-url"
value = "https://${azurerm_servicebus_namespace.sbns.name}.servicebus.windows.net"
}
resource "azurerm_api_management_named_value" "nv_sb_queue" {
name = "sb-queue_or_topic"
resource_group_name = azurerm_resource_group.rg.name
api_management_name = azurerm_api_management.apim.name
display_name = "sb-queue_or_topic"
value = azurerm_servicebus_topic.sbt.name
}
resource "azurerm_api_management_product" "apim_product" {
product_id = "my_product_id"
api_management_name = azurerm_api_management.apim.name
resource_group_name = azurerm_resource_group.rg.name
display_name = "My Product"
description = "My Product Description"
terms = "My Product Terms"
subscription_required = true
subscriptions_limit = 1
approval_required = true
published = true
}
resource "azurerm_api_management_api" "apim_api" {
name = "example-api-name"
resource_group_name = azurerm_resource_group.rg.name
api_management_name = azurerm_api_management.apim.name
revision = "1"
display_name = "Integrations with Azure Managed Services"
api_type = "http"
path = "path"
protocols = ["https"]
subscription_key_parameter_names {
header = "subscription"
query = "subscription"
}
}
resource "azurerm_api_management_product_api" "apim_product_api" {
api_name = azurerm_api_management_api.apim_api.name
product_id = azurerm_api_management_product.apim_product.product_id
resource_group_name = azurerm_resource_group.rg.name
api_management_name = azurerm_api_management.apim.name
}
resource "azurerm_api_management_api_operation" "apim_api_operation_servicebus" {
operation_id = "to-service-bus"
api_name = azurerm_api_management_api.apim_api.name
api_management_name = azurerm_api_management_api.apim_api.api_management_name
resource_group_name = azurerm_api_management_api.apim_api.resource_group_name
display_name = "Send data to the service bus queue"
method = "POST"
url_template = "/servicebus"
description = "Send data to the service bus queue"
}
resource "azurerm_api_management_api_operation_policy" "apim_api_operation_policy_servicebus" {
api_name = azurerm_api_management_api_operation.apim_api_operation_servicebus.api_name
api_management_name = azurerm_api_management_api_operation.apim_api_operation_servicebus.api_management_name
resource_group_name = azurerm_api_management_api_operation.apim_api_operation_servicebus.resource_group_name
operation_id = azurerm_api_management_api_operation.apim_api_operation_servicebus.operation_id
xml_content = <<XML
<policies>
<inbound>
<base />
<choose>
<when condition="@(context.Request.Headers.GetValueOrDefault("brand","") == "adidas")">
<set-variable name="brand" value="adidas" />
</when>
<when condition="@(context.Request.Headers.GetValueOrDefault("brand","") == "nike")">
<set-variable name="brand" value="nike" />
</when>
<otherwise>
<return-response>
<set-status code="400" reason="Bad Request" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var json = new JObject() {{"Error", "Invalid brand"}} ;
return json.ToString(Newtonsoft.Json.Formatting.None);
}</set-body>
</return-response>
</otherwise>
</choose>
<authentication-managed-identity resource="https://servicebus.azure.net/" />
<set-header name="BrokerProperties" exists-action="override">
<value>@{
var json = new JObject();
json.Add("MessageId", context.RequestId);
json.Add("brand", (string)context.Variables["brand"]);
return json.ToString(Newtonsoft.Json.Formatting.None);
}</value>
</set-header>
<set-backend-service base-url="{{sb-base-url}}" />
<rewrite-uri template="{{sb-queue_or_topic}}/messages" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<choose>
<when condition="@(context.Response.StatusCode == 201)">
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var json = new JObject() {{"OperationId", context.RequestId}} ;
return json.ToString(Newtonsoft.Json.Formatting.None);
}</set-body>
</when>
</choose>
</outbound>
<on-error>
<base />
</on-error>
</policies>
XML
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment