https://pulumi.com logo
#python
Title
# python
w

white-zoo-43002

12/04/2023, 11:53 AM
Hello Folks, I have a situation where my code is keep updating the plan with every iteration but nothing is changing in reality. Below the function:
Copy code
def create_route_tables(self, vpc, config, stack):
        self.route_tables = []

        for route_table in config['subnets'].keys():
            self.route_table = aws.ec2.RouteTable(
                f'rt-{route_table}-{stack}',
                vpc_id=vpc.id,
                routes=[
                    aws.ec2.RouteTableRouteArgs(
                        cidr_block='0.0.0.0/0',
                        gateway_id=self.igw.id if route_table == 'public_subnets' \
                        else self.nat_gateways[0].id if (route_table == 'eks_subnets' or route_table == 'database_subnets') \
                        else self.nat_gateways[1].id if route_table == 'tekton_subnets' \
                        else None
                    )
                ],
                tags={
                    "Name": f'route-table-{route_table}-{stack}'
                }
            )
            for i in range(len(config['subnets'][f'{route_table}'])):
                self.route_table_association = aws.ec2.RouteTableAssociation(
                    f'rt-assoc-{route_table}-{stack}-{i}',
                    subnet_id=self.public_subnets[i].id if route_table == 'public_subnets' \
                    else self.tekton_subnets[i].id if route_table == 'tekton_subnets' \
                    else self.eks_subnets[i].id if route_table == 'eks_subnets' \
                    else self.database_subnets[i].id if route_table == 'database_subnets' \
                    else None,
                    route_table_id=self.route_table.id
                )

            self.route_tables.append(self.route_table)
        return self.route_tables
and I get the below update in every execution.
Copy code
Updating (us-west-2-vpc-install-1):
     Type                   Name                                         Status           Info
     pulumi:pulumi:Stack    vpc-install-us-west-2-vpc-install-1                           
 ~   ├─ aws:ec2:RouteTable  rt-eks_subnets-us-west-2-vpc-install-1       updated (2s)     [diff: ~routes]
 ~   ├─ aws:ec2:RouteTable  rt-database_subnets-us-west-2-vpc-install-1  updated (3s)     [diff: ~routes]
 ~   └─ aws:ec2:RouteTable  rt-tekton_subnets-us-west-2-vpc-install-1    updated (4s)     [diff: ~routes]
Any idea why pls?
d

dry-keyboard-94795

12/04/2023, 12:27 PM
Can you do
pulumi preview --diff
to get more details about what's changing in the routes
l

limited-rainbow-51650

12/04/2023, 2:15 PM
My guess for now is the order in one of the collections changes every time and they are regenerated differently.
d

dry-keyboard-94795

12/04/2023, 2:24 PM
Yes,
nat_gateways
looks suspicious, as it's only those 3 route tables affected. @white-zoo-43002 would you be able to post the related code that builds that list?
w

white-zoo-43002

12/04/2023, 2:39 PM
Thank you for your answer! Yes, I am posting the code that created the NAT GWs below:
Copy code
def create_nat_gateways(self, stack):
        clusters = ['eks', 'tekton']
        self.nat_gateways = []
        self.nat_gateways_eips = []

        for cluster in clusters:
            self.nat_gateways_eip = aws.ec2.Eip(
                f'eip-{cluster}-{stack}',
                domain='vpc',
                tags={
                    "Name": f"eip-{cluster}-{stack}"
                }
            )
            self.nat_gateways_eips.append(self.nat_gateways_eip)
        
        for cluster in clusters:
            self.nat_gateway = aws.ec2.NatGateway(
                f'nat-gw-{cluster}-{stack}',
                allocation_id=self.nat_gateways_eips[clusters.index(cluster)].id,
                subnet_id=self.public_subnets[0].id,
                tags={
                    "Name": f"nat-gw-{cluster}-{stack}"
                }
            )
            self.nat_gateways.append(self.nat_gateway)
        return self.nat_gateways, self.nat_gateways_eips
d

dry-keyboard-94795

12/04/2023, 2:42 PM
I'd change to using a dict for storing these instead of an array. So you'd end up with something like:
Copy code
self.nat_gateways[cluster] = aws.ec2.NatGateway(...)
Though if it's static lists as shown, it's odd the routes would change. The full diff will help diagnosing further
w

white-zoo-43002

12/04/2023, 2:44 PM
okay let me try get the diff
Copy code
➜  platform-vpc-install git:(main) pulumi preview --diff                                                                   
Please choose a stack, or create a new one: us-east-1-vpc-install-1
Previewing update (us-east-1-vpc-install-1):
  pulumi:pulumi:Stack: (same)
    [urn=urn:pulumi:us-east-1-vpc-install-1::vpc-install::pulumi:pulumi:Stack::vpc-install-us-east-1-vpc-install-1]
    ~ aws:ec2/routeTable:RouteTable: (update)
        [id=rtb-04f03f011f4bba042]
        [urn=urn:pulumi:us-east-1-vpc-install-1::vpc-install::aws:ec2/routeTable:RouteTable::rt-database_subnets-us-east-1-vpc-install-1]
        [provider=urn:pulumi:us-east-1-vpc-install-1::vpc-install::pulumi:providers:aws::default_6_12_1::92b85d68-3b68-4dd2-974c-3b5542a017af]
      ~ routes: [
          ~ [0]: {
                  ~ cidrBlock   : "0.0.0.0/0" => "0.0.0.0/0"
                  + gatewayId   : "nat-03786086c899872c9"
                  - natGatewayId: "nat-03786086c899872c9"
                }
        ]
    ~ aws:ec2/routeTable:RouteTable: (update)
        [id=rtb-0ed160ccccb3eccff]
        [urn=urn:pulumi:us-east-1-vpc-install-1::vpc-install::aws:ec2/routeTable:RouteTable::rt-eks_subnets-us-east-1-vpc-install-1]
        [provider=urn:pulumi:us-east-1-vpc-install-1::vpc-install::pulumi:providers:aws::default_6_12_1::92b85d68-3b68-4dd2-974c-3b5542a017af]
      ~ routes: [
          ~ [0]: {
                  ~ cidrBlock   : "0.0.0.0/0" => "0.0.0.0/0"
                  + gatewayId   : "nat-03786086c899872c9"
                  - natGatewayId: "nat-03786086c899872c9"
                }
        ]
    ~ aws:ec2/routeTable:RouteTable: (update)
        [id=rtb-0f7c853062fb4b8bc]
        [urn=urn:pulumi:us-east-1-vpc-install-1::vpc-install::aws:ec2/routeTable:RouteTable::rt-tekton_subnets-us-east-1-vpc-install-1]
        [provider=urn:pulumi:us-east-1-vpc-install-1::vpc-install::pulumi:providers:aws::default_6_12_1::92b85d68-3b68-4dd2-974c-3b5542a017af]
      ~ routes: [
          ~ [0]: {
                  ~ cidrBlock   : "0.0.0.0/0" => "0.0.0.0/0"
                  + gatewayId   : "nat-079fa04b3d132cc78"
                  - natGatewayId: "nat-079fa04b3d132cc78"
                }
        ]
Resources:              
    ~ 3 to update
    32 unchanged
d

dry-keyboard-94795

12/04/2023, 2:53 PM
Ah ok. There's a note about this behaviour in the docs: https://www.pulumi.com/registry/packages/aws/api-docs/ec2/routetable/ When specifying a NAT gateway, use the
nat_gateway_id
parameter instead.
l

limited-rainbow-51650

12/04/2023, 2:56 PM
@white-zoo-43002 I don't understand this section:
Copy code
gateway_id=self.igw.id if route_table == 'public_subnets' \
                        else self.nat_gateways[0].id if (route_table == 'eks_subnets' or route_table == 'database_subnets') \
                        else self.nat_gateways[1].id if route_table == 'tekton_subnets' \
                        else None
What object is
self
referring to here?
w

white-zoo-43002

12/04/2023, 2:58 PM
they are part of a VPC class from which I instantiate the VPC object. so the nat gateway of this VPC object.
@dry-keyboard-94795 I did the change as you suggested.
Copy code
diff --git a/vpc.py b/vpc.py
index 7a82499..be32eb1 100644
--- a/vpc.py
+++ b/vpc.py
@@ -83,7 +83,7 @@ class Vpc:
                 routes=[
                     aws.ec2.RouteTableRouteArgs(
                         cidr_block='0.0.0.0/0',
-                        gateway_id=self.igw.id if route_table == 'public_subnets' \
+                        nat_gateway_id=self.igw.id if route_table == 'public_subnets' \
                         else self.nat_gateways[0].id if (route_table == 'eks_subnets' or route_table == 'database_subnets') \
                         else self.nat_gateways[1].id if route_table == 'tekton_subnets' \
                         else None
And now i get the opposite problem for another resource:
Copy code
➜  platform-vpc-install git:(main) ✗ pulumi preview --diff                                                                 <aws:cloudbees-saas-vpc-install-1>
Please choose a stack, or create a new one: us-east-1-vpc-install-1
Previewing update (us-east-1-vpc-install-1):
  pulumi:pulumi:Stack: (same)
    [urn=urn:pulumi:us-east-1-vpc-install-1::vpc-install::pulumi:pulumi:Stack::vpc-install-us-east-1-vpc-install-1]
    ~ aws:ec2/routeTable:RouteTable: (update)
        [id=rtb-032950570eb8b2832]
        [urn=urn:pulumi:us-east-1-vpc-install-1::vpc-install::aws:ec2/routeTable:RouteTable::rt-public_subnets-us-east-1-vpc-install-1]
        [provider=urn:pulumi:us-east-1-vpc-install-1::vpc-install::pulumi:providers:aws::default_6_12_1::92b85d68-3b68-4dd2-974c-3b5542a017af]
      ~ routes: [
          ~ [0]: {
                  ~ cidrBlock   : "0.0.0.0/0" => "0.0.0.0/0"
                  - gatewayId   : "igw-00c531588775592cd"
                  + natGatewayId: "igw-00c531588775592cd"
                }
        ]
Resources:              
    ~ 1 to update
    34 unchanged
➜  platform-vpc-install git:(main) ✗
d

dry-keyboard-94795

12/04/2023, 3:02 PM
The igw still needs to be under
gateway_id
w

white-zoo-43002

12/04/2023, 3:03 PM
ah… okay.
Many thanks! It worked! 🙂