我有一個映射變數來標識現有的 s3 存盤桶:
resource "aws_s3_bucket" "bucket" {
for_each = var.s3_replication
bucket = each.value.source
#other configuration
}
variable "s3_replication" {
description = "Map of buckets to replicate"
type = map
default = {
logs = {
source = "logs_bucket",
destination = "central_logs_bucket"
},
security = {
source = "cloudtrail_bucket",
destination = "central_security_bucket"
}
}
}
由于這些存盤桶已經存在,我嘗試匯入它們,然后將 a 配置應用于它們以更新資源。不幸的是,我無法弄清楚如何對這些進行 terraform 匯入。我試過了:
terraform import aws_s3_bucket.bucket["logs"] logs_bucket
terraform import aws_s3_bucket.bucket[logs] logs_bucket
terraform import aws_s3_bucket.bucket[0] logs_bucket
terraform import aws_s3_bucket.bucket[0].source logs_bucket
terraform import aws_s3_bucket.bucket[0[source]] logs_bucket
都因不同的錯誤而失敗。關于如何匯入地圖上列出的現有資源的任何想法?
uj5u.com熱心網友回復:
該terraform import子命令依賴于資源命名空間內映射鍵中的字串,這些字串是一級運算式,這會導致 shell 解釋器出現問題,其中資源不是一級運算式,因為它們不是 Terraform DSL。您可以通過將整個資源名稱轉換為文字字串來解決此問題:
terraform import 'aws_s3_bucket.bucket["logs"]' logs_bucket
這將解決您的問題。
uj5u.com熱心網友回復:
在做之前,import我建議做一個 terraform plan。計劃的輸出如下所示:
Terraform will perform the following actions:
# aws_s3_bucket.bucket["logs"] will be created
resource "aws_s3_bucket" "bucket" {
acceleration_status = (known after apply)
acl = "private"
arn = (known after apply)
bucket = "logs_bucket"
bucket_domain_name = (known after apply)
bucket_regional_domain_name = (known after apply)
force_destroy = false
hosted_zone_id = (known after apply)
id = (known after apply)
region = (known after apply)
request_payer = (known after apply)
tags_all = (known after apply)
website_domain = (known after apply)
website_endpoint = (known after apply)
versioning {
enabled = (known after apply)
mfa_delete = (known after apply)
}
}
# aws_s3_bucket.bucket["security"] will be created
resource "aws_s3_bucket" "bucket" {
acceleration_status = (known after apply)
acl = "private"
arn = (known after apply)
bucket = "cloudtrail_bucket"
bucket_domain_name = (known after apply)
bucket_regional_domain_name = (known after apply)
force_destroy = false
hosted_zone_id = (known after apply)
id = (known after apply)
region = (known after apply)
request_payer = (known after apply)
tags_all = (known after apply)
website_domain = (known after apply)
website_endpoint = (known after apply)
versioning {
enabled = (known after apply)
mfa_delete = (known after apply)
}
}
Plan: 2 to add, 0 to change, 0 to destroy.
有了這個計劃,我們可以看到將要創建的資源可以參考為aws_s3_bucket.bucket["logs"]和aws_s3_bucket.bucket["security"]。apply我們可以按如下方式匯入它們,而不是執行 a :
重擊:
terraform import 'aws_s3_bucket.bucket["security"]' cloudtrail-bucket
terraform import 'aws_s3_bucket.bucket["logs"]' logs-bucket
Windows CMD:
terraform import 'aws_s3_bucket.bucket[\"security\"]' cloudtrail-bucket
terraform import 'aws_s3_bucket.bucket[\"logs\"]' logs-bucket
uj5u.com熱心網友回復:
發布的答案中的語法是正確的。我的代碼只是出了問題,因為它找不到 varsfile。我需要添加 -var-file={{ path to tfvars }}。所以最終的語法看起來像:
terraform import -var-file={{ path to tfvars}} 'aws_s3_bucket.bucket["logs"]' logs_bucket
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/329837.html
標籤:亚马逊网络服务 字典 亚马逊-s3 地形 terraform-provider-aws
上一篇:"MethodNotAllowedThemethodisnotallowedforrequestedURL"即使啟用了methods=["POST"]?
