cloud security · Freelance

Hybrid Networking on AWS: Site-to-Site VPN, Active Directory and Windows Automation

A site-to-site VPN between an office network and a VPC, an Active Directory domain with scripted user creation, and Ansible roles for repeatable Windows and Oracle database builds.

  • AWS Site-to-Site VPN
  • Virtual Private Gateway
  • AWS Managed Microsoft AD
  • PowerShell
  • CloudFormation
  • Ansible
  • WinRM
  • Oracle Database

// Why this matters

Setting up an aws site to site vpn is usually the first step in a hybrid move: the application tier goes to a VPC while databases, file shares or identity stay in an office or data centre for a while. Until those two networks can talk privately, every migration step has to go over the public internet or not happen at all.

Identity is the other half. Windows workloads expect Active Directory for sign-in, group policy and service accounts. If a directory cannot be reached from the new network, servers cannot join the domain and applications that rely on Kerberos or LDAP fail in ways that are hard to debug.

I have built these pieces for clients who needed an encrypted bridge between an office and AWS, a directory with bulk-created users, and repeatable builds for Windows and Oracle database hosts. This post pulls the techniques together as one reproducible path.

// The problem

VPN setup fails in boring ways. The customer gateway is registered with the wrong public address, the office firewall is configured for different encryption parameters than AWS proposes, or the route tables never learn the on-premises prefixes. The tunnel shows UP while no traffic flows, or shows DOWN because the office side never initiates.

Directory work is slow when done through a GUI. Creating fifty accounts by hand produces typos, inconsistent group membership and no audit trail. A script that creates users must also be safe to run twice, because the second run after a partial failure should fix the gap, not error out on duplicates.

Server builds are the final source of drift. A Windows host configured by clicking and an Oracle install done from a half-remembered checklist cannot be rebuilt when the instance dies. Prerequisite kernel settings, groups, and response files all need to be code.

// The solution

For networking, create a customer gateway object that records the public address of the office device and the routing type. Create a virtual private gateway and attach it to the VPC. Create the VPN connection between them; AWS provisions two IPsec tunnels in different availability zones with their own outside addresses and pre-shared keys. Download the vendor-specific configuration from the console and apply it to the office firewall; this removes most parameter mismatches.

Choose routing deliberately. BGP is preferable when the device supports it because failover between the two tunnels is automatic and prefixes are learned dynamically. Static routing works everywhere: you list the on-premises CIDRs on the connection and propagate them into the VPC route tables. Either way, enable route propagation on the subnet route tables, and align security groups and network ACLs with the office ranges.

For identity, deploy a directory with CloudFormation so the domain, subnets and DNS are reviewable, then run a PowerShell script on a management instance that imports a CSV and creates users with Get-ADUser checks first. That check is what makes it idempotent. Passwords come from a secrets store at run time rather than being embedded in the template or the script.

For servers, Ansible handles both flavours. Windows hosts are managed over WinRM using an inventory group, and Oracle hosts over SSH. A role covers prerequisites (packages, kernel parameters, users and groups, directories), unpacks the installer, and runs a silent install with a templated response file. Roles are small and ordered so a failed run can be repeated.

Alternatives I weighed: AWS Client VPN (for individual users, not for joining networks), Direct Connect (better for steady heavy traffic but with weeks of lead time and a cost floor), and a self-managed VPN appliance (more control, more patching). Site-to-site VPN is the fastest route to private connectivity. For identity patterns across providers see One Config, Two Clouds: Group and Role Management as Code.

How it flows flow1. Customer gateway → 2. Virtual private gateway → 3. VPN connection → 4. Routes → 5. Device config → 6. Directory → 7. Users → 8. Configure01Customer gateway02Virtual privategateway03VPN connection04Routes05 · GATEDevice config06Directory07Users08Configure
How it flows. End-to-end flow. Dashed pink nodes are gates: points where a human or a check must pass before the next stage runs.
  1. Customer gatewayregister the on-premises VPN device public address
  2. Virtual private gatewayattach it to the VPC
  3. VPN connectioncreate two IPsec tunnels with pre-shared keys
  4. Routesenable propagation and add on-premises CIDRs
  5. Device config gateapply the downloaded config to the office firewall
  6. Directorydeploy a managed AD and join Windows hosts
  7. UsersPowerShell creates accounts from a CSV file
  8. ConfigureAnsible installs roles and the database on the hosts
OfficeVPN firewallClientsOn-prem CIDRTunnelsIPsec tunnel 1IPsec tunnel 2BGP or static routesAWS networkVirtual private gatewayVPC route tablesSecurity groupsWorkloadsManaged ADWindows serversOracle hosts
Hybrid topology. Two networks joined by IPsec, with identity available on both sides.
EngineerAWSOffice devicecreate CGW, VGW, VPN connectiontunnel details + device configapply configinitiate IKE negotiationtunnel UP, routes advertised
Bringing a tunnel up. Who does what, in what order.

// Before you start

  • An AWS account with permissions for EC2 networking, Directory Service and CloudFormation
  • An office VPN device (firewall or router) with a static public address and IPsec support
  • The on-premises address ranges, for example 192.168.0.0/16, that must not overlap the VPC CIDR
  • Ansible 2.15 or later with the pywinrm package for Windows targets
  • A secrets store holding directory admin and database credentials, referred to as <vault-path>

// Step-by-step implementation

  1. 01 Create the gateways and the VPN connection

    Register the office device as a customer gateway, create and attach the virtual private gateway, and create the connection. With static routing, pass the option explicitly. The command output contains the tunnel configuration; keep it away from logs and chat.

    Set the BGP ASN to a private value if you choose dynamic routing.

  2. 02 Propagate routes into the VPC

    The connection is useless until the subnets know where to send office-bound traffic. Enable propagation on each route table that needs it; AWS then installs the prefixes automatically and withdraws them when tunnels drop.

    Do the same for any route table used by private subnets that host servers the office must reach.

  3. 03 Configure the office device and check tunnel state

    Download the vendor configuration for your device model from the VPN connection page and apply it. It contains the encryption and lifetime parameters AWS expects, so you are not guessing. Remember that many devices only bring a tunnel up when interesting traffic flows, so generate a ping from the office side.

    Check both tunnels. A single tunnel up is a single point of failure, and AWS performs maintenance on one tunnel at a time.

  4. 04 Deploy the directory with CloudFormation

    A managed directory gives you a domain without running domain controllers yourself. It needs two subnets in different zones and a admin credential. Reference the credential through a dynamic reference to the secrets store so it never appears in the template file.

    Point the VPC DHCP options at the directory DNS so joined hosts resolve the domain.

  5. 05 Create users from a CSV with an idempotent script

    The script imports the CSV, checks whether each account exists, and creates only the missing ones. Idempotence is the point: you can re-run after fixing a bad row without duplicates or errors.

    Take the initial password from a secure prompt or secrets store at run time. Force a change at first logon, and add users to groups from a column in the CSV.

  6. 06 Prepare Windows targets for Ansible over WinRM

    Ansible talks to Windows over WinRM, so enable it on the host and use HTTPS or Kerberos in anything beyond a lab. The inventory sets the connection type and transport; credentials come from an encrypted vars file or an environment variable, never from plaintext inventory.

    Test with the win_ping module before writing any roles.

  7. 07 Install an Oracle database with an Ansible role

    A role in this domain has four jobs: prerequisites, installer, response file, post-install. Prerequisites cover packages, kernel parameters and the oinstall and dba groups. The install is silent and driven by a templated response file, which makes it reproducible.

    Guard each step with creates or a check so the role can be re-run safely after a partial failure.

// How to verify it works

  • describe-vpn-connections shows both tunnels with Status UP.
  • A ping from an office host reaches a private instance in the VPC, and a reverse ping also works.
  • The VPC route table lists the on-premises CIDR with GatewayId set to the virtual private gateway.
  • Re-running create-users.ps1 prints exists for every row and creates nothing new.
  • ansible-playbook with the Oracle role reports changed on the first run and ok on the second.

// Design decisions and trade-offs

  • Use a Virtual Private Gateway with two tunnels and route propagation
  • Choose static or BGP routing based on what the customer gateway device supports
  • Provision the directory and users with CloudFormation plus a PowerShell bootstrap
  • Keep admin credentials out of templates by reading them from a secrets store
  • Configure Windows and Oracle hosts with Ansible over WinRM or SSH, never by hand

// Pitfalls and lessons learned

  • Overlapping CIDRs between the office and the VPC cannot be routed. Check ranges before creating anything.
  • Security groups and network ACLs that do not allow the office ranges give a perfect tunnel with no traffic.
  • A one-way tunnel is common when the office device only responds; initiate traffic from the office side.
  • Embedding the directory admin credential in a template or script leaks it into logs and history. Use dynamic references.
  • WinRM over plain HTTP with basic authentication sends credentials in clear text; use HTTPS or Kerberos.
  • Oracle installers fail late on missing prerequisites; run the prerequisite checks first and fail fast.

// Frequently asked questions

How do I set up an aws site to site vpn?

Create a customer gateway, a virtual private gateway attached to the VPC, and a VPN connection; apply the downloaded configuration to the office device and enable route propagation.

Static routes or BGP?

BGP gives automatic failover and dynamic routes if your device supports it; static routes are simpler and work on any device.

Why is my VPN tunnel down?

Most often an IKE version or pre-shared key mismatch, or no traffic initiated from the office side.

How do I create many Active Directory users at once?

Import a CSV in PowerShell, check each account with Get-ADUser, and create only the missing ones.

Can Ansible manage Windows servers?

Yes, over WinRM with the ansible.windows collection; use HTTPS or Kerberos for authentication.

// The outcome

The office and the VPC share private connectivity over encrypted tunnels, new directory users are created from a CSV with one command, and server builds are repeatable rather than tribal knowledge.

// Where to take it next