Repository: getmoto/moto
Pull request #6750 (2023-09-01 07:38:46)

EC2: Add missing transitGatewayArn field for CreateTransitGatewayResponse
Fixes a small parity issue with AWS. Here's an link to the [examples](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-transit-gateway.html#examples) and I also tried it out against actual AWS and it does indeed populate the ARN on create.

This also fixes this bug https://github.com/localstack/localstack/issues/9042 in `localstack`

mnitchev: @bblommers The linter failed, but it should be fine now. Can you approve the workflows again?

Patch:
diff --git a/moto/ec2/models/transit_gateway.py b/moto/ec2/models/transit_gateway.py
index b6e6bfd2d..2805a0367 100644
--- a/moto/ec2/models/transit_gateway.py
+++ b/moto/ec2/models/transit_gateway.py
@@ -46,6 +46,10 @@ class TransitGateway(TaggedEC2Resource, CloudFormationModel):
     def owner_id(self) -> str:
         return self.ec2_backend.account_id
 
+    @property
+    def arn(self) -> str:
+        return f"arn:aws:ec2:{self.ec2_backend.region_name}:{self.ec2_backend.account_id}:transit-gateway/{self.id}"
+
     @staticmethod
     def cloudformation_name_type() -> str:
         return ""
@@ -62,7 +66,7 @@ class TransitGateway(TaggedEC2Resource, CloudFormationModel):
         cloudformation_json: Any,
         account_id: str,
         region_name: str,
-        **kwargs: Any
+        **kwargs: Any,
     ) -> "TransitGateway":
         from ..models import ec2_backends
 
diff --git a/moto/ec2/responses/transit_gateways.py b/moto/ec2/responses/transit_gateways.py
index f86afaab3..013ab2528 100644
--- a/moto/ec2/responses/transit_gateways.py
+++ b/moto/ec2/responses/transit_gateways.py
@@ -64,6 +64,7 @@ CREATE_TRANSIT_GATEWAY_RESPONSE = """<CreateTransitGatewayResponse xmlns="http:/
     <requestId>151283df-f7dc-4317-89b4-01c9888b1d45</requestId>
     <transitGateway>
         <transitGatewayId>{{ transit_gateway.id }}</transitGatewayId>
+        <transitGatewayArn>{{ transit_gateway.arn }}</transitGatewayArn>
         <ownerId>{{ transit_gateway.owner_id }}</ownerId>
         <description>{{ transit_gateway.description or '' }}</description>
         <createTime>{{ transit_gateway.create_time }}</createTime>
@@ -122,7 +123,7 @@ DESCRIBE_TRANSIT_GATEWAY_RESPONSE = """<DescribeTransitGatewaysResponse xmlns="h
                     </item>
                 {% endfor %}
             </tagSet>
-            <transitGatewayArn>arn:aws:ec2:us-east-1:{{ transit_gateway.owner_id }}:transit-gateway/{{ transit_gateway.id }}</transitGatewayArn>
+            <transitGatewayArn>{{ transit_gateway.arn }}</transitGatewayArn>
             <transitGatewayId>{{ transit_gateway.id }}</transitGatewayId>
         </item>
     {% endfor %}
@@ -166,7 +167,7 @@ MODIFY_TRANSIT_GATEWAY_RESPONSE = """<ModifyTransitGatewaysResponse xmlns="http:
                 </item>
             {% endfor %}
         </tagSet>
-        <transitGatewayArn>arn:aws:ec2:us-east-1:{{ transit_gateway.owner_id }}:transit-gateway/{{ transit_gateway.id }}</transitGatewayArn>
+        <transitGatewayArn>{{ transit_gateway.arn }}</transitGatewayArn>
         <transitGatewayId>{{ transit_gateway.id }}</transitGatewayId>
     </item>
     </transitGatewaySet>


Test patch:
diff --git a/tests/test_ec2/test_transit_gateway.py b/tests/test_ec2/test_transit_gateway.py
index 352c6d72e..d65551e7a 100644
--- a/tests/test_ec2/test_transit_gateway.py
+++ b/tests/test_ec2/test_transit_gateway.py
@@ -23,6 +23,10 @@ def test_create_transit_gateway():
     )
     gateway = response["TransitGateway"]
     assert gateway["TransitGatewayId"].startswith("tgw-")
+    assert (
+        gateway["TransitGatewayArn"]
+        == f"arn:aws:ec2:us-west-1:{ACCOUNT_ID}:transit-gateway/{gateway['TransitGatewayId']}"
+    )
     assert gateway["State"] == "available"
     assert gateway["OwnerId"] == ACCOUNT_ID
     assert gateway["Description"] == "my first gateway"
@@ -50,14 +54,13 @@ def test_create_transit_gateway():
     assert "CreationTime" in gateways[0]
     assert (
         gateways[0]["TransitGatewayArn"]
-        == f"arn:aws:ec2:us-east-1:{ACCOUNT_ID}:transit-gateway/{gateway['TransitGatewayId']}"
+        == f"arn:aws:ec2:us-west-1:{ACCOUNT_ID}:transit-gateway/{gateway['TransitGatewayId']}"
     )
     assert (
         gateways[0]["Options"]["AssociationDefaultRouteTableId"]
         == gateways[0]["Options"]["PropagationDefaultRouteTableId"]
     )
     del gateways[0]["CreationTime"]
-    del gateways[0]["TransitGatewayArn"]
     del gateways[0]["Options"]["AssociationDefaultRouteTableId"]
     assert gateway == gateways[0]