hashicorp / hashicorp/packer

Dynamic data blocks

Open
#12,001 2 comments 7 reactions 0 assignees View on GitHub
enhancement hcl2 sync to jira
Dominant language
Go
Stars
15.8k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

#### Community Note

* Please vote on this issue by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original issue to help the community and maintainers prioritize this request
* Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
* If you are interested in working on this issue or have submitted a pull request, please leave a comment

#### Description

It would be nice to be able to define `dynamic` top-level `data` blocks.

#### Use Case(s)

Let's say I need to build similar images across many AWS regions. Specifically, this involves looking up base AMI per-region. At the moment this can be achieved by generating source blocks dynamically within a build block, something like:

```
variable "regions" {
default = ["us-east-2", "eu-central-1"]
}

source "amazon-ebs" "common-ami-props" {
}

build {
dynamic "source" {
for_each = var.regions
labels = ["amazon-ebs.common-ami-props"]
content {
source_ami_filter {
...
}
region = source.value
}
}
}
```

This works because we use `source_ami_filter` within our source blocks. Introduction of `data` blocks, however seems to discourage that. Quoting the [announcement block post](https://www.hashicorp.com/blog/announcing-the-packer-data-source-component-type):
> Both configuration parameters will remain available for use in Packer build configuration. However, we encourage you to update your configuration to the new data source configuration for future stability.

The problem is that it's not possible to build `data` blocks dynamically with `for_each`. Neither is it possible to inherit `region` in some other way. You'd have to duplicate definition on `data` block for each region, [much like is done in official examples](https://learn.hashicorp.com/tutorials/packer/golden-image-with-hcp-packer#review-golden-image-configuration).

P.S. This is fairly similar to #11301, except that it is for `data` blocks and not `build` blocks.

Contributor guide

Open the contributing guide

Research direction

Start from the dynamic source-block example and the stated limitation that top-level data blocks cannot use for_each. Compare the requested behavior with the related build-block issue #11301 and the linked data-source announcement. Done means data blocks can be defined dynamically for multiple regions without duplicating each regional definition.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, go
Domain
build-system, cloud, devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.