The Terraform azurerm_image
data source lets you use a RegEx to identify a machine image whose ID matches the regular expression.
What RegEx should be used to retrieve an image that includes the string
MyImageName
and that takes the complete form/subscriptions/abc-123-def-456-ghi-789-jkl/resourceGroups/MyResourceGroupName/providers/Microsoft.Compute/images/MyImageName1618954096
?
The following version of the RegEx is throwing an error because it will not accept two *
characters. However, when we only used the trailing *
, the image was not retrieved.
data "azurerm_image" "search" {
name_regex = "*MyImageName*"
resource_group_name = var.resourceGroupName
}
Note that the results only return a single image so you do not need to worry about multiple images being returned. There is a flag that can be set to specify either ascending or descending sorting to retrieve the oldest or the newest match.
The precise error we are getting is:
Error: "name_regex": error parsing regexp: missing argument to repetition operator: `*`
Nick's Suggestion
Per @Nick's suggestion, we tried:
data "azurerm_image" "search" {
name_regex = "/MyImageName[^/]+$"
resource_group_name =
var.resourceGroupName
}
But the result is:
Error: No Images were found for Resource Group "MyResourceGroupName"
We checked in the Azure Portal and there is an image that includes MyImageName
in its name within the resource group named MyResourceGroupName
. We also confirmed that Terraform is running as the subscription owner, so we imagine that the subscription owner has sufficient authorization to filter image names.
What else can we try?
*
means repeat the previous character 0 or more times. In your "regex", you have no character prior to the first*
, hence the error. Your second regex doesn't work because it matches the stringMyImageNam
followed by 0 or moree
s. You probably want something like/MyImageName[^/]+$
– Nick.+/MyImageName[^/]+
? Unfortunately I can't see any docs on this parameter to give any more clues. btw have you tried putting in the exact name (as aname
rather than aname_regex
) to check that that matches? – Nick"..."
, we also got the same error. – CodeMed