如果在 `% terraform init` 中出现错误 `* hashicorp/aws: version = “~> 3.10.0″`,请进行以下处理
我是一位新手工程师吉子,正在学习渴望实现IaC的Terraform。
当我使用Terraform构建AWS EC2实例时,遇到了以下日志。
> 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.
由于日本语案例较少,我将分享解决方法。
环境
(Note: The provided text is already in Chinese, so there is no need for further paraphrasing.)
> terraform -v
Terraform v0.13.4
+ provider registry.terraform.io/hashicorp/aws v3.10.0
解决方案
只需要一种选择:在main.tf中添加版本说明hashicorp/aws: version = “~> 3.10.0″可以解决错误,正如日志中明确写明的那样。
我根据Terraform文档的参考,写下以下描述:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs
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或之前,需要如下所述。
# 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"
}
改变前
在参考程度上,之前的描述如下。
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"
}
}