Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_application_insights asks for workspace_id but requires id of the azurerm_log_analytics_workspace in lower case #13348

Closed
JamesDLD opened this issue Sep 14, 2021 · 6 comments · Fixed by #19325
Labels
bug service/application-insights upstream/microsoft Indicates that there's an upstream issue blocking this issue/PR v/2.x (legacy)

Comments

@JamesDLD
Copy link

Terraform Version

Terraform v1.0.6
on darwin_amd64
+ provider registry.terraform.io/hashicorp/azurerm v2.75.0

Expected Behavior

resource "azurerm_application_insights" "example" {
  name                = "tf-test-appinsights"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  workspace_id        = azurerm_log_analytics_workspace.example.workspace_id
  application_type    = "web"
}

Actual Behavior

resource "azurerm_application_insights" "example" {
  name                = "tf-test-appinsights"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  workspace_id        = azurerm_log_analytics_workspace.example.id
  application_type    = "web"
}

Additional Context

On the resource "azurerm_log_analytics_workspace" :

  • The id is The Log Analytics Workspace ID.
  • The workspace_id is The Workspace (or Customer) ID for the Log Analytics Workspace.

And it requires it in lower case, when using a data source on azurerm_log_analytics_workspace we are asked to update the resource -->

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # azurerm_application_insights.ains["xxx"] will be updated in-place
  ~ resource "azurerm_application_insights" "ains" {
        id                                    = "/subscriptions/xxx/resourceGroups/xxx/providers/microsoft.insights/components/xxx"
        name                                  = "xxx"
      ~ workspace_id                          = "/subscriptions/xxx/resourcegroups/xxx/providers/microsoft.operationalinsights/workspaces/xxx" -> "/subscriptions/xxxxxx/resourceGroups/xxx/providers/Microsoft.OperationalInsights/workspaces/xxx"
        # (12 unchanged attributes hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

References

@dhtek
Copy link

dhtek commented Nov 21, 2021

Same issue for me with a new clean deployment...

Provider registry.terraform.io/hashicorp/azurerm v2.85.0

workspace_id = azurerm_log_analytics_workspace.default.id -> no error when executing the Terraform apply but the change is not applied in the Azure portal and App Insights is still using the old way.

workspace_id = azurerm_log_analytics_workspace.default.workspace_id -> error when executing terraform plan with the following error: Can not parse "workspace_id" as a resource id: Cannot parse Azure ID: parse "a_guid": invalid URI for request

@DaleyKD
Copy link

DaleyKD commented Jul 18, 2022

Any update on this? Can someone from TF submit a ticket to MSFT?

@matbest1
Copy link

matbest1 commented Aug 24, 2022

Same problem here:

resource "azurerm_log_analytics_workspace" "this" {
  name                = "${local.stack_name}-la"
  location            = azurerm_resource_group.this.location
  resource_group_name = azurerm_resource_group.this.name
  sku                 = "PerGB2018"
  retention_in_days   = 90
}

resource "azurerm_application_insights" "this" {
  name                = "${local.stack_name}-ai"
  location            = azurerm_resource_group.this.location
  resource_group_name = azurerm_resource_group.this.name
  workspace_id        = azurerm_log_analytics_workspace.this.id
  application_type    = "web"
}
 ~ workspace_id  = "/subscriptions/xxx/resourcegroups/demo-group/providers/microsoft.operationalinsights/workspaces/xxx-demo-la" -> "/subscriptions/xxx/resourceGroups/demo-group/providers/Microsoft.OperationalInsights/workspaces/xxx-demo-la" # forces replacement
Terraform v1.2.6
on windows_amd64
+ provider registry.terraform.io/hashicorp/azurerm v3.19.1

@ms-henglu Could you please have a look at this? Noticed that you commented a similar issue #18030

Update:

Maybe it's possible to use the parsing function from the LogAnalyticsWorkspace resource?

func (id LogAnalyticsWorkspaceId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.OperationalInsights/workspaces/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.WorkspaceName)
}
// LogAnalyticsWorkspaceID parses a LogAnalyticsWorkspace ID into an LogAnalyticsWorkspaceId struct
func LogAnalyticsWorkspaceID(input string) (*LogAnalyticsWorkspaceId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, err
}
resourceId := LogAnalyticsWorkspaceId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}
if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}
if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}
if resourceId.WorkspaceName, err = id.PopSegment("workspaces"); err != nil {
return nil, err
}
if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}
return &resourceId, nil
}

if workspaceRaw, hasWorkspaceId := d.GetOk("workspace_id"); hasWorkspaceId {
applicationInsightsComponentProperties.WorkspaceResourceID = utils.String(workspaceRaw.(string))
}

Update 2:

Seems that the same solution has already been used in the code, like here:

workspaceID, err := loganalyticsParse.LogAnalyticsWorkspaceID(d.Get("log_analytics_workspace_id").(string))

Can provide a PR in case...

Update 3:

Seems that a substantial change to log analytics is being worked on in this PR ( #18098 ), could maybe the team have a look at this after?

@AshutoshNirkhe
Copy link

Seems like this same issue is coming even after using v3.21.1,
parsing segment "staticResourceGroups": expected the segment "resourcegroups" to be "resourceGroups"

@mgattei
Copy link
Contributor

mgattei commented Nov 15, 2022

The issue is still present.

I have tried:
Deleting Log Analytics Workspace resource from state
Deleting Application Insights resource from state
Imported them back, but the provider still asks for recreation.

Can anyone please have a look? Or has anyone any suggestion? I can't update my infrastructure since I have some important data on AI and they will be lost if I recreate the resource...

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug service/application-insights upstream/microsoft Indicates that there's an upstream issue blocking this issue/PR v/2.x (legacy)
Projects
None yet
8 participants