Terraform Gives errors Failed to load plugin schemas

Viewed 6182

I have below code which I am using for create s3 bucket and cloud front in aws through terraform but terraform gives error. I am using latest version of terraform cli exe for windows. Main.tf Please find below code of main.tf file :

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.70.0"
    }
  }
}

provider "aws" {
    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    region = "${var.aws_region}"
}

resource "aws_s3_bucket" "mybucket" {
    bucket = "${var.bucket_name}"
    acl = "public-read"

    website {
        redirect_all_requests_to = "index.html"
    }

    cors_rule {
        allowed_headers = ["*"]
        allowed_methods = ["PUT","POST"]
        allowed_origins = ["*"]
        expose_headers = ["ETag"]
        max_age_seconds = 3000
    }

    policy = <<EOF
{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "PublicReadForGetBucketObjects",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::${var.bucket_name}/*"
        }
    ]
}
EOF
}

resource "aws_cloudfront_distribution" "distribution" {
    origin {
        domain_name = "${aws_s3_bucket.mybucket.website_endpoint}"
        origin_id   = "S3-${aws_s3_bucket.mybucket.bucket}"

        custom_origin_config  {
            http_port = 80
            https_port = 443
            origin_protocol_policy = "match-viewer"
            origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
        }
    }
    default_root_object = "index.html"
    enabled             = true

    custom_error_response {
        error_caching_min_ttl = 3000
        error_code            = 404
        response_code         = 200
        response_page_path    = "/index.html"
    }

    default_cache_behavior {
        allowed_methods  = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
        cached_methods   = ["GET", "HEAD"]
        target_origin_id = "S3-${aws_s3_bucket.mybucket.bucket}"

        forwarded_values {
            query_string = true

            cookies {
                forward = "none"
            }
      }

        viewer_protocol_policy = "allow-all"
        min_ttl                = 0
        default_ttl            = 3600
        max_ttl                = 86400
    }

    # Restricts who is able to access this content
    restrictions {
        geo_restriction {
            # type of restriction, blacklist, whitelist or none
            restriction_type = "none"
        }
    }

    # SSL certificate for the service.
    viewer_certificate {
        cloudfront_default_certificate = true
    }
}

Please find below error message:

Error: Failed to load plugin schemas
│
│ Error while loading schemas for plugin components: Failed to obtain provider schema: Could not load the schema for provider registry.terraform.io/hashicorp/aws: failed to retrieve schema
│ from provider "registry.terraform.io/hashicorp/aws": Plugin did not respond: The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).GetProviderSchema call. The
│ plugin logs may contain more details...

Please help to resolve the issue , I'm new in terraforms. P.S. This error generated while terraform plan

2 Answers

I had faced the same issue. The error was a little different tho. I was following the HashiCups provider example.

It happens when initializing it fails to upgrade or detect corrupt cached providers, or if you have changed the version but when you only run terraform init it finds the version in cache and decides to go with it. Delete the terraform directory and lock file, and then init again terraform init -upgrade

If you're on running it on Apple M1 chip, you might as well need to set this:

export GODEBUG=asyncpreemptoff=1;

https://discuss.hashicorp.com/t/terraform-aws-provider-panic-plugin-did-not-respond/23396 https://github.com/hashicorp/terraform/issues/26104

I'm a beginner at terraform and I had same problem, so I hope this can help. I had same error "Failed to load plugin schemas" and I made a mess with .terraform.lock.hcl .terraform files. Here's what I did

  1. I created new project at the path where I installed terraform and aws, and it was at “desktop” .. I don't know if that was necessary or not.
  2. Then I created new two files provider.tf, and main.tf then I typed in terminal terraform init after that, it will automatically generate those files: .terraform .terraform.lock.hcl and I didn't change something on them. then run terraform plan in terminal.

here's my provider.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

provider "aws" {
  region = "us-east-1"
  shared_config_files      = ["~/.aws/config"]
  shared_credentials_files = ["~/.aws/credentials"]
  profile                  = "terraform-user"
}

and here's main.tf

resource "aws_vpc" "vpc" {
    cidr_block = "10.123.0.0/16"
    enable_dns_hostnames = true
    enable_dns_support = true
    tags = {
        Name = "vpc"
    }
}
 

here's some other problems you may face too: after you create provider.tf and main.tf then after running terraform init in terminal you get "no change", then you should save files before you run the commands in terminal.

Related