hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.58k stars 9.54k forks source link

Crash when running `terraform init` (crash.log included) #21741

Closed tjarjoura closed 5 years ago

tjarjoura commented 5 years ago

Terraform Version

Terraform v0.12.1

Terraform Configuration Files

[tyler@tjarjoura-pc web_unit_tests]$ cat ../../modules/web_unit_tests/main.tf 
variable "web_tag" {
  type        = string
  description = "Tag of the web image to run unit tests on."
}

module "ecs_cluster" {
  source                   = "../ecs_cluster"
  cluster_name             = "unit-tests"
  instance_type            = "c5.4xlarge"
  max_cluster_size         = 10
  desired_cluster_capacity = 1
  ec2_security_groups      = []
}

data "template_file" "container_definitions" {
  template = "${file("${path.module}/container_definitions.json")}"

  vars = {
    tag                     = var.web_tag
    log_group               = "web-unittest"
    log_stream              = "web-unittest-${var.web_tag}"
  }
}

resource "aws_ecs_task_definition" "web-unit-tests" {
  family = "web-unit-tests"
  execution_role_arn    = "arn:aws:iam::697549588058:role/sandbox"
  container_definitions = "${data.template_file.container_definitions.rendered}"
  volume {
    name = "logs"
  }
  volume {
    name = "sumo-sources"
  }
[tyler@tjarjoura-pc web_unit_tests]$ cat ../../modules/ecs_cluster/*
variable "instance_type" {
  default = "t3.medium"
}

variable "cluster_name" {
}

variable "max_cluster_size" {
  default = 10
}

variable "desired_cluster_capacity" {
  default = 3
}

resource "aws_ecs_cluster" "cluster" {
  name = var.cluster_name
}

variable "ec2_security_groups" {
  description = "Used in asg launch configs"
  type        = list(string)
}

data "aws_ami" "latest_ecs" {
  most_recent = true
  owners      = ["591542846629"] # AWS

  filter {
    name   = "name"
    values = ["amzn2-ami-ecs-hvm-2.0.*-x86_64-ebs"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}

locals {
  // TODO: turn into IaC
  asg_role          = "arn:aws:iam::697549588058:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
  ecs_instance_role = "arn:aws:iam::697549588058:instance-profile/sandbox"
}

data "template_file" "user_data" {
  template = file("${path.module}/user_data.sh")

  vars = {
    cluster_name = var.cluster_name
  }
}

resource "aws_cloudwatch_log_group" "log_group" {
  name = var.cluster_name
}

resource "aws_launch_configuration" "asg_conf" {
  name_prefix   = var.cluster_name
  image_id      = data.aws_ami.latest_ecs.id
  instance_type = var.instance_type

  // TODO: think about keypair
  key_name             = "royzheng"
  security_groups      = var.ec2_security_groups
  user_data            = data.template_file.user_data.rendered
  iam_instance_profile = local.ecs_instance_role

  # TODO LEGO-63 do not give public IP

  root_block_device {
    volume_size = 48
  }
  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "asg" {
  name                      = "${var.cluster_name}-asg"
  launch_configuration      = aws_launch_configuration.asg_conf.name
  min_size                  = 0
  max_size                  = var.max_cluster_size
  desired_capacity          = var.desired_cluster_capacity
  health_check_grace_period = 0

  service_linked_role_arn = local.asg_role
  vpc_zone_identifier     = ["subnet-ee7042a8"]

  // TODO: use launch template

  lifecycle {
    create_before_destroy = true
  }

  tag {
    key                 = "Name"
    value               = "TF-ECS Instance - EC2ContainerService-${var.cluster_name}"
    propagate_at_launch = "true"
  }
}

output "ecs_cluster_name" {
  value = aws_ecs_cluster.cluster.name
}

output "log_group_name" {
  value = aws_cloudwatch_log_group.log_group.name
}

[tyler@tjarjoura-pc web_unit_tests]$ cat web-unit-tests.tf 
provider "aws" {
  region                  = "us-west-1"
  shared_credentials_file = "~/.aws/credentials"
  profile                 = "mfa-role"
  version                 = "~> 2.4"
}

terraform {
  required_version = ">= 0.12"
  backend "remote" {
    hostname     = "app.terraform.io"
    organization = "eatclub"

    workspaces {
      name = "web-unit-tests"
    }
  }
}

module "web_unit_tests" {
  source  = "../../modules/web_unit_tests"
  web_tag = "6f9f0916448ff72f1836a85963d25e6f6607d253"
}

Debug Output

[tyler@tjarjoura-pc web_unit_tests]$ terraform init && terraform apply
2019/06/15 12:59:46 [INFO] Terraform version: 0.12.1  
2019/06/15 12:59:46 [INFO] Go runtime version: go1.12.5
2019/06/15 12:59:46 [INFO] CLI args: []string{"/usr/bin/terraform", "init"}
2019/06/15 12:59:46 [DEBUG] Attempting to open CLI config file: /home/tyler/.terraformrc
2019/06/15 12:59:46 Loading CLI configuration from /home/tyler/.terraformrc
2019/06/15 12:59:46 [INFO] CLI command args: []string{"init"}
Initializing modules...
- web_unit_tests in ../../modules/web_unit_tests
- web_unit_tests.ecs_cluster in ../../modules/ecs_cluster

Initializing the backend...
2019/06/15 12:59:49 [ERR] Checkpoint error: Get https://checkpoint-api.hashicorp.com/v1/check/terraform?arch=amd64&os=linux&signature=f76da6d5-94dd-2139-f99f-e025cd5e4208&version=0.12.1: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
2019/06/15 12:59:53 [INFO] Failed to read plugin lock file .terraform/plugins/linux_amd64/lock.json: open .terraform/plugins/linux_amd64/lock.json: no such file or directory

Successfully configured the backend "remote"! Terraform will automatically
use this backend unless the backend configuration changes.
2019/06/15 12:59:53 [INFO] Failed to read plugin lock file .terraform/plugins/linux_amd64/lock.json: open .terraform/plugins/linux_amd64/lock.json: no such file or directory

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (terraform-providers/aws) 2.15.0...
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x50 pc=0x555908be5496]

goroutine 1 [running]:
github.com/hashicorp/terraform/plugin/discovery.(*ProviderInstaller).Get(0xc0001307e0, 0xc000040930, 0x8, 0x55590b374ba0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
    /build/terraform/src/src/github.com/hashicorp/terraform/plugin/discovery/get.go:207 +0xf06
github.com/hashicorp/terraform/command.(*InitCommand).getProviders(0xc000308ea0, 0xc000131030, 0x0, 0x0, 0xc0001760f0, 0x0, 0x0, 0x0)
    /build/terraform/src/src/github.com/hashicorp/terraform/command/init.go:497 +0x417
github.com/hashicorp/terraform/command.(*InitCommand).Run(0xc000308ea0, 0xc0000ca030, 0x0, 0x0, 0xc00054e3e0)
    /build/terraform/src/src/github.com/hashicorp/terraform/command/init.go:338 +0x1023
github.com/hashicorp/terraform/vendor/github.com/mitchellh/cli.(*CLI).Run(0xc00032a140, 0xc00032a140, 0xc00053bd90, 0x1)
    /build/terraform/src/src/github.com/hashicorp/terraform/vendor/github.com/mitchellh/cli/cli.go:255 +0x1f3
main.wrappedMain(0x0)
    /build/terraform/src/src/github.com/hashicorp/terraform/main.go:223 +0xb01
main.realMain(0x0)
    /build/terraform/src/src/github.com/hashicorp/terraform/main.go:100 +0xb6
main.main()
    /build/terraform/src/src/github.com/hashicorp/terraform/main.go:36 +0x3d
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x50 pc=0x555908be5496]

goroutine 1 [running]:
github.com/hashicorp/terraform/plugin/discovery.(*ProviderInstaller).Get(0xc0001307e0, 0xc000040930, 0x8, 0x55590b374ba0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
    /build/terraform/src/src/github.com/hashicorp/terraform/plugin/discovery/get.go:207 +0xf06
github.com/hashicorp/terraform/command.(*InitCommand).getProviders(0xc000308ea0, 0xc000131030, 0x0, 0x0, 0xc0001760f0, 0x0, 0x0, 0x0)
    /build/terraform/src/src/github.com/hashicorp/terraform/command/init.go:497 +0x417
github.com/hashicorp/terraform/command.(*InitCommand).Run(0xc000308ea0, 0xc0000ca030, 0x0, 0x0, 0xc00054e3e0)
    /build/terraform/src/src/github.com/hashicorp/terraform/command/init.go:338 +0x1023
github.com/hashicorp/terraform/vendor/github.com/mitchellh/cli.(*CLI).Run(0xc00032a140, 0xc00032a140, 0xc00053bd90, 0x1)
    /build/terraform/src/src/github.com/hashicorp/terraform/vendor/github.com/mitchellh/cli/cli.go:255 +0x1f3
main.wrappedMain(0x0)
    /build/terraform/src/src/github.com/hashicorp/terraform/main.go:223 +0xb01
main.realMain(0x0)
    /build/terraform/src/src/github.com/hashicorp/terraform/main.go:100 +0xb6
main.main()
    /build/terraform/src/src/github.com/hashicorp/terraform/main.go:36 +0x3d

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Terraform crashed! This is always indicative of a bug within Terraform.
A crash log has been placed at "crash.log" relative to your current
working directory. It would be immensely helpful if you could please
report the crash with Terraform[1] so that we can fix this.

When reporting bugs, please include your terraform version. That
information is available on the first line of crash.log. You can also
get it by running 'terraform --version' on the command line.

[1]: https://github.com/hashicorp/terraform/issues

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Crash Output

https://gist.github.com/tjarjoura/597eb565d1986b0db08763f14bc63954

Expected Behavior

Not crashed.

Actual Behavior

Crashed.

Steps to Reproduce

Run terraform init

Additional Context

References

mildwonkey commented 5 years ago

Hi @tjarjoura! Thanks for reporting this issue, and I'm sorry you've experienced it.

I'd like to figure out why you're seeing this error:

2019/06/15 12:59:53 [INFO] Failed to read plugin lock file .terraform/plugins/linux_amd64/lock.json: open .terraform/plugins/linux_amd64/lock.json: no such file or directory

I suspect if you can work around that, your crash will go away (though we still need to fix things so terraform does not crash in this case!). Does manually creating that directory (.terraform/plugins/linux_amd64) fix the crash?

mildwonkey commented 5 years ago

🤔 actually, I think I sent myself down a wild goose chase - I went back to your crash log and it appears that the directory was created later:

2019/06/15 13:00:41 [DEBUG] checking for provider in ".terraform/plugins/linux_amd64"
2019/06/15 13:00:41 [DEBUG] checking for provider in ".terraform/plugins/linux_amd64"
2019/06/15 13:00:41 [DEBUG] found provider "terraform-provider-aws_v2.15.0_x4"

Please ignore my previous suggestion!

mildwonkey commented 5 years ago

The panic points to this line of code: https://github.com/hashicorp/terraform/blob/master/plugin/discovery/get.go#L207

I'm not sure why it failed to find download URLs - I was able to pull the same provider - but we should guard against nil results here regardless.

Does this error persist if you retry init?

hashibot commented 5 years ago

Hello again!

We didn't hear back from you, so I'm going to close this in the hope that a previous response gave you the information you needed. If not, please do feel free to re-open this and leave another comment with the information my human friends requested above. Thanks!

ghost commented 5 years ago

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.