小编Imr*_*ozi的帖子

在 terraform.tfvars 中声明列表和映射变量值

我最近将 terraform 代码中的变量值移至 terraform.tfvars。我现在收到一个错误,这是由于我声明列表和映射变量的方式造成的。我收到错误的代码复制如下:

image_id             = var.web_amis[var.region]
Run Code Online (Sandbox Code Playgroud)

这就是我在 terraform.tfvars 中指定这些变量的方式:

web_amis                      = ["ami-0dacb0c129b49f529", "ami-00068cd7555f543d5", ]
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误代码:

Error: Invalid index

  on autoscaling.tf line 3, in resource "aws_launch_configuration" "web_lc":
   3:   image_id             = var.web_amis[var.region]
    |----------------
    | var.region is "us-east-2"
    | var.web_amis is tuple with 2 elements

The given key does not identify an element in this collection value: a number
is required.
Run Code Online (Sandbox Code Playgroud)

上面给出的错误消息的屏幕截图显示 Terraform 已突出显示 var.region 部分作为源代码片段中有问题的部分。

variables terraform

6
推荐指数
1
解决办法
2万
查看次数

Terraform aws_iam_role_policy 中的 JSON 语法错误

因此,通过 Terraform,我创建了一个 IAM 策略并将其附加到一个角色。我目前正在运行:

Terraform v0.12.16
provider.aws v2.40.0
provider.template v2.1.2
Run Code Online (Sandbox Code Playgroud)

执行代码时,我可以毫无问题地初始化 terraform。运行 terraform plan 时,出现以下错误:

Error: "policy" contains an invalid JSON: invalid character '}' looking for beginning of value

  on ec2-iam.tf line 8, in resource "aws_iam_role_policy" "s3_ec2_policy":
   8: resource "aws_iam_role_policy" "s3_ec2_policy" {
Run Code Online (Sandbox Code Playgroud)

我陷入了这个错误。任何意见将是有益的。下面是我的代码:

 data "template_file" "s3_web_policy" {
  template = file("scripts/iam/web-ec2-policy.json")
  vars = {
    s3_bucket_arn = "arn:aws:s3:::${var.my_app_s3_bucket}/*"
  }
}

resource "aws_iam_role_policy" "s3_ec2_policy" {
  name = "s3_ec2_policy"
  role = aws_iam_role.s3_ec2_role.id

  policy = data.template_file.s3_web_policy.rendered
}

resource "aws_iam_role" "s3_ec2_role" {
  name = "s3_ec2_role"

  assume_role_policy …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services terraform terraform-template-file terraform-provider-aws

3
推荐指数
1
解决办法
1万
查看次数