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.
First of all, you need to install AWS CIDR finder in your account. The included install.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.
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 properties called CidrBlock1, CidrBlock2, and CidrBlock3 to contain the 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 cidr block from the CidrFindr resource
Subnet1:
Type: AWS::EC2::Subnet
Properties:
CidrBlock: !GetAtt CidrFindr.CidrBlock1
VpcId: !Ref Vpc
# Use the second cidr block from the CidrFindr resource
Subnet2:
Type: AWS::EC2::Subnet
Properties:
CidrBlock: !GetAtt CidrFindr.CidrBlock2
VpcId: !Ref Vpc
# Use the third cidr block from the CidrFindr resource
Subnet3:
Type: AWS::EC2::Subnet
Properties:
CidrBlock: !GetAtt CidrFindr.CidrBlock3
VpcId: !Ref Vpc