AWS Amazon EC2 VPN Gateway Attachment

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

aws_vpn_gateway_attachment (Terraform)

The VPN Gateway Attachment in Amazon EC2 can be configured in Terraform with the resource name aws_vpn_gateway_attachment. The following sections describe 2 examples of how to use the resource and its parameters.

Example Usage from GitHub

vpn_ptl.tf#L1
resource "aws_vpn_gateway_attachment" "ptl_vpn_gw_attachment" {
  count = var.ptl_connected ? 1 : 0
  vpc_id = aws_vpc.base_vpc.id
  vpn_gateway_id = data.aws_vpn_gateway.ptl_gateway[0].id
}
vpn.tf#L122
resource "aws_vpn_gateway_attachment" "vpn_attachment_vpg_1" {
  vpc_id         = aws_vpc.vpc_1.id
  vpn_gateway_id = aws_vpn_gateway.virtual_private_gateway_1.id
  depends_on = [
    aws_vpc.vpc_1,
    aws_vpn_gateway.virtual_private_gateway_1

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

Provides a Virtual Private Gateway attachment resource, allowing for an existing hardware VPN gateway to be attached and/or detached from a VPC. -> Note: The aws_vpn_gateway resource can also automatically attach the Virtual Private Gateway it creates to an existing VPC by setting the vpc_id attribute accordingly.

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::VPNGateway (CloudFormation)

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

Example Usage from GitHub

vpn-bgp.yml#L88
    Type: AWS::EC2::VPNGateway
    Properties:
      Type: ipsec.1
      Tags:
      - Key: Name
        Value:
aws-vpn-stack.yml#L26
    Type: "AWS::EC2::VPNGateway"
    Properties:
      Type: ipsec.1

  VPCGatewayAttachment:
    Type: "AWS::EC2::VPCGatewayAttachment"
vpn.yml#L21
    Type: "AWS::EC2::VPNGateway"
    Properties:
      Type: "ipsec.1"

  VpnConnection:
    Type: "AWS::EC2::VPNConnection"
mu.yml#L16
        Type: "AWS::EC2::VPNGateway"
        Properties:
          Type: ipsec.1
      VPNconnection:
        Type: "AWS::EC2::VPNConnection"
        Properties:
mu.yml#L16
        Type: "AWS::EC2::VPNGateway"
        Properties:
          Type: ipsec.1
      VPNconnection:
        Type: "AWS::EC2::VPNConnection"
        Properties:
vpn-bgp.json#L97
      "Type": "AWS::EC2::VPNGateway",
      "Properties": {
        "Type": "ipsec.1",
        "Tags": [
          {
            "Key": "Name",
vpn-bgp.json#L97
      "Type": "AWS::EC2::VPNGateway",
      "Properties": {
        "Type": "ipsec.1",
        "Tags": [
          {
            "Key": "Name",
indent_output.json#L93
            "Type": "AWS::EC2::VPNGatewayRoutePropagation"
        },
        "aclentry1": {
            "Properties": {
                "CidrBlock": "172.16.0.0/24",
                "Egress": "true",
vpc.json#L82
         "Type" : "AWS::EC2::VPNGateway",
         "Properties" : {
            "Type" : "ipsec.1"
         }
      },
      "VpnConnectionGreen" : {
vpngw.json#L14
        "Type" : "AWS::EC2::VPNGateway",
        "Properties" : {
           "Type" : "ipsec.1",
           "Tags" : [ { "Key" : "Name", "Value" : "Production" } ]
        }
     },

Parameters

Explanation in CloudFormation Registry

Specifies a virtual private gateway. A virtual private gateway is the endpoint on the VPC side of your VPN connection. You can create a virtual private gateway before creating the VPC itself.

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 Gateway Attachment?

AWS Amazon EC2 VPN Gateway Attachment 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 Gateway Attachment?

For Terraform, the nhsconnect/integration-adaptors and juliocesarscheidt/vpn-site-to-site-aws source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the librannk/AWS-CloudFormation-tmplte, djoreilly/aws-vpn-testing and anri-c/my-aws-cf-templates source code examples are useful. See the CloudFormation Example section for further details.