hel*_*per 9 amazon-web-services terraform terraform-provider-aws
我的简单 terraform 文件是:
provider "aws" {
region = "region"
access_key = "key"
secret_key = "secret_key"
}
terraform {
backend "s3" {
# Replace this with your bucket name!
bucket = "great-name-terraform-state-2"
key = "global/s3/terraform.tfstate"
region = "eu-central-1"
# Replace this with your DynamoDB table name!
dynamodb_table = "great-name-locks-2"
encrypt = true
}
}
resource "aws_s3_bucket" "terraform_state" {
bucket = "great-name-terraform-state-2"
# Enable versioning so we can see the full revision history of our
# state files
versioning {
enabled = true
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
resource "aws_dynamodb_table" "terraform_locks" {
name = "great-name-locks-2"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的就是将我的后端从本地替换为存储在 S3 中。我正在做以下事情:
terraform init (当 terrafrom{} 块是评论时)
terrafrom apply - 我可以在我的 AWS 中看到存储桶已创建,以及 Dynmpo 表。
现在我不再评论 terrafrom 块,terraform init并且我收到以下错误:
Error loading state:
AccessDenied: Access Denied
status code: 403, request id: xxx, host id: xxxx
Run Code Online (Sandbox Code Playgroud)
我的 IAM 具有
我正在使用的管理访问权限Terraform v0.12.24
,我直接在文件中写入我的 AWS 密钥和秘密
我究竟做错了什么?
我感谢任何帮助!
小智 10
我以前遇到过这个。以下是帮助您克服该错误的步骤-
backend "s3" {
bucket = "great-name-terraform-state-2"
key = "global/s3/terraform.tfstate"
region = "eu-central-1"
access_key = "<access-key>"
secret_key = "<secret-key>"
}
}
Run Code Online (Sandbox Code Playgroud)
错误应该消失了。
Bla*_*gle 10
terraform init我知道,通过在为其 Terraform 后端共享相同 S3 存储桶的其他项目上运行,我的凭据没有问题。
对我有用的:
rm -rf .terraform/
Run Code Online (Sandbox Code Playgroud)
编辑
terraform init请务必在删除本地目录后再次运行.terraform,以确保安装了所需的软件包。
小智 5
我也面临同样的问题。然后我手动从本地系统中删除状态文件。您可以在.terraform/目录下找到terraform.tfstate文件并再次运行init。如果您在 aws cli 中配置了多个配置文件。在 aws 提供程序配置下不提及配置文件将使 terraform 使用默认配置文件。
为了更好的安全性,您可以像这样使用shared_credentials_file和profile ;
provider "aws" {
region = "region"
shared_credentials_file = "$HOME/.aws/credentials # default
profile = "default" # you may change to desired profile
}
terraform {
backend "s3" {
profile = "default" # change to desired profile
# Replace this with your bucket name!
bucket = "great-name-terraform-state-2"
key = "global/s3/terraform.tfstate"
region = "eu-central-1"
# Replace this with your DynamoDB table name!
dynamodb_table = "great-name-locks-2"
encrypt = true
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15938 次 |
| 最近记录: |