0
0

More than 3 years have passed since last update.

[Terraform] mapのリストへのアクセスでエラーが出たら、これかも。

Last updated at Posted at 2020-09-01

Terraformのaws_route53_recordで作成したリソースの変数を取得しようとしたら、エラーが出たので解決法を載せます。
使用しているTerraform/probider awsのバージョンはTerraform v0.12.5 + provider.aws v3.4.0です。

エラー内容

Error: Invalid index

  on main.tf line 124, in resource "aws_route53_record" "example_certificate":
 124:   name      = aws_acm_certificate.example.domain_validation_options[0].resource_rcord_name

This value does not have any indices.


Error: Invalid index

  on main.tf line 125, in resource "aws_route53_record" "example_certificate":
 125:   type      = aws_acm_certificate.example.domain_validation_options[0].resource_record_type

This value does not have any indices.


Error: Invalid index

  on main.tf line 126, in resource "aws_route53_record" "example_certificate":
 126:   records   = [aws_acm_certificate.example.domain_validation_options[0].resource_record_value]

This value does not have any indices.

使用したコード

main.tf

resource "aws_acm_certificate" "example" {
  domain_name               = "xxxxxxx.com"
  subject_alternative_names = ["*.xxxxxxx.com"]
  validation_method         = "DNS"

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_route53_record" "example_certificate" {
  name      = aws_acm_certificate.example.domain_validation_options[0].resource_rcord_name
  type      = aws_acm_certificate.example.domain_validation_options[0].resource_record_type
  records   = [aws_acm_certificate.example.domain_validation_options[0].resource_record_value]
  zone_id   = data.aws_route53_zone.example.zone_id
  ttl       = 60
}

解決方法

mapのリストにうまくアクセス出来ていないかったようです。
表記方法変えたらエラーなくなりました。

main.tf
name      = aws_acm_certificate.example.domain_validation_options[0].resource_rcord_name

name      = aws_acm_certificate.example.domain_validation_options.*.resource_record_name[0]

ちなみにterraform consoleを使えば対話的にリソースの確認も出来るのでデバッグに便利でした。

$ terraform console
> aws_acm_certificate.example.domain_validation_options
[
  {
    "domain_name" = "*.xxxxxxx.com"
    "resource_record_name" = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxx.com."
    "resource_record_type" = "CNAME"
    "resource_record_value" = "xxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxx.acm-validations.aws."
  },
  {
    "domain_name" = "xxxxxxx.com"
    "resource_record_name" = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxx.com."
    "resource_record_type" = "CNAME"
    "resource_record_value" = "xxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxx.acm-validations.aws."
  },
]
>  
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0