Assigning Elastic IPs to Secondary Private IPs via SecondaryPrivateIpAddresses List Attribute

386 views Asked by At

Below is a snippet of my CloudFormation template used to associate an Elastic IP Address with the primary IP of a network interface:

        "MyInterfaceSelfEipAssociation": {
        "Properties": {
            "AllocationId": "eipalloc-XXXXX",
            "NetworkInterfaceId": {
                "Ref": "MyInterface"
            },
            "PrivateIpAddress": {
                "Fn::GetAtt": [
                    "MyInterface",
                    "PrimaryPrivateIpAddress"
                ]
            }
        },
        "Type": "AWS::EC2::EIPAssociation"

I want to do the same thing for the secondary IPs on this interface, of which there are two (particular IPs I have given in a list, not assigned by AWS). i.e. the "PrivateIpAddresses" block of the interface looks like this:

                "PrivateIpAddresses": [
                {
                    "PrivateIpAddress": "10.X.X.XX",
                    "Primary": "true"
                },
                {
                    "PrivateIpAddress": "10.X.X.XX",
                    "Primary": "false"
                },
                {
                    "PrivateIpAddress": "10.X.X.XX",
                    "Primary": "false"
                }
            ],

I know I can access the list of secondary private IPs with the Fn:GetAtt attribute call "SecondaryPrivateIpAddresses" which will return the two secondary private IPs above to me as a list. My question is, how can I address this list in JSON - by index?

For example, if I wanted to assign a private IP to the second element in the list of secondary IPs, is it valid to do something like:

                "PrivateIpAddress": {
                "Fn::GetAtt": [
                    "Bigip1subnet1Az1Interface",
                    "SecondaryPrivateIpAddresses[1]"
                ]
            }

How can I achieve this? I feel like it should be straightforward but I'm not clear on how to do this in JSON.

1

There are 1 answers

2
wjordan On BEST ANSWER

Use the Fn::Select intrinsic function to return a single object from a list of objects by index:

        "PrivateIpAddress": {
          "Fn::Select": [
            1,
            { "Fn::GetAtt": [
                "Bigip1subnet1Az1Interface",
                "SecondaryPrivateIpAddresses"
            ]}
          ]
        }