Skip to content

Instantly share code, notes, and snippets.

@benbridts

benbridts/map.md Secret

Last active May 14, 2021 15:27
Show Gist options
  • Save benbridts/810ad93de0a2acd6744746478785973d to your computer and use it in GitHub Desktop.
Save benbridts/810ad93de0a2acd6744746478785973d to your computer and use it in GitHub Desktop.
map

Map

Assuming Subnets is equivalent to [ eu-west-1a, eu-west-1b]

Resources:
  EfsMountTargets:
    # this assigns each item from Subnets to x while iterating
    Map: {x: !Ref Subnets}
    Type: AWS::EFS::MountTarget
    Properties:
      FileSystemId: !Ref EFSVolume
      SecurityGroups:
        - !Ref EFSSecurityGroup
      SubnetId: !Ref x
Outputs:
  EfsMountIpAddress:
    Map: {x: !Ref Subnets}
    Description: !Sub "Ip Address for ${x}"
    # this might be better without the !Sub, assuming !GetAtt/Ref knows to resolve that in the Map
    Value: !GetAtt !Sub "EfsMountTargets.${x}.IpAddress"

This could be equivalent to

Resources:
  # the part after the . can be anything; as long as the same input keeps the same resource name
  # using the value as-is might help with linking mapped resources (see outputs)? 
  EfsMountTarget.eu-west-1a:
    Type: AWS::EFS::MountTarget
    Properties:
      FileSystemId: !Ref EFSVolume
      SecurityGroups:
        - !Ref EFSSecurityGroup
      SubnetId: eu-west-1a
  EfsMountTarget.eu-west-1b:
    Type: AWS::EFS::MountTarget
    Properties:
      FileSystemId: !Ref EFSVolume
      SecurityGroups:
        - !Ref EFSSecurityGroup
      SubnetId: eu-west-1a
Outputs:
  EfsMountIpAddress.eu-west-1a:
    Description: Ip Address for eu-west-1a
    Value: !GetAtt EfsMountTargets.eu-west-1a.IpAddress
  EfsMountIpAddress.eu-west-1b:
    Description: Ip Address for eu-west-1b
    Value: !GetAtt EfsMountTargets.eu-west-1b.IpAddress

Note: I think a simple Fn::Map function would still be usefull, for places where the Property expects a list. Eg:

# with !Ref AZs being [a, b]
!Map:
  - AZ: !Sub "${AWS::Region}${x}"
  - x: !Ref AZs

would be equivalent to

- !Sub "${AWS::Region}a"
- !Sub "${AWS::Region}b"

More notes: if the behaviour of different lengths is well defined, this could also be useful:

# with !Ref AZs being [a, b]
!Map:
  - AZ: !Sub "${AWS::Region}${x} - ${y}"
  - x: !Ref AZs
    y: [ one, two ]

would be equivalent to

- !Sub "${AWS::Region}a - one"
- !Sub "${AWS::Region}b - two"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment