我试图在AWS环境中创建一个简单的示例。一开始,我导出两个值:
export AWS_ACCESS_KEY_ID= something
export AWS_SECRET_ACCESS_KEY= something
之后,我写了一个简单的代码。
provider "aws" {
region = "us-east-1"
access_key = AWS_ACCESS_KEY_ID
secret_key = AWS_SECRET_ACCESS_KEY
}
resource "aws_instance" "example" {
ami = "ami-40d28157"
instance_type = "t2.micro"
tags = {
Name = "terraform-example"
}
}
当我定义值而不是参数AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY时,一切正常,但使用提供的代码,我看到以下错误
on main.tf line 4, in provider "aws":
4: secret_key = AWS_SECRET_ACCESS_KEY
A reference to a resource type must be followed by at least one attribute
access, specifying the resource name.
关于如何解决这个问题的一些想法?
您不需要做任何事情。如AWS提供商的terraform
认证文档中所述,terraform
将按以下顺序自动使用凭据:
- 静态凭据
- 环境变量
- 共享凭据/配置文件
- CodeBuild、ECS和EKS角色
- EC2实例元数据服务(IMDS和IMDSv2(
因此,一旦您导出密钥(确保正确导出(:
export AWS_ACCESS_KEY_ID="something"
export AWS_SECRET_ACCESS_KEY="something"
在您的配置文件中,您只需使用(文档中举例说明(:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-40d28157"
instance_type = "t2.micro"
tags = {
Name = "terraform-example"
}
}