0

One of my projects consists of Stored Procs, Triggers, UDfs in Azure CosmosDB. We would like to automate it so it is easy to deploy to different environments, e.g. Dev QA, Prod, in the same effect of using Infrastructure as Code tool, Terraform

One of the possible solutions is by syncing them to an Azure Repo, but it seems it is not supported at the moment. Connecting GitHub below is disabled, and no support is available for Azure Repo. enter image description here

Any idea for other solutions.

Update:

This is another solution but still is not the best.

https://learn.microsoft.com/en-us/azure/cosmos-db/sql/how-to-use-stored-procedures-triggers-udfs#stored-procedures

1 Answer 1

1

You can use ARM/Bicep templates, AzureCLI, Azure Powershell and various SDKs to manage the SQL resources. You can even use Terraform:

resource "azurerm_cosmosdb_sql_stored_procedure" "example" {
  name                = "test-stored-proc"
  resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
  account_name        = azurerm_cosmosdb_account.example.name
  database_name       = azurerm_cosmosdb_sql_database.example.name
  container_name      = azurerm_cosmosdb_sql_container.example.name

  body = <<BODY
      function () { var context = getContext(); var response = context.getResponse(); response.setBody('Hello, World'); }
BODY
}

I would try to use the same tooling as you are already using to maintain the database containers. If that does not make sense in your project, I would probably prefer AzureCLI instead of ARM templates in this specific case, because of how cumbersome it can be to maintain the function code inside the ARM template file.

With this command you can upload the contents of a sproc.js file as the function body:

az cosmosdb sql stored-procedure create \
    --account-name myaccount \
    --body @sproc.js \
    --container-name mycontainer  \
    --database-name mydb \
    --name myfunc \
    --resource-group myrg
Sign up to request clarification or add additional context in comments.

5 Comments

Embedding code inside it like that is not the best way. Please see my update, which is better but still not the ideal one.
Is it possible to load files instead of hard coding the procs like that?
That is why I would use Azure CLI in this case . I added an example of what I mean.
I guess the CLI needs to detect whether or not it already exists? Terraform cannot load a file like CLI. I think you might want to add the link for it. Your answer is good, but Microsoft's solution is not the best at the moment. registry.terraform.io/providers/hashicorp/azurerm/latest/docs/….
You can always use update, even if it does not exist yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.