akourt
akourt

Reputation: 5563

Creating an Azure Email Communication Service using Terraform

I am going through the documentation of the AzureRM provider regarding Azure Communication services and I am a bit confused regarding the resources.

At the moment I see that there are two *-commnication_service available:

  1. azurerm_communication_service
  2. azurerm_email_communication_service

Based on the examples I see, I understand that I can simply use the latter of the two to create the communication as well as a azurerm_email_communication_service_domain to create an AzureManaged domain for this email service.

What I don't understand is that there is no way for the resource to provide an output regarding the key and endpoint of the created resource. I see that the only output of this resource is the id of the created resource.

In this example though a more intricate setup is created where a communication-service gets created, along with an email-communication-service a domain and an association between those two.

My question is then, in order for me to be able to fetch the service's credentials for this resource shall I use this setup? Creating a separate communication service, an email communication service, a domain and an association between these two, then ending by getting the credentials from the output? I am bit a confused as to why I can't fetch those directly from the azurerm_email_communication_service resource.

Any input will be welcome!

Upvotes: 0

Views: 512

Answers (1)

Vinay B
Vinay B

Reputation: 2401

Fetching keys and endpoint of azure communication service using terraform.

The azure email communication service doesn't allow you to fetch keys and endpoints you need azure communication sevices along with it. You must create both azurerm_communication_service and azurerm_email_communication_service, along with the domain and association.

However direct fetching of endpoints using terraform is not possible as terraform don't supports this feature and, but keys can be accessed using terraform output modules.

If you need endpoints to be fetched from the communication services, we may need custom script inside a null resource which may be helpful in achieving the requirement.

Terraform configuration:

resource "azurerm_communication_service" "comm_service" {
  name                = "vinay-communication-service"
  resource_group_name = data.azurerm_resource_group.rg.name
  data_location       = "United States"
}

resource "azurerm_email_communication_service" "email_service" {
  name                = "vinay-email-service"
  data_location       = "United States"
  resource_group_name = data.azurerm_resource_group.rg.name
}

resource "azurerm_email_communication_service_domain" "domain" {
  name                     = "AzureManagedDomain"
  email_service_id         = azurerm_email_communication_service.email_service.id
  domain_management        = "AzureManaged"
}

resource "azurerm_communication_service_email_domain_association" "association" {
  email_service_domain_id  = azurerm_email_communication_service_domain.domain.id
  communication_service_id = azurerm_communication_service.comm_service.id
}

resource "null_resource" "fetch_hostname" {
   depends_on = [ azurerm_email_communication_service_domain.domain, azurerm_communication_service.comm_service ]
  provisioner "local-exec" {
    command = <<EOT
    $communicationservice = Get-AzCommunicationService -Name '${azurerm_communication_service.comm_service.name}' -ResourceGroupName '${azurerm_communication_service.comm_service.resource_group_name}'
    $hostname = $communicationservice.HostName
    Write-Output "Communication Service HostName (Endpoint): $hostname"
    EOT
    interpreter = ["pwsh", "-Command"]
  }

  triggers = {
    communication_service_id = azurerm_communication_service.comm_service.id
  }
}

output "communication_service_primary_access_key" {
  value     = azurerm_communication_service.comm_service.primary_key
  sensitive = true
}

output "communication_service_secondary_access_key" {
  value     = azurerm_communication_service.comm_service.secondary_key
  sensitive = true
}

output "communication_service_primary_connection_string" {
  value     = azurerm_communication_service.comm_service.primary_connection_string
  sensitive = true
}

output "communication_service_secondary_connection_string" {
  value     = azurerm_communication_service.comm_service.secondary_connection_string
  sensitive = true
}

Deployment:

enter image description here

enter image description here

enter image description here

Refer:

https://learn.microsoft.com/es-es/powershell/module/az.communication/get-azcommunicationservice?view=azps-12.2.0&viewFallbackFrom=azps-12.0.0

permissions - How to get Azure Email Communication Service Connection String - Stack Overflow check with the answer by crazyoptimist

azurerm_communication_service_email_domain_association | Resources | hashicorp/azurerm | Terraform | Terraform Registry

Upvotes: 2

Related Questions