Repository: chanzuckerberg/fogg
Pull request #299 (2019-06-18 17:19:32)

[feature] Add terraform remote state between accounts
### Summary
Similar to other components, accounts now get a reference to all other account's remote state

### Test Plan
unittests + ran it in a couple of repos

### References


codecov[bot]: # [Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/299?src=pr&el=h1) Report
> Merging [#299](https://codecov.io/gh/chanzuckerberg/fogg/pull/299?src=pr&el=desc) into [master](https://codecov.io/gh/chanzuckerberg/fogg/commit/d7a5ccc903fd704db3e77149175bbc763a928b4e?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/chanzuckerberg/fogg/pull/299/graphs/tree.svg?width=650&token=57DhtZ7g2A&height=150&src=pr)](https://codecov.io/gh/chanzuckerberg/fogg/pull/299?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #299   +/-   ##
=======================================
  Coverage   80.09%   80.09%           
=======================================
  Files          17       17           
  Lines        1422     1422           
=======================================
  Hits         1139     1139           
  Misses        162      162           
  Partials      121      121
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/299?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/299?src=pr&el=footer). Last update [d7a5ccc...83f8057](https://codecov.io/gh/chanzuckerberg/fogg/pull/299?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/templates/account/fogg.tf.tmpl b/templates/account/fogg.tf.tmpl
index 22b4178b..3076bd32 100644
--- a/templates/account/fogg.tf.tmpl
+++ b/templates/account/fogg.tf.tmpl
@@ -99,3 +99,20 @@ data "terraform_remote_state" "global" {
     {{ if .Global.Backend.Profile }}profile        = "{{ .Global.Backend.Profile }}"{{ end }}
   }
 }
+
+{{ $outer := . }}
+{{ range $accountName, $accountID := .AllAccounts}}
+{{ if ne $accountName $outer.AccountName }}
+data "terraform_remote_state" "{{ $accountName }}" {
+  backend = "s3"
+
+  config = {
+    bucket = "{{ $outer.Backend.Bucket }}"
+    {{ if $outer.Backend.DynamoTable }}dynamodb_table = "{{ $outer.Backend.DynamoTable }}"{{ end -}}
+    key            = "terraform/{{ $outer.Project }}/accounts/{{ $accountName }}.tfstate"
+    region         = "{{ $outer.Backend.Region }}"
+    {{ if $outer.Backend.Profile }}profile        = "{{ $outer.Backend.Profile }}"{{ end }}
+  }
+}
+{{ end }}
+{{ end }}


Test patch:
diff --git a/testdata/v1_full/terraform/accounts/bar/fogg.tf b/testdata/v1_full/terraform/accounts/bar/fogg.tf
index 6092f351..90f06138 100644
--- a/testdata/v1_full/terraform/accounts/bar/fogg.tf
+++ b/testdata/v1_full/terraform/accounts/bar/fogg.tf
@@ -78,3 +78,16 @@ data "terraform_remote_state" "global" {
     profile        = "czi"
   }
 }
+
+data "terraform_remote_state" "foo" {
+  backend = "s3"
+
+  config = {
+    bucket         = "bar-bucket"
+    dynamodb_table = "bar-table"
+
+    key     = "terraform/bar-project/accounts/foo.tfstate"
+    region  = "us-west-bar1"
+    profile = "czi-bar"
+  }
+}
diff --git a/testdata/v1_full/terraform/accounts/foo/fogg.tf b/testdata/v1_full/terraform/accounts/foo/fogg.tf
index 8a591403..d052cda1 100644
--- a/testdata/v1_full/terraform/accounts/foo/fogg.tf
+++ b/testdata/v1_full/terraform/accounts/foo/fogg.tf
@@ -78,3 +78,16 @@ data "terraform_remote_state" "global" {
     profile        = "czi"
   }
 }
+
+data "terraform_remote_state" "bar" {
+  backend = "s3"
+
+  config = {
+    bucket         = "foo-bucket"
+    dynamodb_table = "foo-table"
+
+    key     = "terraform/foo-project/accounts/bar.tfstate"
+    region  = "us-west-foo1"
+    profile = "czi-foo"
+  }
+}
diff --git a/testdata/v2_full/terraform/accounts/bar/fogg.tf b/testdata/v2_full/terraform/accounts/bar/fogg.tf
index 89e96989..e66017c6 100644
--- a/testdata/v2_full/terraform/accounts/bar/fogg.tf
+++ b/testdata/v2_full/terraform/accounts/bar/fogg.tf
@@ -70,3 +70,14 @@ data "terraform_remote_state" "global" {
     profile = "profile"
   }
 }
+
+data "terraform_remote_state" "foo" {
+  backend = "s3"
+
+  config = {
+    bucket  = "buck"
+    key     = "terraform/proj/accounts/foo.tfstate"
+    region  = "us-west-2"
+    profile = "profile"
+  }
+}
diff --git a/testdata/v2_full/terraform/accounts/foo/fogg.tf b/testdata/v2_full/terraform/accounts/foo/fogg.tf
index cfc6b865..cb357c6e 100644
--- a/testdata/v2_full/terraform/accounts/foo/fogg.tf
+++ b/testdata/v2_full/terraform/accounts/foo/fogg.tf
@@ -70,3 +70,14 @@ data "terraform_remote_state" "global" {
     profile = "profile"
   }
 }
+
+data "terraform_remote_state" "bar" {
+  backend = "s3"
+
+  config = {
+    bucket  = "buck"
+    key     = "terraform/proj/accounts/bar.tfstate"
+    region  = "us-west-2"
+    profile = "profile"
+  }
+}