Ansible Pilot

Create Network Infrastructure on AWS using Ansible Modules

Learn how to take advantage of Infrastructure as Code to create our own infrastructure on-demand on Amazon Web Services (AWS) using the Ansible collection and modules.

March 31, 2023
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

There is a growing need for streamlined network infrastructure management as more companies migrate to the cloud. Amazon Web Services (AWS), which is one of the most widely used cloud platforms, provides organizations with a variety of tools and services that make it easier for them to manage their network infrastructure. Creating a network infrastructure on AWS can be a daunting task as it involves various cloud computing challenges, but with Ansible modules, the process becomes easier and more manageable. Ansible is one of the most popular ways to manage infrastructure on Amazon. It is an open-source automation platform that lets businesses automate their infrastructure management tasks. In this article, we’ll cover how to create network infrastructure on Amazon Web Services (AWS) by utilizing Ansible modules. We’ll go through several Ansible modules that may assist in the creation of a virtual private cloud (VPC), subnet, internet gateway, route table, security group, instances, and load balancers.

What are the Ansible Modules?

Ansible is a platform for automation in infrastructure management that simplifies operations for businesses. It has a number of modules that can be used to automate different tasks, such as setting up and managing virtual machines, configuring networks, and deploying applications. Ansible modules are pre-written scripts that can be used to carry out particular tasks like configuring settings, installing software, and managing resources. There are hundreds of different Ansible modules available, and each one has its own specific purpose that it serves in the automation of infrastructure management tasks.

Why Should We Use Ansible Modules on AWS?

Using Ansible modules for the management of network infrastructure on Amazon Web Services (AWS) comes with a number of advantages. Initially, Ansible modules enable organizations to automate tasks, therefore saving time and reducing the probability of errors. Second, anyone can use Ansible modules without needing programming knowledge or technical expertise. Thirdly, Ansible modules work on different platforms and can be moved around, which makes it easy to manage infrastructure across many cloud providers. Prior to moving further let’s have a look on and In simple words, “” and “” are two collections of pre-built tools that help you manage your resources on Amazon Web Services (AWS) using Ansible. We can also combine additional tool like Terraform to define our infrastructure as code, so you can easily manage and deploy your AWS resources in a consistent and repeatable way. The “” collection contains modules that are created and maintained by Amazon. These modules cover a wide range of AWS services and are tested and supported by Amazon. They are a good choice for managing commonly used AWS resources, like EC2 instances and S3 buckets. The “” collection contains modules that are created by members of the Ansible community. These modules cover a wider range of AWS resources and can be useful for managing less commonly used resources or for more specialized use cases. While these modules are not officially supported by Amazon, they are often well-documented and can be a valuable resource for Ansible users. You can easily use these collections within Ansible by specifying the module source in your Ansible code. This allows you to quickly and easily deploy and manage your AWS resources without having to write everything from scratch.

Creating Network Infrastructure on AWS with Ansible Modules

Let’s look at how to use Ansible modules to build network architecture on Amazon. The preliminary steps will come first, and then we’ll get to the detailed procedure after that.

The Best Resources For Ansible


Video Course

Printed Book


Configuring Ansible for AWS

Before we can begin using Ansible to build network infrastructure on Amazon, we must first configure Ansible to work with AWS. The steps involved include installing the Amazon CLI, setting up AWS credentials, and getting the required Ansible modules.

Installing the AWS CLI

Installing the AWS CLI is the first step in configuring Ansible for AWS. The Amazon CLI is a command-line interface that enables interaction with numerous AWS services, such as EC2 and VPC. We can install the AWS CLI using our operating system’s package manager or by downloading it from the AWS website. More information:

AWS Account Credentials Setup

After the installation of the Amazon CLI, we must set up AWS credentials. Ansible uses these credentials to log in with Amazon and access our resources. The “aws configure” tool may be used to set up our Amazon credentials. This command will ask us for our AWS Access Key ID and Secret Access Key, which we can get from the AWS console.

Ansible Amazon Module Installation

Once the AWS Command Line Interface (CLI) is installed and set up, we can install the necessary Ansible modules for AWS. Using the “ansible-galaxy” command, which comes standard with Ansible, is the quickest and simplest method to do this. Just execute the following command to install the Amazon collections:

ansible-galaxy collection install
ansible-galaxy collection install

Now that we have Ansible configured for Amazon, we are able to begin the procedure for building network infrastructure. Here’s a rundown of what needs to be done:

  1. Creating a VPC
  2. Creating Subnets
  3. Creating an Internet Gateway
  4. Creating a Route Table
  5. Creating a Security Group

Building a Virtual Private Cloud (VPC)

A VPC is the first step in building network infrastructure on AWS (Virtual Private Cloud). The Amazon cloud’s VPCs are logically isolated areas where we may deploy resources for a virtual network that we specify.

Creating Subnets

After creating a virtual private cloud, the following step is to create subnets. A Virtual Private Cloud (VPC) may be divided into smaller networks (subnets) for easier management. We can use the “” module in Ansible to make subnets. This module supports a variety of parameters, including state, vpc_id, cidr_block, and tags. With the state parameter, we can tell the subnet what state we want it to be in. When the state is set to present, Ansible creates a subnet if one doesn’t already exist. The vpc_id parameter is used to indicate the identifier of the VPC to which the subnet should be associated. We may define the subnet’s IP range using the cidr_block parameter.

Creating an Internet Gateway

Making an Internet Gateway is the next step in the process in creating networks on AWS. Communication between instances in a virtual private cloud and the internet is enabled via an Internet Gateway, which is a horizontally scalable, redundant, and highly available component of a virtual private cloud. We can make use of the “” module in Ansible to build an Internet Gateway for ourselves.

- name: Create Internet Gateway
    vpc_id: vpc-1245678
    state: present
  register: igw

Creating a Route Table

Once we have created a VPC and an Internet Gateway, the next step is to create a Route Table. A Route Table is a set of rules, called routes, that are used to determine where network traffic is directed. To create a Route Table using Ansible, we can use the “” module. This module supports various parameters such as state, vpc_id, and tags.

- name: Set Up Public Subnet Route Table
    vpc_id: vpc-1245678
    region: us-west-1
      Name: Public
      - ''
      - dest:
        gateway_id: "{{ igw.gateway_id }}"
      - dest: ::/0
        gateway_id: "{{ igw.gateway_id }}"

Creating a Security Group

Creating a Security Group is the last part of setting up network infrastructure on AWS. A Security Group is like a virtual firewall for our instances. It controls the traffic coming in and going out. We may utilize the “” module of Ansible to build a Security Group. Other parameters supported by this module include state, name, description, and rules. The preferred state of the Security Group is specified using the state option. Ansible will create a Security Group if none already exists if the state is set to present. The name of the Security Group is specified using the name parameter. A description of the Security Group is provided via the description argument. The Security Group’s incoming and outbound traffic rules are specified using the rules parameter.

- name: Security Group Rule
    name: "{{ name }}"
    description: sg with rule descriptions
    vpc_id: vpc-1245678
    region: us-west-1
      - proto: tcp
        - 80
        rule_desc: allow all on port 80


Here we have covered the basics of using Ansible modules to deploy networks in the Amazon cloud. The process of setting up a VPC, subnets, an Internet gateway, a routing table, and a security group has been examined, and the required Ansible modules have been shown for each stage. By using Ansible to automate the creation of network infrastructure on AWS, we can save time and make sure that our deployments are consistent and correct. Using Ansible, we can concentrate on providing value to our customers while efficiently managing and scaling our network infrastructure on Amazon.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.


Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases