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

[Bug]: Error: The terraform-provider-aws_v4.67.0_x5 plugin crashed! #32165

Open
jsparrow7777 opened this issue Jun 22, 2023 · 3 comments
Open
Labels
crash Results from or addresses a Terraform crash or kernel panic. service/ecs Issues and PRs that pertain to the ecs service.

Comments

@jsparrow7777
Copy link

Terraform Core Version

1.4.0

AWS Provider Version

5.1.0

Affected Resource(s)

  • aws_ecs_task_definition

Expected Behavior

It should add an environment variable if it's value is not null, else ignore it :

environment = [
        {
            name  = "ENV_VAR1"
            value = var.value1
        },
        var.ENV_VAR2 != null ? {
            name  = "ENV_VAR2"
            value = var.value2
        } : null
]

Actual Behavior

Stack trace from the terraform-provider-aws_v4.67.0_x5 plugin:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xa4dd396]

Error: The terraform-provider-aws_v4.67.0_x5 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

Relevant Error/Panic Output Snippet

Error: Plugin did not respond
│
│   with aws_alb_target_group.depTargetGroup,
│   on lb_resources.tf line 29, in resource "aws_alb_target_group" "TargetGroup":
│   29: resource "aws_alb_target_group" "TargetGroup" {
│
│ The plugin encountered an error, and failed to respond to the
│ plugin.(*GRPCProvider).ReadResource call. The plugin logs may contain more
│ details.
╵
╷
│ Error: Request cancelled
│
│   with aws_alb.depDB_NLB,
│   on lb_resources.tf line 45, in resource "aws_alb" "NLB":
│   45: resource "aws_alb" "NLB" {
│
│ The plugin.(*GRPCProvider).ReadResource request was cancelled.
╵
╷
│ Error: Request cancelled
│
│   with aws_security_group.sgEFS,
│   on securitygroup.tf line 102, in resource "aws_security_group" "EFS":
│  102: resource "aws_security_group" "EFS" {
│
│ The plugin.(*GRPCProvider).ReadResource request was cancelled.
╵
Releasing state lock. This may take a few moments...

Stack trace from the terraform-provider-aws_v4.67.0_x5 plugin:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xa4dd396]

goroutine 1045 [running]:
github.com/hashicorp/terraform-provider-aws/internal/service/ecs.containerDefinitions.OrderEnvironmentVariables.func1(0xc005059d88?, 0xc008260c50?)
	github.com/hashicorp/terraform-provider-aws/internal/service/ecs/task_definition_equivalency.go:108 +0x36
sort.insertionSort_func({0xc004176848?, 0xc0050f4be0?}, 0x0, 0x9)
	sort/zsortfunc.go:12 +0xb1
sort.pdqsort_func({0xc004176848?, 0xc0050f4be0?}, 0xc005059d88?, 0x0?, 0xc0079c93b0?)
	sort/zsortfunc.go:73 +0x345
sort.Slice({0xbbdbde0, 0xc005059d88}, 0x9?)
	sort/slice.go:23 +0x97
github.com/hashicorp/terraform-provider-aws/internal/service/ecs.containerDefinitions.OrderEnvironmentVariables({0xc0050f4b60, 0x1, 0xbda1480?})
	github.com/hashicorp/terraform-provider-aws/internal/service/ecs/task_definition_equivalency.go:107 +0x85
github.com/hashicorp/terraform-provider-aws/internal/service/ecs.ResourceTaskDefinition.func2({0xbda1480?, 0xc0056d2a50?})
	github.com/hashicorp/terraform-provider-aws/internal/service/ecs/task_definition.go:83 +0x52
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.schemaMap.diffString(0xc000680000?, {0xe85b0ee, 0x15}, 0xc00117aa00, 0xc004176ad8, {0xfcae850, 0xc0055e6980}, 0x0)
	github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/schema.go:1550 +0x103
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.schemaMap.diff(0x6?, {0xfca11c0, 0xc005206060}, {0xe85b0ee, 0x15}, 0xc00117aa00, 0xc0055e6900, {0xfcae850?, 0xc0055e6980}, 0x0)
	github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/schema.go:1129 +0x16b
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.schemaMap.Diff(0xc0011762a0, {0xfca11c0, 0xc005206060}, 0xc00827fe10, 0xc0052105d0, 0xc0010fa9a8, {0xe78cb00, 0xc0003f0c00}, 0x0)
	github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/schema.go:679 +0x353
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).SimpleDiff(0xfca22d0?, {0xfca11c0?, 0xc005206060?}, 0xc00827fe10, 0xca76e80?, {0xe78cb00?, 0xc0003f0c00?})
	github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/resource.go:890 +0x6c
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).PlanResourceChange(0xc002801698, {0xfca11c0?, 0xc0051e4240?}, 0xc004945130)
	github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/grpc_provider.go:741 +0x9c9
github.com/hashicorp/terraform-plugin-mux/tf5muxserver.muxServer.PlanResourceChange({0xc004097380, 0xc0040973e0, {0xc004729ea0, 0x2, 0x2}, {0x0, 0x0, 0x0}, {0x0, 0x0, ...}, ...}, ...)
	github.com/hashicorp/terraform-plugin-mux@v0.10.0/tf5muxserver/mux_server_PlanResourceChange.go:56 +0x1d8
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).PlanResourceChange(0xc0034fabe0, {0xfca11c0?, 0xc0051e14d0?}, 0xc002aaf810)
	github.com/hashicorp/terraform-plugin-go@v0.15.0/tfprotov5/tf5server/server.go:783 +0x574
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_PlanResourceChange_Handler({0xe4780c0?, 0xc0034fabe0}, {0xfca11c0, 0xc0051e14d0}, 0xc002aaf7a0, 0x0)
	github.com/hashicorp/terraform-plugin-go@v0.15.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:401 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0048ea1e0, {0xfcb1a60, 0xc0058da680}, 0xc004ff07e0, 0xc0059698f0, 0x168fe688, 0x0)
	google.golang.org/grpc@v1.54.0/server.go:1345 +0xdf0
google.golang.org/grpc.(*Server).handleStream(0xc0048ea1e0, {0xfcb1a60, 0xc0058da680}, 0xc004ff07e0, 0x0)
	google.golang.org/grpc@v1.54.0/server.go:1722 +0xa2f
google.golang.org/grpc.(*Server).serveStreams.func1.2()
	google.golang.org/grpc@v1.54.0/server.go:966 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
	google.golang.org/grpc@v1.54.0/server.go:964 +0x28a

Error: The terraform-provider-aws_v4.67.0_x5 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

Terraform Configuration Files

variable "ENV_VAR2" {
    description = "optional Environment var"
    type        = string
    default     = null
}

resource "aws_ecs_task_definition" "SampleTDF" {
    family                           = "xyz"
    requires_compatibilities         = ["FARGATE"]
    cpu                              = 1024
    memory                           = 4096
    network_mode                     = "awsvpc"
    execution_role_arn               = "${local.ecsTaskExecutionRole}"
    container_definitions             = jsonencode([
    {
        name = "xxxx"
        image = "xxxx"
        essential = true
        logConfiguration = {
            logDriver = "awslogs"
            options = {
                awslogs-group = "/ecs/abcd"
                awslogs-region = "${var.region}"
                awslogs-stream-prefix = "ecs"
            }
        }
        cpu = 0
        environment = [
                {
                    name  = "ENV_VAR1"
                    value = var.ENV_VAR1
                },
                var.ENV_VAR2 != null ? {
                    name  = "ENV_VAR2"
                    value = var.ENV_VAR2
                } : null
        ]
        

        ulimits                 = null
    }])
}

Steps to Reproduce

run terraform plan / terraform apply

Debug Output

No response

Panic Output

No response

Important Factoids

This error only comes when I keep the variable value as null.
I want this environment variable to be added only if my variable is not null.

References

#31443

Would you like to implement a fix?

None

@jsparrow7777 jsparrow7777 added bug Addresses a defect in current functionality. needs-triage Waiting for first response or review from a maintainer. labels Jun 22, 2023
@github-actions
Copy link

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@github-actions github-actions bot added crash Results from or addresses a Terraform crash or kernel panic. service/ecs Issues and PRs that pertain to the ecs service. service/elbv2 Issues and PRs that pertain to the elbv2 service. service/vpc Issues and PRs that pertain to the vpc service. labels Jun 22, 2023
@trevorrea
Copy link
Contributor

Could be linked to #31722

If you are limiting memory on your Terraform runner increase it and see if the error still occurs.

@justinretzolk
Copy link
Member

Hey @jsparrow7777 👋 Thank you for taking the time to raise this! Based on some testing I've done, this appears to be due to passing a literal null value in the list of maps for the environment argument. In doing that testing, I found a way to restructure things a bit to get around this. I'm using output blocks in the following example to illustrate.

First, looking at the example that you provided, I constructed the following sample configuration:

variable "ENV_VAR2" {
  description = "optional Environment var"
  type        = string
  default     = null
}

variable "ENV_VAR1" {
  description = "a Environment var that has a value"
  type        = string
  default     = "avalue"
}

output "example" {
  value = [
    {
      name  = "ENV_VAR1"
      value = var.ENV_VAR1
    },
    var.ENV_VAR2 != null ? {
      name  = "ENV_VAR2"
      value = var.ENV_VAR2
    } : null
  ]
}

When running a terraform plan, the following is displayed for the example output (note the literal null):

$ terraform plan

Changes to Outputs:
  + example = [
      + {
          + name  = "ENV_VAR1"
          + value = "avalue"
        },
      + null,
    ]

To get around this, I introduced a locals block as well as a for loop (omitting the variable definition for brevity):

locals {
  env_vars = [
    {
      name  = "ENV_VAR1"
      value = var.ENV_VAR1
    },
    var.ENV_VAR2 != null ? {
      name  = "ENV_VAR2"
      value = var.ENV_VAR2
    } : null
  ]
}

output "example" {
  value = [for var in local.env_vars : var if var != null]
}

With this change, the example output now reads as following when terraform plan is run:

$ terraform plan

Changes to Outputs:
  + example = [
      + {
          + name  = "ENV_VAR1"
          + value = "avalue"
        },
    ]

When passing a value to the ENV_VAR2 variable using terraform plan -var ENV_VAR2="anothervalue", the following is produced:

$ terraform plan -var ENV_VAR2="anothervalue"

Changes to Outputs:
  + example = [
      + {
          + name  = "ENV_VAR1"
          + value = "avalue"
        },
      + {
          + name  = "ENV_VAR2"
          + value = "anothervalue"
        },
    ]

This will get around passing a literal null value, and should get you past this error. To clean up the configuration a bit, you may consider passing the variables as a map, but I understand that might not be desirable in your configuration. I came up with the following to accomplish that, with the same result on the outputs:

variable "ENV_VAR2" {
  description = "optional Environment var"
  type        = map(string)
  default     = null
}

variable "ENV_VAR1" {
  description = "not optional Environment var"
  type        = map(string)
  default = {
    name  = "ENV_VAR1"
    value = "thisisavalue"
  }
}

locals {
  env_vars = [var.ENV_VAR1, var.ENV_VAR2]
}

output "locals" {
  value = [for var in local.env_vars : var if var != null]
}

@justinretzolk justinretzolk added waiting-response Maintainers are waiting on response from community or contributor. and removed bug Addresses a defect in current functionality. service/elbv2 Issues and PRs that pertain to the elbv2 service. needs-triage Waiting for first response or review from a maintainer. service/vpc Issues and PRs that pertain to the vpc service. labels Jun 26, 2023
@github-actions github-actions bot removed the waiting-response Maintainers are waiting on response from community or contributor. label Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Results from or addresses a Terraform crash or kernel panic. service/ecs Issues and PRs that pertain to the ecs service.
Projects
None yet
Development

No branches or pull requests

3 participants