ARM Template - How to reference a copyIndex() deployment output? - azure

I deploy 30 SQL databases via copyIndex() as sub deployments of the main deployment, I want to be able to reference the outputs of the dynamic deployments when kicking off another deployment. Once all the databases are deployed, I want to then all Azure Monitor metric rules to the DBs, and need their resourceIds (the Output of the db deploy).
The answer here sounds exactly like what I'm trying to do, and I understand that each deployment is chained to have the output of the previous deploy. But then if I want to use the chained up "state" output, is it the very last element in the array that has the full chain? If so is the best way to reference that to just build up the name of the deployment and append on the length of the copyIndex array?
reference(concat('reference', length(variables('types'))).outputs.state.value
As so?

yes, you basically need to construct a name that is the name of the deployment:
referenceX
where X is the number of the last deployment, you can use length() function for that exactly as you suggest it.
the above will work only if you gather the output from all the intermediate steps, obviously

Related

How to get value on Azure ARM uniqueString function in Powershell

Is there a way to get the value returned by uniqueString in Powershell. I am creating the bulk of my environment using ARM templates, but I still need to run Powershell for certain things. Powershell needs to know the resource name suffixes generated by uniqueString. Currently I have these values hard coded.
Also, the value returned by uniqueString is excessive and severely limits resource names, ie espstorage is too long to use with uniqueString. I am considering replacing uniqueString with a CRC32 or .Net String Hash value in my templates -- since I end up hard coding the values anyway in Powershell. But from all the examples, uniqueString appears to be the "correct" way.
I had a similar problem with subscriptionId. My resolution is at post: https://stackoverflow.com/questions/56195642/is-there-a-way-to-get-the-subscriptionid-used-in-task-azure-resource-group-dep

Azure Datafactory V2 - IfCondition activity cannot include another IfCondition activity

I'm using Azure Data factory V2 and I need to implement two levels of checks with in a parent check validation. example: IF Condition one is true Then I need to check for Condition 2. And, If Condition2 is True then Check for Condition 3.
It's kind of hierarchical checks. When I implement using multiple IF Condition activities within parent IF condition activity, then it doesn't work. I do see following error, when i implement it.
"ERROR: IfCondition activity cannot include another IfCondition activity"
ADF- V2 doesn't support multiple IfCondition activities with in a single pipeline.
So as a workaround, we used multiple filter activities upfront and split the pipeline flow before IfCondition Activity is invoked.
Thanks
You can use some of the dynamic functions to implement a more complex logic with and() and or().
You can put several continuous if's that set up variables, and use those variables to determine the outcome at the end.
And if that doesn't work for you, or you don't feel like it, you can always Execute a Pipeline inside of the If that uses another If Activity. But I wouldn't recommend this if you want to keep your monitor as easy to read as possible since it launches another pipeline (or more than one)

Variables across terraform plans and modules?

What's the common pattern for not duplicating variable values across plans?
We have a standard set of tags we use in plans and modules for which we wish to define once and use many. For example: we set CostType tag to values like compute, storage, etc.. We can define it plan level, or module level but that means defining a variable in multiple places which isn't very DRY (don't repeat yourself).
Options
non infrastructure changing module which defines these "global" variables and all modules/plans use that first so the rest of the actions can harvest the values from that plan
use a non infrastructure changing plan store remote state to store variable values and access it as from module/plans
use a tfvars file and handle it via the scripts that wrap terraform actions
devops elves magically handle this problem
How do you solve this problem in your organization?
I used with success symbolic links to link the same variable file in multiple locations.
Symbolic links are well supported by Git and can be used on Windows too (with some care Git Symlinks in Windows).

How to store and reuse terraform interpolation result within resources?

How do I store and reuse terraform interpolation result within resources that do not expose them as output?
example: In aws_ebs_volume , I am calculating my volume size using:
size = "${lookup(merge(var.default_ebs_vol_sizes,var.ebs_vol_sizes),
var.tag_disk_location[var.extra_ebs_volumes[count.index % length(var.extra_ebs_volumes)]])}"
Now I need to reuse the same size for calculating the cost tags in the same resource as well as in corresponding ec2 resource (in same module). How do I do this without copy pasting the entire formula?
PS: I have come across this usecase in multiple scenarios, so the above is just one of the use cases where I need to reuse the interpolated results. Getting the interpolated result using the corresponding data source is one way out in this case but looking for a more straight forward solution.
This is now possible using the local variable available from terraform 0.10.3 onwards.
https://www.terraform.io/docs/configuration/locals.html
Local values assign a name to an expression, that can then be used
multiple times within a module.

Resource Template - Count array items on condition

Is it possible to count the number of items in an array based upon a certain condition in Resource Templates? Similar to how we can use 'Where-Object' within PowerShell. Seems that the 'length' function is only able to count the total number of items.
No, you cannot do that, unless you hack your way through using nested templates. And that is only possible if you want to compare against a specific object, and you would probably need at least 2 levels of indirection.
And i would generally advice against that, unless there's no other option.
but if you want to do that, you would need this function, nested deployments and ARM template way of doing conditionals and I would argue that you would need a state parameter in the nested templates to share the state between those.
The other answer is pretty much old and is outdated.
The ARM template function length(arg1)returns the number of elements in an array, characters in a string, or root-level properties in an object.
https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-array#length

Resources