Copyright (c) 2025 MindMesh Academy. All rights reserved. This content is proprietary and may not be reproduced or distributed without permission.

4.3.2. Network Infrastructure as Code (IaC)

Network Infrastructure as Code (IaC) defines and manages network infrastructure through version-controlled code, ensuring consistent, repeatable, and auditable network deployments and configurations.

Scenario: You need to provision new VPCs with specific subnets, route tables, and Security Groups for each new development project. Manually configuring these networks is leading to inconsistencies and errors.

For network specialists, adopting Infrastructure as Code (IaC) is a critical practice for managing complex cloud networks. Instead of manually configuring network resources through the AWS Management Console or individual CLI commands, IaC allows you to define your entire network infrastructure in machine-readable files.

Key Tools for Network IaC:
Key Benefits of Network IaC:
  • Consistency: Eliminates "configuration drift" between environments.
  • Repeatability: Easily recreate network environments for testing, disaster recovery, or new deployments.
  • Version Control: All network infrastructure changes are tracked in a version control system (Git), providing an audit trail and enabling rollbacks.
  • Automation: Integrate network deployments into CI/CD pipelines.
Practical Implementation: Simple CloudFormation Template for a VPC
AWSTemplateFormatVersion: '2010-09-09'
Description: A simple VPC with public and private subnets

Parameters:
  VpcCidr:
    Type: String
    Default: 10.0.0.0/16
    Description: CIDR block for the VPC

  PublicSubnetCidr:
    Type: String
    Default: 10.0.1.0/24
    Description: CIDR block for the public subnet

  PrivateSubnetCidr:
    Type: String
    Default: 10.0.2.0/24
    Description: CIDR block for the private subnet

  AvailabilityZone:
    Type: AWS::EC2::AvailabilityZone::Name
    Description: The Availability Zone for the subnets
    Default: us-east-1a

Resources:
  MyVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VpcCidr
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: MyNetworkVPC

  MyInternetGateway:
    Type: AWS::EC2::InternetGateway
    Tags:
      - Key: Name
        Value: MyNetworkIGW

  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref MyVPC
      InternetGatewayId: !Ref MyInternetGateway

  MyPublicSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: !Ref PublicSubnetCidr
      AvailabilityZone: !Ref AvailabilityZone
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: MyNetworkPublicSubnet

  MyPrivateSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: !Ref PrivateSubnetCidr
      AvailabilityZone: !Ref AvailabilityZone
      Tags:
        - Key: Name
          Value: MyNetworkPrivateSubnet

  MyPublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref MyVPC
      Tags:
        - Key: Name
          Value: MyNetworkPublicRT

  PublicRoute:
    Type: AWS::EC2::Route
    DependsOn: AttachGateway
    Properties:
      RouteTableId: !Ref MyPublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref MyInternetGateway

  AssociatePublicRouteTable:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref MyPublicSubnet
      RouteTableId: !Ref MyPublicRouteTable

  MyPrivateRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref MyVPC
      Tags:
        - Key: Name
          Value: MyNetworkPrivateRT

  AssociatePrivateRouteTable:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref MyPrivateSubnet
      RouteTableId: !Ref MyPrivateRouteTable

  # NAT Gateway would be added here, and a route from MyPrivateRouteTable to it.
  # For brevity, omitting NAT Gateway and its EIP here.

Outputs:
  VPCId:
    Description: The ID of the new VPC
    Value: !Ref MyVPC
  PublicSubnetId:
    Description: The ID of the public subnet
    Value: !Ref MyPublicSubnet
  PrivateSubnetId:
    Description: The ID of the private subnet
    Value: !Ref MyPrivateSubnet

⚠️ Common Pitfall: Making manual changes to infrastructure that was provisioned with IaC. This creates "configuration drift," where the actual state of the infrastructure no longer matches the state defined in the code, leading to failed updates and inconsistencies.

Key Trade-Offs:
  • Initial Setup Time vs. Long-Term Consistency: Setting up IaC (e.g., writing CloudFormation templates) requires an upfront investment but saves significant time and reduces errors in the long run.

Reflection Question: How does defining and managing network infrastructure through version-controlled code (using CloudFormation or CDK) fundamentally enable consistent, repeatable, and auditable network deployments and configurations, ensuring operational consistency?