Create Azure Storage Queue using ARM template
Asked Answered
O

5

10

Is there a way to create Azure Storage Queues through ARM templates? I can find a way to create containers, but cannot find anything related to creation of Storage Queue through ARM.

Osei answered 10/8, 2018 at 8:50 Comment(0)
P
10

Seems that support for it is available, but maybe not officially as of 29.07.2020. The documentation is available at: https://learn.microsoft.com/en-us/azure/templates/microsoft.storage/storageaccounts/queueservices/queues

Here is what worked for me:

"variables": {
    "storageAccountName": "[toLower(concat('sa', 'demo', parameters('environmentName')))]"
},
"resources": [
    {
        "type": "Microsoft.Storage/storageAccounts",
        "name": "[variables('storageAccountName')]",
        "location": "[parameters('location')]",
        "apiVersion": "2019-06-01",
        "sku": {
            "name": "[parameters('storageAccountType')]"
        },
        "kind": "StorageV2",
        "properties": {}
    },
    {
        "name": "[concat(variables('storageAccountName'), '/default/myqueue01')]",
        "type": "Microsoft.Storage/storageAccounts/queueServices/queues",
        "apiVersion": "2019-06-01",
        "dependsOn": [
            "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
        ],
        "properties": {
            "metadata": {}

        }
    }
],

Let me know if it works for you.

Pascual answered 29/7, 2020 at 19:17 Comment(1)
When it's documented, it is officially supported. At the time of writing my answer, it has not been documented yet, not even as preview, the documentation itself says it was created 2020-06-15. I included the info and an executable example in my answer. +1 for you for letting us know about the support!Perigon
C
5

No, you can't create Azure Storage Queues through ARM templates but I doubt it is necessary because when you use e. g. the .NET SDK to interact with the queue, you can call the CreateIfNotExistsAsync() method to create it. Example:

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the queue client.
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

// Retrieve a reference to a container.
CloudQueue queue = queueClient.GetQueueReference("myqueue");

// Create the queue async if it doesn't already exist
await queue.CreateIfNotExistsAsync();

Source

Chronology answered 10/8, 2018 at 9:0 Comment(5)
Yes we can do that from the .NET SDK, but we need it for automating the resource deployment through ARM templates.Osei
I thought I showed you why you don't need it. You provision the Storage Account using ARM and use the CreatIfNotExists method whenever you have to interact with the queue.Chronology
@MartinBrandl we need to be able to this when creating eventsubscriptions that points to a queueAffirmative
@MartinBrandl: i have a use case where I have a logic app that needs to write to a queue.Anytime
@DanielMacias If Logic App doesn't do this for you, consider outsource the "write to a queue" task to an Azure Function (PowerShell, C# or whatever) and use the CreateIfNotExists SDK Method there. Then you can Invoke the Function within your logic appChronology
P
4

ARM gradually adds support for creation of sub-resources of storage accounts:

ARM template describing a storage account with a queue:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "environment": {
      "type": "string",
      "allowedValues": [
        "dev",
        "test",
        "prod"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "storageAccountSkuName": {
      "type": "string",
      "defaultValue": "Standard_LRS"
    },
    "storageAccountSkuTier": {
      "type": "string",
      "defaultValue": "Standard"
    }
  },
  "variables": {
    "uniquePart": "[take(uniqueString(resourceGroup().id), 4)]",
    "storageAccountName": "[concat('mystorageaccount', variables('uniquePart'), parameters('environment'))]",
    "queueName": "myqueue"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "apiVersion": "2019-06-01",
      "sku": {
        "name": "[parameters('storageAccountSkuName')]",
        "tier": "[parameters('storageAccountSkuTier')]"
      },
      "kind": "StorageV2",
      "properties": {},
      "resources": [
        {
          "name": "[concat('default/', variables('queueName'))]",
          "type": "queueServices/queues",
          "apiVersion": "2019-06-01",
          "dependsOn": [
            "[variables('storageAccountName')]"
          ],
          "properties": {
            "metadata": {}
          }
        }
      ]
    }
  ]
}

Before the support was added, the resources could be created via other means (best to worst):

  1. during deployment of ARM templates, using tricks to execute arbitrary code
  2. during deployment overall, using Azure CLI called by the deployment tool
  3. in application startup code, using the Azure SDK

The option number two for queues uses the az storage queue create command.

You can add in the Azure CLI task in Azure DevOps, hook up the subscription and the give it an inline script like so:

call az storage queue create -n "awesome-queue-1" --connection-string "$(storageAccountConnectionString)"

If you're using a Windows build agent then you need to include the call to ensure that multiple lines are executed. If you're on a Linux agent then call can be omitted.

That connection string can be exported from your ARM template as an output parameter and then sucked into the DevOps variables using ARM Outputs.

-- Simon Timms: Creating Storage Queues in Azure DevOps @ Western Devs

Perigon answered 7/1, 2020 at 8:33 Comment(0)
S
0

No, there is no way of doing this at this time. Containers were only added recently.

You can always use Azure Function hack: Just call the function as a nested deployment and code the function to do all the work.

Or you can use deployment scripts, which enables running scripts as part of ARM template deployment. (Currently in preview, you need to subscribe to the preview.)

Seismic answered 10/8, 2018 at 8:53 Comment(5)
Have not heard about deployment scripts before. Had to google them. Looks like Azure PS needs to be called there to create the queue. dev.to/omiossec/arm-template-what-s-new-for-2020-4kli learn.microsoft.com/en-us/azure/storage/queues/…Perigon
I found other options of executing code during deployment and posted an answer to another question, along with links to detailed descriptions. https://mcmap.net/q/1176858/-is-there-a-way-trigger-quot-http-trigger-quot-azure-function-after-deploy-arm-templatePerigon
not sure which of them are other. all of them are obvious and 2 of them I mentioned in this discussionSeismic
By other options, I meant Terraform (as an alternative to ARM) and direct use of ACI.Perigon
terraform is not an option. you might say that you can use the portal as an alternative, or powershell or rest calls, etc. there are endless options like these. ACI is just another form of azure functions, more or less. you can create a VM and use custom script extension, for example. again, endless possibilities.Seismic
D
0
param storageAccountName string 

param maxAgeInSeconds int 
resource storageAcc 'Microsoft.Storage/storageAccounts@2022-05-01'existing={
  name:storageAccountName
}


resource queueServices 'Microsoft.Storage/storageAccounts/queueServices@2022-05-01' = {
  name: 'default'
  parent:storageAcc
  properties: {

    cors: {
      corsRules: [
        {
          allowedHeaders: [
            'string'
          ]
          allowedMethods: [
            'string'
          ]
          allowedOrigins: [
            'string'
          ]
          exposedHeaders: [
            'string'
          ]
          maxAgeInSeconds: maxAgeInSeconds
        }
      ]
    }
    
  }
}

Dannydannye answered 21/11, 2022 at 11:20 Comment(3)
a storage queue service can be added that way with bicep language after that create a queueDannydannye
learn.microsoft.com/en-us/azure/templates/microsoft.storage/…Dannydannye
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Baerl

© 2022 - 2024 — McMap. All rights reserved.