jameszh / aws-cidr-finder

A lambda function, a cloudformation template to get it running, and example templates showing how to use a custom resource (calling the lambda) to automatically calculate CIDR ranges for subnets based on the VPC range.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CIDR finder

AWS CIDR Finder is a tool for adding more convenience to your AWS CloudFormation templates and AWS Service Catalog products by calculating the CIDR ranges of new subnets for you so that your users don't have to supply them.

In the DevOps world, where automation rules, the exact IP addresses of your servers don't really matter when they can otherwise be identified by tagging or API calls. For that reason, when launching CloudFormation stacks, it's good to have an option not to have to specify the CIDR ranges for your subnets.

AWS CIDR finder provides a Lambda function that can be used as a custom resource within your own CloudFormation templates to calculate CIDR ranges.

Usage

First of all, you need to install AWS CIDR finder in your account. The included deploy.sh script will create the lambda function for you and provide an exported CloudFormation value that you can make use of in your own templates.

Example CloudFormation template

The following example is included in full in the cfn directory and creates a new VPC along with 3 new subnets using automatically calculated CIDR ranges.

Resources:
  # Create a new VPC for the example
  Vpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 192.168.0.0/23

  # Call the custom resource, specify 3 subnets of different sizes.
  # The resource will have a property called CidrBlocks with an array of 3 CIDR block definitions
  CidrFindr:
    Type: Custom::CidrFindr
    Properties:
      ServiceToken: !ImportValue CidrFindr
      VpcId: !Ref Vpc  # Refer to the VPC created above
      Sizes: [24, 25, 26]  # 3 subnets of differing sizes

  # Use the first entry from CidrFindr's CidrBlocks property
  Subnet1:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: !Select [0, !GetAtt [CidrFindr, CidrBlocks]]
      VpcId: !Ref Vpc
      
  # Use the second entry from CidrFindr's CidrBlocks property
  Subnet2:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: !Select [1, !GetAtt [CidrFindr, CidrBlocks]]
      VpcId: !Ref Vpc
      
  # Use the third entry from CidrFindr's CidrBlocks property
  Subnet3:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: !Select [2, !GetAtt [CidrFindr, CidrBlocks]]
      VpcId: !Ref Vpc

About

A lambda function, a cloudformation template to get it running, and example templates showing how to use a custom resource (calling the lambda) to automatically calculate CIDR ranges for subnets based on the VPC range.

License:Apache License 2.0


Languages

Language:Python 89.5%Language:Shell 10.5%