Terraform AWS提供程序无法进行身份验证

41次阅读
没有评论

问题描述

在尝试使用Terraform创建一个新的服务器实例时,使用AWS提供程序时遇到了以下错误:

Warning: Resource targeting is in effect
You are creating a plan with the -target option, which means that the result of this plan may not represent all of the changes requested by the current configuration.
The -target option is not for routine use, and is provided only for exceptional situations such as recovering from errors or mistakes, or when Terraform specifically suggests to use it as part of an error message.

Error: error configuring Terraform AWS Provider: failed to get shared config profile, 2auth
  with provider["registry.terraform.io/hashicorp/aws"],
  on main.tf line 1, in provider "aws":
   1: provider "aws" {

用户表示对于此处引用的”2auth”配置文件完全不清楚,并且已经尝试在tf文件中设置凭据以及作为环境变量,但仍然无法解决问题。他希望能够得到一些关于问题的指导。

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

Terraform AWS提供程序在配置身份验证时,默认会使用[default]配置文件中的凭据。如果你的~/.aws/credentials文件中包含多个配置文件,你需要明确指定要使用的配置文件。以下是解决此问题的步骤:
1. 打开你的~/.aws/credentials文件,确保其中包含你的凭据信息,例如:

[default]
aws_access_key_id = xxxxxxxx
aws_secret_access_key = xxxxxxxx
[profile2]
aws_access_key_id = yyyyyyyy
aws_secret_access_key = yyyyyyyyy
  1. 在你的Terraform代码中,明确指定要使用的配置文件,使用profile属性,如下所示:
provider "aws" {
   region = var.aws_region
   shared_credentials_file = "/Users/samueldare/.aws/credentials"
   profile = "profile2"
}

通过以上设置,你明确告诉Terraform要使用profile2中的凭据信息。请确保将文件路径和配置文件名替换为你实际的路径和配置文件名。

请参考Terraform官方文档获取更多关于shared configuration和credentials files的信息。

方案2

如果你在Terraform中使用了模块,并且在模块内部声明了提供程序,而你可能没有注意到这一点,也可能导致身份验证问题。请检查你的Terraform配置中是否有其他地方声明了AWS提供程序。确保在使用提供程序的所有地方,都使用了正确的凭据信息。

方案3(用户评论提到的情况)

如果你的Terraform配置使用了-target选项,可能会导致部分资源的创建计划,你需要确保在使用这个选项时有充分的理由,并且明确了使用它的目的。在正常情况下,不建议常规使用-target选项,这个选项主要用于特殊情况,比如从错误中恢复。

以上是针对问题的几种可能解决方案,请根据你的具体情况选择适合的方案来解决身份验证问题。

正文完