10
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Terraformで循環参照のエラーが出た時

Posted at

これは、リソースを変数で参照して整合性?を保とうとしたときに良くなる。

variable "env" {
  type = "string"
  default = "dev"
}
variable "service" {
  type = "string"
  default = "foo"
}

resource "aws_cloudfront_origin_access_identity" "origin_access" {
  comment = "access-identity-${var.env}-${var.service}.s3.amazonaws.com"
}

data "template_file" "s3_policy" {
  template = "${file("s3-bucket-policy.json")}"

  vars {
    bucket_name            = "${var.env}-${var.service}"
    origin_access_identity = "${aws_cloudfront_origin_access_identity.origin_access.id}"
  }
}

resource "aws_s3_bucket" "foo" {
  bucket = "${var.env}-${var.service}"
  acl    = "private"
  policy = "${data.template_file.s3_policy.rendered}"

  tags {
    Name        = "${var.env}-${var.service}"
    Environment = "${var.env}"
    ServiceName = "${var.service}"
  }
}

となっている場合、循環参照が発生する。

❯ terraform plan
Error configuring: 1 error(s) occurred:

* Cycle: data.template_file.s3_policy, aws_s3_bucket.foo, aws_cloudfront_origin_access_identity.origin_access

もう、わけわかんないし辛い気持ちになるけど、Terraformには便利な機能がある。

依存グラフの出力である。

terraform graph -draw-cycles | dot -Tpng > graph.png

graph.png

-draw-cyclesをつけると赤くなるので、分かりやすい。
後は、その依存関係を解決していけば良いだけ。

今回のケースは、aws_cloudfront_origin_access_identityでバケット名を参照しているが、バケット名は"${var.env}-${var.service}"で置き換え可能なため、

resource "aws_cloudfront_origin_access_identity" "origin_access" {
  comment = "access-identity-${var.env}-${var.service}.s3.amazonaws.com"
}

とすると直る。

graph2.png

References

10
3
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
10
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?