AWS Amazon EC2 VPN Connection

This page shows how to write Terraform and CloudFormation for Amazon EC2 VPN Connection and write them securely.

aws_vpn_connection (Terraform)

The VPN Connection in Amazon EC2 can be configured in Terraform with the resource name aws_vpn_connection. The following sections describe 3 examples of how to use the resource and its parameters.

Example Usage from GitHub

vpn.tf#L20
resource "aws_vpn_connection" "west" {
  count               = var.vpn_enabled
  vpn_gateway_id      = aws_vpn_gateway.west.id
  customer_gateway_id = aws_customer_gateway.gate.id
  type                = "ipsec.1"

40_gateways.tf#L28
resource "aws_vpn_connection" "A4LTGW_R1" {
  customer_gateway_id = aws_customer_gateway.router1.id
  transit_gateway_id  = aws_ec2_transit_gateway.tgw.id
  type                = aws_customer_gateway.router1.type

  tags = {
vpn_connection_test.tf#L12
resource "aws_vpn_connection" "vpn_connection" {
  customer_gateway_id = "dummy-customer-gateway-id"
  type                = "ipsec.1"
}

resource "aws_vpn_connection" "transit" {

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Manages an EC2 VPN connection. These objects can be connected to customer gateways, and allow you to establish tunnels between your network and Amazon.

Note: All arguments including tunnel1_preshared_key and tunnel2_preshared_key will be stored in the raw state as plain-text. Read more about sensitive data in state. Note: The CIDR blocks in the arguments tunnel1_inside_cidr and tunnel2_inside_cidr must have a prefix of /30 and be a part of a specific range. Read more about this in the AWS documentation.

Tips: Best Practices for The Other AWS Amazon EC2 Resources

In addition to the aws_default_vpc, AWS Amazon EC2 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

aws_default_vpc

Ensure to avoid using default VPC

It is better to define the own VPC and use it.

risk-label

aws_network_acl_rule

Ensure your network ACL rule blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

risk-label

aws_ebs_volume

Ensure to use a customer-managed key for EBS volume encryption

It is better to use a customer-managed key for EBS volume encryption. It can be gain more control over the encryption by using customer-managed keys (CMK).

risk-label

aws_instance

Ensure to avoid storing AWS access keys in user data

It is better to avoid storing AWS access keys in user data. `aws_iam_instance_profile` could be used instead.

risk-label

aws_security_group

Ensure your security group blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

Review your AWS Amazon EC2 settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

AWS::EC2::VPNConnection (CloudFormation)

The VPNConnection in EC2 can be configured in CloudFormation with the resource name AWS::EC2::VPNConnection. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

private-cloud-infra.yml#L80
    Type: AWS::EC2::VPNConnection
    DependsOn: [CustomerGateWay, VirtualPrivateGateWay]
    Properties:
      Type: ipsec.1
      CustomerGatewayId: !Ref CustomerGateWay
      VpnGatewayId:  !Ref VirtualPrivateGateWay
aws-vpn-stack.yml#L52
    Type: "AWS::EC2::VPNConnection"
    Properties:
      Type: ipsec.1
      CustomerGatewayId: !Ref CustomerGateway
      StaticRoutesOnly: False
      VpnGatewayId: !Ref VPNGateway
vpn.yml#L49
    Type: AWS::EC2::VPNConnection
    Properties:
      Type: ipsec.1
      StaticRoutesOnly: true
      CustomerGatewayId: !Ref CustomerGateway
      VpnGatewayId: !Ref VPNGateway
transit-vpn-attachment.yml#L43
    Type: AWS::EC2::VPNConnection
    Properties:
      CustomerGatewayId: !Ref rCustomerGatewayA
      TransitGatewayId: !Ref pTransitGatewayId
      Tags:
        - Key: Name
Cloudformation%20Openswan.yml#L177
    Type: AWS::EC2::VPNConnection
    Properties:
      Type: ipsec.1
      StaticRoutesOnly: 'true'
      CustomerGatewayId: !Ref CustomerGateway
      VpnGatewayId: !Ref VPNGateway
Common-VPNConnection.json#L69
      "Type" : "AWS::EC2::VPNConnection",
      "Properties" : {
        "Type" : "ipsec.1",
        "StaticRoutesOnly" : { "Fn::If" : [ "CreateVPNConnectionRoute", "true", "false" ]},
        "VpnGatewayId" : { "Fn::ImportValue" : { "Fn::Sub" : "${VPCStackName}-VPNGateway" }},
        "CustomerGatewayId" : { "Fn::ImportValue" : { "Fn::Sub" : "${CustomerGatewayStackName}-CustomerGateway" }},
vpn.json#L65
        "Type" : "AWS::EC2::VPNConnection",
        "Properties" : {
           "Type" : "ipsec.1",
           "StaticRoutesOnly" : "true",
           "CustomerGatewayId" : {"Ref" : "VirtualCGWUS"},
           "VpnGatewayId" : {"Ref" : "ProdVPGW"},
vpn.json#L65
        "Type" : "AWS::EC2::VPNConnection",
        "Properties" : {
           "Type" : "ipsec.1",
           "StaticRoutesOnly" : "true",
           "CustomerGatewayId" : {"Ref" : "VirtualCGWUS"},
           "VpnGatewayId" : {"Ref" : "ProdVPGW"},
aip-vpc-admin-vpn-abbottpark.cfn.json#L54
      "Type": "AWS::EC2::VPNConnection",
      "Properties": {
        "Tags": [
          {
            "Key": "Name",
            "Value": "aip-all-vpn"
custvpn.json#L26
      "Type" : "AWS::EC2::VPNConnection",
      "Properties" : {
         "Type" : "ipsec.1",
         "StaticRoutesOnly" : "true",
         "CustomerGatewayId" : {"Ref" : "CustomerGateway"},
         "VpnGatewayId" : {"Ref" : "vpnGWId"},

Parameters

Explanation in CloudFormation Registry

Specifies a VPN connection between a virtual private gateway and a VPN customer gateway or a transit gateway and a VPN customer gateway.

To specify a VPN connection between a transit gateway and customer gateway, use the TransitGatewayId and CustomerGatewayId properties.

To specify a VPN connection between a virtual private gateway and customer gateway, use the VpnGatewayId and CustomerGatewayId properties.

For more information, see AWS Site-to-Site VPN in the AWS Site-to-Site VPN User Guide.

Frequently asked questions

What is AWS Amazon EC2 VPN Connection?

AWS Amazon EC2 VPN Connection is a resource for Amazon EC2 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.

Where can I find the example code for the AWS Amazon EC2 VPN Connection?

For Terraform, the heldersepu/hs-scripts, pgastinger/terraform-aws-vpn-bgp-demo and infracost/infracost source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the Lorioux/CloudDevopsEngineerAWS, djoreilly/aws-vpn-testing and myshkin5/aws-musings source code examples are useful. See the CloudFormation Example section for further details.