diff --git a/.changelog/36054.txt b/.changelog/36054.txt new file mode 100644 index 00000000000..ae151ddcdf1 --- /dev/null +++ b/.changelog/36054.txt @@ -0,0 +1,3 @@ +```release-note:bug +data-source/aws_instance: Fix `panic: Invalid address to set` related to `root_block_device.0.tags_all` +``` \ No newline at end of file diff --git a/internal/service/ec2/ec2_instance.go b/internal/service/ec2/ec2_instance.go index cd9db6f5f8a..e2e5a1b4992 100644 --- a/internal/service/ec2/ec2_instance.go +++ b/internal/service/ec2/ec2_instance.go @@ -1319,7 +1319,7 @@ func resourceInstanceRead(ctx context.Context, d *schema.ResourceData, meta inte return sdkdiag.AppendErrorf(diags, "reading EC2 Instance (%s): %s", d.Id(), err) } - if err := readBlockDevices(ctx, d, meta, instance); err != nil { + if err := readBlockDevices(ctx, d, meta, instance, false); err != nil { return sdkdiag.AppendErrorf(diags, "reading EC2 Instance (%s): %s", d.Id(), err) } @@ -2161,8 +2161,8 @@ func modifyInstanceAttributeWithStopStart(ctx context.Context, conn *ec2.EC2, in return nil } -func readBlockDevices(ctx context.Context, d *schema.ResourceData, meta interface{}, instance *ec2.Instance) error { - ibds, err := readBlockDevicesFromInstance(ctx, d, meta, instance) +func readBlockDevices(ctx context.Context, d *schema.ResourceData, meta interface{}, instance *ec2.Instance, ds bool) error { + ibds, err := readBlockDevicesFromInstance(ctx, d, meta, instance, ds) if err != nil { return fmt.Errorf("reading block devices: %w", err) } @@ -2213,7 +2213,7 @@ func readBlockDevices(ctx context.Context, d *schema.ResourceData, meta interfac return nil } -func readBlockDevicesFromInstance(ctx context.Context, d *schema.ResourceData, meta interface{}, instance *ec2.Instance) (map[string]interface{}, error) { +func readBlockDevicesFromInstance(ctx context.Context, d *schema.ResourceData, meta interface{}, instance *ec2.Instance, ds bool) (map[string]interface{}, error) { blockDevices := make(map[string]interface{}) blockDevices["ebs"] = make([]map[string]interface{}, 0) blockDevices["root"] = nil @@ -2279,9 +2279,13 @@ func readBlockDevicesFromInstance(ctx context.Context, d *schema.ResourceData, m bd["device_name"] = aws.StringValue(instanceBd.DeviceName) } if v, ok := d.GetOk("volume_tags"); !ok || v == nil || len(v.(map[string]interface{})) == 0 { - tags := KeyValueTags(ctx, vol.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig) - bd[names.AttrTags] = tags.RemoveDefaultConfig(defaultTagsConfig).Map() - bd[names.AttrTagsAll] = tags.Map() + if ds { + bd[names.AttrTags] = KeyValueTags(ctx, vol.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map() + } else { + tags := KeyValueTags(ctx, vol.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig) + bd[names.AttrTags] = tags.RemoveDefaultConfig(defaultTagsConfig).Map() + bd[names.AttrTagsAll] = tags.Map() + } } if blockDeviceIsRoot(instanceBd, instance) { diff --git a/internal/service/ec2/ec2_instance_data_source.go b/internal/service/ec2/ec2_instance_data_source.go index ecc11abd043..09733231d14 100644 --- a/internal/service/ec2/ec2_instance_data_source.go +++ b/internal/service/ec2/ec2_instance_data_source.go @@ -21,6 +21,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKDataSource("aws_instance") @@ -98,7 +99,7 @@ func DataSourceInstance() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "tags": tftags.TagsSchemaComputed(), + names.AttrTags: tftags.TagsSchemaComputed(), "throughput": { Type: schema.TypeInt, Computed: true, @@ -332,7 +333,7 @@ func DataSourceInstance() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "tags": tftags.TagsSchemaComputed(), + names.AttrTags: tftags.TagsSchemaComputed(), "throughput": { Type: schema.TypeInt, Computed: true, @@ -549,7 +550,7 @@ func instanceDescriptionAttributes(ctx context.Context, d *schema.ResourceData, } // Block devices - if err := readBlockDevices(ctx, d, meta, instance); err != nil { + if err := readBlockDevices(ctx, d, meta, instance, true); err != nil { return fmt.Errorf("reading EC2 Instance (%s): %w", aws.StringValue(instance.InstanceId), err) } if _, ok := d.GetOk("ephemeral_block_device"); !ok { diff --git a/internal/service/ec2/ec2_spot_instance_request.go b/internal/service/ec2/ec2_spot_instance_request.go index 0cb13a8c114..f8226a6e1ce 100644 --- a/internal/service/ec2/ec2_spot_instance_request.go +++ b/internal/service/ec2/ec2_spot_instance_request.go @@ -317,7 +317,7 @@ func readInstance(ctx context.Context, d *schema.ResourceData, meta interface{}) "host": *instance.PrivateIpAddress, }) } - if err := readBlockDevices(ctx, d, meta, instance); err != nil { + if err := readBlockDevices(ctx, d, meta, instance, false); err != nil { return sdkdiag.AppendFromErr(diags, err) }