Deploying Azure Email Communication Service with Bicep

To be able to use send Emails through the Azure Communication Service you need a few ressources that are best created using Infrastructure-as-Code – in this case with Bicep.

Here’s what we need:

  • Email Communication Service
  • Email Communication Services Domain – either provided by Azure which is good for testing purposes or a custom one for production
  • SenderUsername for the domain (e.g. noreply@mydomain.com)
  • Communication Service that is linked to the Domain

To support different environments we also want to use an Azure-managed domain e.g. for dev & test scenarios and a proper custom domain for production.

Note

Only a sucessfully verified Email Communication Service Domain can be linked to the Communication Service. So you might need to run the script, verify the domain when it first fails and then run the script again.

Code

Here’s the full code which can be called directly as a module by just passing the info if we’re in a production environment or not.

param isProd bool = false
param communicationServiceName string = 'cs-medienstudio-dev'
param emailServiceName string = 'es-medienstudio-dev'
// Email Communication Service
resource emailService 'Microsoft.Communication/emailServices@2023-03-31' = {
name: emailServiceName
location: 'global'
properties: {
dataLocation: 'Europe'
}
}
// Email Communication Services Domain (Azure Managed)
resource emailServiceAzureDomain 'Microsoft.Communication/emailServices/domains@2023-03-31' = if (!isProd) {
parent: emailService
name: 'AzureManagedDomain'
location: 'global'
properties: {
domainManagement: 'AzureManaged'
userEngagementTracking: 'Disabled'
}
}
// SenderUsername (Azure Managed Domain)
resource senderUserNameAzureDomain 'Microsoft.Communication/emailServices/domains/senderUsernames@2023-03-31' = if (!isProd) {
parent: emailServiceAzureDomain
name: 'donotreply'
properties: {
username: 'DoNotReply'
displayName: 'DoNotReply'
}
}
// Email Communication Services Domain (Customer Managed)
resource emailServiceCustomDomain 'Microsoft.Communication/emailServices/domains@2023-03-31' = if (isProd) {
parent: emailService
name: 'pentenrieder.dev'
location: 'global'
properties: {
domainManagement: 'CustomerManaged '
userEngagementTracking: 'Disabled'
}
}
// SenderUsername (Customer Managed Domain)
resource senderUserNameCustomDomain 'Microsoft.Communication/emailServices/domains/senderUsernames@2023-03-31' = if (isProd) {
parent: emailServiceCustomDomain
name: 'donotreply'
properties: {
username: 'DoNotReply'
displayName: 'DoNotReply'
}
}
// Link the correct domain based on the environment
var emailServiceResource = isProd ? emailServiceCustomDomain.id : emailServiceAzureDomain.id
// Communication Service
resource communcationService 'Microsoft.Communication/communicationServices@2023-03-31' = {
name: communicationServiceName
location: 'global'
properties: {
dataLocation: 'Europe'
linkedDomains: [
emailServiceResource
]
}
}
// put this connection string into a key vault or (even better) use a Managed Identity to access Communication Service
var communicationServiceConnectionString = communcationService.listKeys().primaryConnectionString

Also check out Philipp’s blog post about how to use the Azure Email Communication Service with .NET.

One thought on “Deploying Azure Email Communication Service with Bicep”

  1. Hey Thomas, It was quite a insightful article for me as I’m trying to deploy about 100 email communication service to my Azure account.

    I’m from non technical background and trying to understand how to deploy around 100 communication services with 100 different domains. Your article is the closet thing I found on internet.

    Can you please give me some idea on how to do this? Deploying 100 communication service and 100 domains into my account and how to verify these domains after all.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.