Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple include blocks #1804

Merged
merged 26 commits into from
Sep 14, 2021
Merged

Multiple include blocks #1804

merged 26 commits into from
Sep 14, 2021

Conversation

yorinasub17
Copy link
Contributor

This is the implementation of the feature described in #1803

This is backward compatible for the most part, except for the expose feature, where you must now index into the label - any prior references to exposed include must be updated to include[""]. E.g., if the user had:

include {
  path = find_in_parent_folders()
  expose = true
}

inputs = {
  region = include.locals.region
}

the include.locals.region reference must be updated to include[""].locals.region.

include {
  path = find_in_parent_folders()
  expose = true
}

inputs = {
  region = include[""].locals.region
}

Copy link
Member

@brikis98 brikis98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work Yori 👍

config/config_helpers.go Show resolved Hide resolved
config/include.go Show resolved Hide resolved
config/include.go Show resolved Hide resolved
config/include.go Outdated Show resolved Hide resolved
}
```

### Using read\_terragrunt\_config to DRY parent configurations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned this in the other PR, but we probably want to encourage putting things that you typically change on an env-by-env basis in the env itself so it works better with CI / CD; and putting things that you would change globally (albeit rarely) into an _env folder. So for example, env name, instance type, number of instances, ref (version) are all things you may want to change in one env, but not another, and you may want CI / CD to be able to detect it based on the file you changed, so it makes more sense to define those params directly in stage/app/terragrunt.hcl for stage and prod/app/terragrunt.hcl for prod. For everything else, you can put it in _env, under the assumption that it changes very rarely, and you can somehow manually trigger CI / CD as necessary for that change.

That does make me think of an alternative approach, which might be nonsense, but I figured it's worth thinking through: would it make sense to version the terragrunt.hcl files in _env? Ignoring implementation details, imagine for a second that all the _env stuff is defined in infrastructure-modules; inside the terragrunt.hcl files in _env, it uses relative paths within infrastructure-modules; and the whole repo is versioned as usual. Then, in infrastructure-live, you include not from a path but from a versioned source parameter. That allows you to use different versions of _env in each actual env, and promote changes from one env to another. In that case, you could define environment-specific values in _env as well (e.g., instance type, instance count, etc), as you'd bump the version number in stage or prod to pick up the new configs, and CI / CD would pick that up.

So, in effect, infra-live would become VERY minimal: just a bunch of terragrunt.hcl marker files that use include to pull in the actual configs from versioned URLs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does make me think of an alternative approach, which might be nonsense, but I figured it's worth thinking through: would it make sense to version the terragrunt.hcl files in _env? Ignoring implementation details, imagine for a second that all the _env stuff is defined in infrastructure-modules; inside the terragrunt.hcl files in _env, it uses relative paths within infrastructure-modules; and the whole repo is versioned as usual. Then, in infrastructure-live, you include not from a path but from a versioned source parameter. That allows you to use different versions of _env in each actual env, and promote changes from one env to another. In that case, you could define environment-specific values in _env as well (e.g., instance type, instance count, etc), as you'd bump the version number in stage or prod to pick up the new configs, and CI / CD would pick that up.

Yes I had this exact thought! This actually has another advantage of better handling variable updates across module versions. E.g., in the current setup, there is a limitation where the shared inputs are in a static file shared across dev, stage, and prod. What if a version bump removes a variable, or there is a new required variable, or an old variable type changed? Ideally, you would make the change in the shared variables in _env, but then that would break stage and prod.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned this in the other PR, but we probably want to encourage putting things that you typically change on an env-by-env basis in the env itself so it works better with CI / CD; and putting things that you would change globally (albeit rarely) into an _env folder. So for example, env name, instance type, number of instances, ref (version) are all things you may want to change in one env, but not another, and you may want CI / CD to be able to detect it based on the file you changed, so it makes more sense to define those params directly in stage/app/terragrunt.hcl for stage and prod/app/terragrunt.hcl for prod. For everything else, you can put it in _env, under the assumption that it changes very rarely, and you can somehow manually trigger CI / CD as necessary for that change.

Good point. I updated the docs to use expose instead of read_terragrunt_config for the module version, and updated read_terragrunt_config docs to be something more generic: c034ca9

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does make me think of an alternative approach, which might be nonsense, but I figured it's worth thinking through: would it make sense to version the terragrunt.hcl files in _env? Ignoring implementation details, imagine for a second that all the _env stuff is defined in infrastructure-modules; inside the terragrunt.hcl files in _env, it uses relative paths within infrastructure-modules; and the whole repo is versioned as usual. Then, in infrastructure-live, you include not from a path but from a versioned source parameter. That allows you to use different versions of _env in each actual env, and promote changes from one env to another. In that case, you could define environment-specific values in _env as well (e.g., instance type, instance count, etc), as you'd bump the version number in stage or prod to pick up the new configs, and CI / CD would pick that up.

Yes I had this exact thought! This actually has another advantage of better handling variable updates across module versions. E.g., in the current setup, there is a limitation where the shared inputs are in a static file shared across dev, stage, and prod. What if a version bump removes a variable, or there is a new required variable, or an old variable type changed? Ideally, you would make the change in the shared variables in _env, but then that would break stage and prod.

We should def experiment with this as the way to make the Ref Arch super DRY, versioned, etc.

@yorinasub17 yorinasub17 force-pushed the yori-multiple-include-blocks branch from 9ecd023 to fb5d604 Compare September 13, 2021 20:01
Copy link
Member

@brikis98 brikis98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great work Yori 👍

config/include.go Show resolved Hide resolved

include "env" {
path = "${get_terragrunt_dir()}/../../_env/app.hcl"
expose = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat 👍

```


### Using read\_terragrunt\_config to DRY parent configurations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice, this is a better example 👍

}

terraform {
source = "../modules/${path_relative_to_include("root")}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing in the name is a great solution 👍

@yorinasub17
Copy link
Contributor Author

Thanks for review! Will merge this in, cut a new release as backward compatible (given how much code change there is to the include machinery), and then open a follow up PR to update that function docs.

@yorinasub17 yorinasub17 merged commit f02beeb into master Sep 14, 2021
@yorinasub17 yorinasub17 deleted the yori-multiple-include-blocks branch September 14, 2021 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants