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

ec2/instance: Fix data source regression #36054

Merged
merged 6 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/36054.txt
Original file line number Diff line number Diff line change
@@ -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`
```
18 changes: 11 additions & 7 deletions internal/service/ec2/ec2_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions internal/service/ec2/ec2_instance_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/ec2/ec2_spot_instance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Loading