Terraform実行時にエラー「Unrecognized remote plugin message」に遭遇したので原因を探ってみた

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

どうもコンサルティング部の後藤です。

今回、Terraformで全リージョンのGuardDutyを有効化する設定を行おうとしていたところ、見慣れないエラー「Unrecognized remote plugin message」に遭遇したので、その原因を探ってみました。

やろうとした事

Terraform実行環境は以下の通りです。

・実行環境
EC2(t3.micro)

・Terraformバージョン
$ terraform --version
Terraform v0.12.29
+ provider.aws v3.5.0

上記実行環境にて、全リージョンのGuardDutyを有効化するTerraformを実行します。
今回は全リージョンに対してTerraformを実行するため、以下のようにProviderの設定で各リージョンを指定し、それをmoduleで読み込んでいます。

$ cat providers.tf
provider "aws" {
  region = "ap-northeast-1"
  alias  = "ap-northeast-1"
}

provider "aws" {
  region = "ap-northeast-2"
  alias  = "ap-northeast-2"
}

provider "aws" {
  region = "ap-east-1"
  alias  = "ap-east-1"
}
~~全リージョン分記載~~
東京リージョンを指定する場合
$ cat ap-northeast-1.tf
module "ap-northeast-1" {
  source = "./modules/aws/"

  providers = {
    aws = aws.ap-northeast-1
  }
}

エラー「Unrecognized remote plugin message」について

上記のTerraformを実行しようとすると、以下のようなエラーが発生しました。

$ terraform plan

Error: Unrecognized remote plugin message: 

This usually means that the plugin is either invalid or simply
needs to be recompiled to support the latest protocol.

初めて見るエラーでなんだこれわからん状態だったので、取り合えず検索検索ぅ!してみると、以下参考になりそうなページがヒットしました。
Upgrading From Versions Earlier Than 0.7
"Unrecognized remote plugin message" following 012upgrade instructions

内容としてはTerraformのバージョンを更新した際に古いプラグインが残っているためエラーとなっているとのことですが、今回はバージョンの更新等は行っておらず、プラグインもterraform initで最新のものを取得していました。

デバッグログから原因を探ってみる

Terraformでは環境変数でTF_LOG=DEBUGを設定することでデバッグログを出力することが出来ます。

デバッグログ出力を有効にしつつ、先程エラーが出たterraform planを実行してみます。

$ TF_LOG=DEBUG terraform plan
2020/09/11 02:11:30 [WARN] Log levels other than TRACE are currently unreliable, and are supported only for backward compatibility.
  Use TF_LOG=TRACE to see Terraform's internal logs.
  ----
2020/09/11 02:11:30 [INFO] Terraform version: 0.12.29  
2020/09/11 02:11:30 [INFO] Go runtime version: go1.12.13
2020/09/11 02:11:30 [INFO] CLI args: []string{"/home/terraform/.local/bin/terraform", "plan"}

~~~長いので省略~~~

2020/09/11 02:11:33 [ERROR] <root>: eval: *terraform.EvalSequence, err: Unrecognized remote plugin message: 

This usually means that the plugin is either invalid or simply
needs to be recompiled to support the latest protocol.
2020-09-11T02:11:33.707Z [DEBUG] plugin: plugin process exited: path=/home/terraform/PATH/.terraform/plugins/linux_amd64/terraform-provider-aws_v3.5.0_x5 pid=22251 error="exit status 2"
2020-09-11T02:11:33.708Z [INFO]  plugin: configuring client automatic mTLS
2020-09-11T02:11:33.737Z [DEBUG] plugin: starting plugin: path=/home/terraform/PATH/.terraform/plugins/linux_amd64/terraform-provider-aws_v3.5.0_x5 args=[/home/terraform/PATH/.terraform/plugins/linux_amd64/terraform-provider-aws_v3.5.0_x5]
2020-09-11T02:11:33.737Z [DEBUG] plugin: plugin started: path=/home/terraform/PATH/.terraform/plugins/linux_amd64/terraform-provider-aws_v3.5.0_x5 pid=22252
2020-09-11T02:11:33.737Z [DEBUG] plugin: waiting for RPC address: path=/home/terraform/PATH/.terraform/plugins/linux_amd64/terraform-provider-aws_v3.5.0_x5
2020-09-11T02:11:33.738Z [DEBUG] plugin.terraform-provider-aws_v3.5.0_x5: fatal error: runtime: out of memory

~~~

デバッグログ内に、先程と同様のエラーを見つけ、その後の出力を見てみるとplugin.terraform-provider-aws_v3.5.0_x5: fatal error: runtime: out of memoryの文言を見つけました。

もしやメモリ不足か?ということで実行環境のEC2インスタンスタイプをt3.microからt3.mediumに変更して、改めてTerraformを実行したところ

$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.ap-east-1.aws_guardduty_detector.guardduty will be created
  + resource "aws_guardduty_detector" "guardduty" {
      + account_id                   = (known after apply)
      + arn                          = (known after apply)
      + enable                       = true
      + finding_publishing_frequency = "FIFTEEN_MINUTES"
      + id                           = (known after apply)
    }

~~~

plan,applyすることが出来ました。

まとめ

Terraformエラー「Unrecognized remote plugin message」の対応として実行環境EC2のインスタンスタイプを上げたところ、エラーを解決することが出来ました。今回はメモリが原因だったのかもしれませんが、このエラーは記事の中にもあるように他の原因でも発生する可能性があるエラーのため、全てこの対応で解決できるという訳では無さそうです。

この記事が何方かのお役に立てば幸いです。