Search code examples
azureconnection-stringazure-rm-template

Output Azure SQL Database ConnectionString after template deployment


I have an ARM template that (among others) creates a database on an Azure SQL server (which is also created by the template).

I need to output the database ADO.NET connectionstring.

Becuase I wasn't sure what the key is called, I am outputting the whole object: This is what I have on the JSON template file:

 "DatabaseConnectionString": {
      "type": "object",
      "value": "[listkeys(variables('dbResourceId'), variables('apiVersion'))]"
    }

The dbResourceId is 100% correct. If I output it instead, I get the correct ID and the apiVersion is the same I use when creating the DB. BUT, I get this error:

"code": "NotFound",

"message": "Resource not found for the segment 'listkeys'.",

The database is being created correctly

I have the exact same pattern/idea with a service bus and it works perfectly Help, this is killing me


Solution

  • Well, looks like the connection string is simply not a property, this is why it cannot be returned with listkeys, it needs to be "calculated" using concat

    "DatabaseConnectionString": {
      "type": "string",
      "value": "[concat('Server=tcp:',reference(variables('sqlserverName')).fullyQualifiedDomainName,',1433;Initial Catalog=',variables('dbName'),';Persist Security Info=False;User ID=',reference(variables('sqlserverName')).administratorLogin,';Password=',reference(variables('sqlserverName')).administratorLoginPassword,';MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;')]"
    }