hashicorp/packer · error

Failed to write amazon-ami data source: %v

Error message

Failed to write amazon-ami data source: %v

What it means

Error while converting a legacy JSON template to HCL2 (AmazonAmiDatasourceParser.Parse): a builder with a source_ami_filter could not be decoded into a data source block, so the amazon-ami filter configuration cannot be transposed.

Source

Thrown at command/hcl2_upgrade.go:1127

type AmazonAmiDatasourceParser struct {
	Builders        []*template.Builder
	WithAnnotations bool
	out             []byte
}

func (p *AmazonAmiDatasourceParser) Parse(_ *template.Template) error {
	if p.out == nil {
		p.out = []byte{}
	}

	amazonAmiFilters := []map[string]interface{}{}
	i := 1
	for _, builder := range p.Builders {
		if strings.HasPrefix(builder.Type, "amazon-") {
			if sourceAmiFilter, ok := builder.Config["source_ami_filter"]; ok {
				sourceAmiFilterCfg := map[string]interface{}{}
				if err := mapstructure.Decode(sourceAmiFilter, &sourceAmiFilterCfg); err != nil {
					return fmt.Errorf("Failed to write amazon-ami data source: %v", err)
				}

				sourceAmiFilterCfg, err := copyAWSAccessConfig(sourceAmiFilterCfg, builder.Config)
				if err != nil {
					return err
				}

				duplicate := false
				dataSourceName := fmt.Sprintf("autogenerated_%d", i)
				for j, filter := range amazonAmiFilters {
					if reflect.DeepEqual(filter, sourceAmiFilterCfg) {
						duplicate = true
						dataSourceName = fmt.Sprintf("autogenerated_%d", j+1)
						continue
					}
				}

				// This is a hack...

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Check that source_ami_filter in the JSON template is a plain map of strings/arrays without unexpected nesting
  2. Fix or hand-write the equivalent data "amazon-ami" block in the upgraded HCL2 file
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at command/hcl2_upgrade.go:1127 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/a8329468f31c6e12. Report an issue: GitHub.