hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.75k stars 9.1k forks source link

[Enhancement]: aws_dx_gateway_association --> tgw-attach-id output #35009

Open eandresr opened 9 months ago

eandresr commented 9 months ago

Description

Hello,

As data source with aws_ec2_transit_gateway_dx_gateway_attachment fails when referencing to an resouce created in the same workspace (even with depends_on), it would be great to have the tgw-attach-id output in the resource aws_dx_gateway_association as the Transit Gateway Attachment is created automatically and you also have to wait till the association (it takes a lot of time, so 1 or 2 minutes more are not important). This way you can avoid several error and re-plan/apply in TF

I know it is not common because DxC is not as extended as for example VPN, but it is used in big Enterprises where errors in plans are not welcome. Thank you.

Affected Resource(s) and/or Data Source(s)

aws_dx_gateway_association

Potential Terraform Configuration

No response

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 9 months ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

eandresr commented 8 months ago

Hello,

I think it would need something like the following in the internal/service/directconnect/gateway_association.go --> func resourceGatewayAssociationRead

// ########### START OF NEW ATTACHMENT PART OBTAINER ##############
    if output.AssociatedGateway.Type == "transitGateway" {
        directConnectGatewayID := d.Get("dx_gateway_id").(string)
        attachmentConn := meta.(*conns.AWSClient).EC2Conn(ctx)
        var transitGatewayAttach []*ec2.TransitGatewayAttachment
        inputAttachment := &ec2.DescribeTransitGatewayAttachmentsInput{
            Filters: BuildAttributeFilterList(map[string]string{
                "resource-type": ec2.TransitGatewayAttachmentResourceTypeDirectConnectGateway,
                "resource-id":   directConnectGatewayID
            }),
        }
        err := attachmentConn.DescribeTransitGatewayAttachmentsPagesWithContext(ctx, inputAttachment, func(page *ec2.DescribeTransitGatewayAttachmentsOutput, lastPage bool) bool {
            if page == nil {
                return !lastPage
            }

            for _, v := range page.TransitGatewayAttachments {
                if v != nil {
                    transitGatewayAttach = append(output, v)
                }
            }

            return !lastPage
        })

        if tfawserr.ErrCodeEquals(err, errCodeInvalidTransitGatewayAttachmentIDNotFound) {
            return nil, &retry.NotFoundError{
                LastError:   err,
                LastRequest: input,
            }
        }

        if err != nil {
            return nil, err
        }
        d.Set("dx_gateway_attachment_id", transitGatewayAttach[0])
    } else {
        d.Set("dx_gateway_attachment_id", nil)
    }
// ########### END OF NEW ATTACHMENT PART OBTAINER ##############

And the following in the Schema

    "dx_gateway_attachment_id": {
                Type:     schema.TypeString,
                Computed: true,
            },