AWS VPN Site to Site - Création via Terraform


Dans cet article, nous allons déployer un VPN Site-to-Site, en utilisant Terraform, afin de connecter un réseau on-premise à un VPC via une Virtual Private Gateway 

variables.tf

On commence par déclarer les variables pour rendre le code réutilisable :

variable "aws_region" {
  description = "Région AWS"
  type        = string
  default     = "eu-west-1"
}

variable "vpc_id" {
  description = "ID du VPC cible"
  type        = string
}

variable "onprem_ip" {
  description = "Adresse IP publique de l'équipement on-premise"
  type        = string
}

variable "onprem_cidr" {
  description = "CIDR du réseau on-premise"
  type        = string
  default     = "192.168.0.0/24"
}

variable "bgp_asn" {
  description = "ASN BGP du Customer Gateway"
  type        = number
  default     = 65000
}


terraform.tfvars

aws_region  = "eu-west-1"
vpc_id      = "vpc-0abc12345def67890"
onprem_ip   = "203.0.113.10"
onprem_cidr = "192.168.0.0/24"
bgp_asn     = 65000


main.tf

Dans le fichier main.tf, ou mettre les VPNs dans un autre fichier par exemple vpn.tf

#Provider AWS
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.aws_region
}

#Customer Gateway (CGW)
#Customer Gateway (CGW)
resource "aws_customer_gateway" "cgw" {
  bgp_asn    = var.bgp_asn
  ip_address = var.onprem_ip
  type       = "ipsec.1"

  tags = {
    Name = "cgw-onprem"
  }
}

#Virtual Private Gateway (VGW)
resource "aws_vpn_gateway" "vgw" {
  vpc_id = var.vpc_id

  tags = {
    Name = "vgw-production"
  }
}

#Connexion VPN
resource "aws_vpn_connection" "vpn" {
  vpn_gateway_id      = aws_vpn_gateway.vgw.id
  customer_gateway_id = aws_customer_gateway.cgw.id
  type                = "ipsec.1"
  static_routes_only  = true

  tags = {
    Name = "vpn-onprem-to-aws"
  }
}

#Route statique vers le réseau on-premise
resource "aws_vpn_connection_route" "onprem_route" {
  vpn_connection_id      = aws_vpn_connection.vpn.id
  destination_cidr_block = var.onprem_cidr
}


Déploiement

$ terraform init
$ terraform plan
$ terraform apply

Résultat de la commande apply passe avec succès

Vérification depuis la console

On trouve bien la connexion VPN


Avec les deux Tunnels, et du coup les deux IPs à fournir à l'admin réseau on prem