The new Azure Managed Redis can be deployed with balanced compute and memory with high availability, and useful modules such as RedisJson and RedisSearch. This is really a useful and good pricing options to use Redis as a managed service in Azure. Note that it is not the enterprise redis offering in Azure and managed redis for Azure has more flexible pricing options. However, terraform support for this is yet to be added and will be available in another month or so as per the pull request here. Let's see how to get a Azure Managed Redis deployed with terraform for no using AzAPI.
The expectaion is to have a deployed Azure Managed Redis as shown below.
Here is the step by step details. The full terraform example is in GitHub.
First let's define few local variables. SPN below should have required permissoin to create new resources in the subscription.
Then we need the providers setup as shown below.
To setup Azure managed redis with required modules we can use azapi as shwon below.
Then we can get the host name and port for output as below.
However, getting the password/accesskey is bit challanging with as azapi does not have a direct way to get it as of now.
Therefore, we have to use a workaround as shown below.
Using the always running null resource below, we can extract the accesskey/password for redis and save it in a json file as a temp file.
resource "null_resource" "redis_key" { triggers = { always_run = timestamp() } depends_on = [ azapi_resource.managed_redis, azapi_resource.managed_redis_database ] provisioner "local-exec" { command = <<-SHELL az login --service-principal -u ${local.spn_app_id} -p ${local.spn_pwd} --tenant ${local.tenant_id} az account set --subscription ${local.subscription_id} az extension add --name redisenterprise --upgrade --yes $key = az redisenterprise database list-keys --cluster-name ${azapi_resource.managed_redis.name} --resource-group ${azurerm_resource_group.rg.name} --query primaryKey --output tsv $json = @{ primary_key = $key } | ConvertTo-Json -Compress $json | Out-File -FilePath redis_key.json -Encoding utf8 SHELL interpreter = ["PowerShell"] } }
Then we can have a powershell script setup in the terraform folder with name read-redis-key.ps1.
The script implments a mechanism to read the saved password from the temp file.
Then, we can use below terraform data source to run the above powershell script, to get the password and output it. If you are on linux you may replace powershell with bash script to get the same thing done.
No comments:
Post a Comment