2

I'm using the CDK to create some infrastructure from a yaml template file. Some resources require multiple instances. I thought writing a function would be the easiest way to create multiple instance of the resource

Function

def create_vpn_connection_route(cidr_count, destination_cidr):
    vpn_connection_route = aws_ec2.CfnVPNConnectionRoute(
        self,
        f'vpn_connection_route{cidr_count}',
        vpn_connection_id=vpn_connection.ref,
        destination_cidr_block=destination_cidr
    )
    return vpn_connection_route

I then loop over it and generate the "Id" by enumarating over the destination_cidrs like so

for cidr_count, destination_cidr in enumerate(tenant_config['vpn_config'][0]['destination_cidrs']):
            create_vpn_connection_route(cidr_count, destination_cidr)

This is what's in my yaml

vpn_config:
    - private_ip:
        - 10.1.195.201/32
        - 10.1.80.20/32
        - 10.1.101.8/32

Is there a better way to do this in the CDK? and can I dynamically generate Id'S for resources?

Cheers

1 Answer 1

1

I don't know that it makes your code much better, but you can use a Construct instead of a function.

class VpnConnectionRoute(core.Construct):
    def __init__(self, scope, id_, vpn_connection, destination_cidr):
        super().__init__(scope, id_)

        self.vpn_connection_route = aws_ec2.CfnVPNConnectionRoute(
            self,
            'vpn_connection_route',
            vpn_connection_id=vpn_connection.vpn_id,
            destination_cidr_block=destination_cidr
        )

# ...
for cidr_count, destination_cidr in enumerate(tenant_config['vpn_config'][0]['destination_cidrs']):
  VpnConnectionRoute(self, f"route{cidr_count}", vpn_connection, destination_cidr)
  VpnConnectionRoute(self, f"route{cidr_count}", vpn_connection, destination_cidr)
  VpnConnectionRoute(self, f"route{cidr_count}", vpn_connection, destination_cidr)

CDK will automatically name your resources based on both the construct and your name. So the end result will look like:

    "route1vpnconnectionrouteAE1C11A9": {
      "Type": "AWS::EC2::VPNConnectionRoute",
      "Properties": {
        "DestinationCidrBlock": "10.1.195.201/32",
        "VpnConnectionId": {
          "Ref": "Vpn6F669752"
        }
      },
      "Metadata": {
        "aws:cdk:path": "app/route1/vpn_connection_route"
      }
    },

You can also just put destination_cidr inside your route name. CDK will remove all unsupported characters for you automatically.

for destination_cidr in tenant_config['vpn_config'][0]['destination_cidrs']:
        aws_ec2.CfnVPNConnectionRoute(
            self,
            f'VPN Connection Route for {destination_cidr}',
            vpn_connection_id=vpn_connection.vpn_id,
            destination_cidr_block=destination_cidr
        )

The best solution here probably depends on what you want to happen when these addresses change. For this particular resource type, any change in the name or the values will require a replacement anyway. So keeping the names consistent while the values change might not matter that much.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.