packer

Announcing the Packer Data Source Component Type

The HashiCorp Packer team is excited to announce the release of Data Source, a new component type to fetch or compute data for use elsewhere in a Packer configuration.

The HashiCorp Packer team is excited to announce the release of Data Source, a new component type to fetch or compute data for use elsewhere in a Packer configuration.

Starting in Packer 1.7.0, users and plugin developers will use data source plugins within HCL2-enabled build templates. As we continue to focus on favoring HCL2 over legacy JSON templates, data source components will be the first Packer feature exclusively available to HCL2.

Data sources in Packer function similarly to Terraform’s data sources. Data source components fetch data from outside Packer and make detailed information about that data available to Packer HCL configuration blocks. A data source runs before any build. This allows build sources in your configuration to access the result of the data source.

»Amazon Web Services Data Sources

Packer 1.7.0 includes two data sources: the Amazon AMI data source and Amazon Secrets Manager data source.

The Amazon AMI data source filters images from the marketplace, similar to the source_ami_filter configuration. The Amazon Secrets Manager data source retrieves secrets for the build configuration, similar to the aws_secretsmanager configuration. 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.

You can upgrade from legacy JSON to HCL2 with the hcl2_upgrade command. This will upgrade the source_ami_filter and aws_secretsmanager options to their respective data sources.

»Using Data Sources

You can reference a data source in locals and sources as data.<TYPE>.<NAME>.<ATTRIBUTE>. For example, you can use the Amazon Secrets Manager data source as a local variable to store the value and version of a secret.

data "amazon-secretsmanager" "basic-example" {
  name = "my_super_secret"
  key  = "my_secret_key"
}
 
# usage example of the data source output
locals {
  secret_value         = data.amazon-secretsmanager.basic-example.value
  secret_version_id = data.amazon-secretsmanager.basic-example.version_id
}
data "amazon-secretsmanager" "basic-example" {  name = "my_super_secret"  key  = "my_secret_key"} # usage example of the data source outputlocals {  secret_value         = data.amazon-secretsmanager.basic-example.value  secret_version_id = data.amazon-secretsmanager.basic-example.version_id}

You can then use the local.secret_value and local.secret_version_id anywhere in your configuration. For more information, review our documentation on using data sources.

»Developing a Data Source

You can write your own data source. Follow the instructions for Custom Data Sources.

»Upcoming Data Sources

In an upcoming Packer release, we plan to roll out two more data sources as a replacement for two of our existent functions:

If you have any questions or feedback on Packer data sources, you can do so in our Discuss forum, or submit an issue.


Sign up for the latest HashiCorp news