LoginSignup
3
0

More than 3 years have passed since last update.

`% terraform init` で `* hashicorp/aws: version = "~> 3.10.0"` とエラーが表示される場合

Last updated at Posted at 2020-10-10

IaCに憧れてTerraformを勉強中の新米エンジニアよしこです。

AWS EC2にVPCを構築するのにTerraformを使用しているときに以下のログに遭遇しました


> terraform init

Initializing the backend...

Initializing provider plugins...
- Using previously-installed hashicorp/aws v3.10.0

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* hashicorp/aws: version = "~> 3.10.0"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

日本語のケースワークが少なかったので解消方法を共有します

環境

> terraform -v
Terraform v0.13.4
+ provider registry.terraform.io/hashicorp/aws v3.10.0

解決策

ログにもしっかり書かれていますが、hashicorp/aws: version = "~> 3.10.0" のようにバージョンの記述を main.tf へ追加することでエラーが解消します。

Terraformのドキュメントを参考に以下のように記述しました。
https://registry.terraform.io/providers/hashicorp/aws/latest/docs

main.tf
variable "access_key" {}
variable "secret_key" {}
variable "region" {}

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 3.10"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  access_key = var.access_key
  secret_key = var.sevret_key
  region = var.region
}

# Create a VPC
resource "aws_vpc" "vpc-1" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "vpc-1"
  }
}

こちらはterraformのバージョン0.13 以降の記述になります。

バージョン0.12 以前の場合は以下のように記述します。

main.tf
# Configure the AWS Provider
provider "aws" {
  version = "~> 3.0"
  region  = "us-east-1"
}

# Create a VPC
resource "aws_vpc" "example" {
  cidr_block = "10.0.0.0/16"
}

変更前

参考程度に、変更前はこのように記述していました

main.tf
variable "access_key" {}
variable "secret_key" {}
variable "region" {}

provider "aws" {
  access_key = var.access_key
  secret_key = var.secret_key
  region     = var.region
}

resource "aws_vpc" "vpc-1" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "vpc-1"
  }
}
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0