aws-samples / aws2tf

aws2tf - automates the importing of existing AWS resources into Terraform and outputs the Terraform HCL code.
MIT No Attribution
581 stars 101 forks source link

Failed to upload complete tgw resource #8

Closed MAYUR3598 closed 1 year ago

MAYUR3598 commented 2 years ago

Hi Team,

getting below error while importing tgw resources following below command: ./aws2tf.sh -v yes -t tgw

Logs: on aws_ec2_transit_gateway_route__tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16.tf line 6, in resource "aws_ec2_transit_gateway_route" "tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16": 6: transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.tgw-attach-0775386e6f5a52a99.id

A managed resource "aws_ec2_transit_gateway_vpc_attachment" "tgw-attach-0775386e6f5a52a99" has not been declared in the root module. fix default SG's Terraform validate ...

Error: Reference to undeclared resource

on aws_ec2_transit_gateway_route__tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16.tf line 6, in resource "aws_ec2_transit_gateway_route" "tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16": 6: transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.tgw-attach-06b62799c8aa0b843.id

A managed resource "aws_ec2_transit_gateway_vpc_attachment" "tgw-attach-06b62799c8aa0b843" has not been declared in the root module.

Error: Reference to undeclared resource

on aws_ec2_transit_gateway_route__tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16.tf line 6, in resource "aws_ec2_transit_gateway_route" "tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16": 6: transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.tgw-attach-0775386e6f5a52a99.id

A managed resource "aws_ec2_transit_gateway_vpc_attachment" "tgw-attach-0775386e6f5a52a99" has not been declared in the root module.

awsandy commented 2 years ago

Hi,

Could you confirm the TGW attachment in question is in an "available" state ?

aws2tf ignores attachments that are not "available" because the resource being attached to the TGW is likely to have been deleted and so the attachment is no longer intended/valid.

MAYUR3598 commented 2 years ago

Hi,

I've verified twice and attachments shows in available state.

awsandy commented 2 years ago

Thanks for checking.

Are these attachments to resources within the same AWS account - or another AWS account ?

Have a possible fix in the pipeline - although it's wrapped up in other v4.xx testing, as there's quite a lot changed with the latest aws provider version.

MAYUR3598 commented 2 years ago

Hi,

1 attachment is from same account and another one is from same account but different region.

Also would like to get aws_route_table but it seems to very few resources defined in aws2tf.sh file, i can see the files in scripts but not sure how can we proceed?

awsandy commented 2 years ago

The cumulative mode will help you (-c yes)

You could start with ./aws2tf.sh -t tgw

Then for subsequent runs include "-c yes" as you import more resource types, eg to get a VPC & it's subnets, route tables etc...

./aws2tf.sh -c yes -t vpc -i vpc-xxxxxxxxxxx

or the other way around if that makes more sense - vpc first and tgw in cumulative mode

Crossing regions will require manual copying and merging of Terraform resources - as this tool currently uses a separate directory in the generated sub directory for each account-region combination.


It is possible to call the individual script files one at a time (having done an initial aws2tf.sh run.)

$ cd generated/tf.0123456789012/_region $ export AWS=aws

Then call then individually

../../scripts/107-get-route-table.sh vpc-xxxxxxxxxxx

It's not intended to be used this way - but I do it for testing/debugging.

awsandy commented 2 years ago

changes included - let me know if it fixes your issue

MAYUR3598 commented 2 years ago

Hi,

It's still giving me an error.

Error: Reference to undeclared resource

on aws_ec2_transit_gateway_route__tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16.tf line 6, in resource "aws_ec2_transit_gateway_route" "tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16": 6: transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.tgw-attach-06b62799c8aa0b843.id

A managed resource "aws_ec2_transit_gateway_vpc_attachment" "tgw-attach-06b62799c8aa0b843" has not been declared in the root module. type-get-transitgw.sh runtime 153 seconds Total runtime in seconds 153 Wed 4 May 2022 12:16:06 IST terraform fmt > /dev/null ...

Error: Reference to undeclared resource

on aws_ec2_transit_gateway_route__tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16.tf line 6, in resource "aws_ec2_transit_gateway_route" "tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16": 6: transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.tgw-attach-06b62799c8aa0b843.id

A managed resource "aws_ec2_transit_gateway_vpc_attachment" "tgw-attach-06b62799c8aa0b843" has not been declared in the root module. fix default SG's Terraform validate ...

Error: Reference to undeclared resource

on aws_ec2_transit_gateway_route__tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16.tf line 6, in resource "aws_ec2_transit_gateway_route" "tgw-rtb-0f65b6e20bfd4083b_10_0_0_0_16": 6: transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.tgw-attach-06b62799c8aa0b843.id

A managed resource "aws_ec2_transit_gateway_vpc_attachment" "tgw-attach-06b62799c8aa0b843" has not been declared in the root module.

awsandy commented 2 years ago

Is this attachment the one related to the other region ?

MAYUR3598 commented 2 years ago

Yes that is from another region and resource type is Peering.

awsandy commented 1 year ago

Committed fixes that should now fix this. Although it is difficult to check/test every case. Just open a new issue if you continue to see problems