AWS Amazon EC2 Only Internet Gateway

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

aws_egress_only_internet_gateway (Terraform)

The Only Internet Gateway in Amazon EC2 can be configured in Terraform with the resource name aws_egress_only_internet_gateway. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

egress_only_internet_gateway.tf#L4
resource "aws_egress_only_internet_gateway" "egress_only_internet_gateway" {
  count = var.enable_egress_only_internet_gateway ? 1 : 0

  vpc_id = var.vpc_id != "" && ! var.enable_vpc ? var.vpc_id : element(concat(aws_vpc.vpc.*.id, [""]), 0)

  lifecycle {
egress_gw.tf#L1
resource "aws_egress_only_internet_gateway" "egressinet" {
  vpc_id = aws_vpc.default.id

  tags = {
    Name = var.egressinet_tag
  }
main.tf#L5
resource "aws_egress_only_internet_gateway" "egress" {
  vpc_id = aws_vpc.vpc.id
}

resource "aws_internet_gateway" "gateway" {
  vpc_id = aws_vpc.vpc.id
f0-internet-gateway.tf#L10
resource "aws_egress_only_internet_gateway" "egress-only-gateway" {
  vpc_id = module.vpc_settings.VPC_ID

  tags = merge(
  module.project_provider_settings.tags,
  var.EGW-tags
main.tf#L7
resource "aws_egress_only_internet_gateway" "this" {
  tags   = var.tags
  vpc_id = var.vpc_id
}

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

  • id optional computed - string
  • tags optional - map from string to string
  • vpc_id required - string

Explanation in Terraform Registry

[IPv6 only] Creates an egress-only Internet gateway for your VPC. An egress-only Internet gateway is used to enable outbound communication over IPv6 from instances in your VPC to the Internet, and prevents hosts outside of your VPC from initiating an IPv6 connection with your instance.

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

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

Example Usage from GitHub

002-gateway.yml#L9
    Type: 'AWS::EC2::EgressOnlyInternetGateway'
    Properties:
      VpcId: !ImportValue oe-integrations-vpc
  VPCGatewayAttachment:
    Type: 'AWS::EC2::VPCGatewayAttachment'
    Properties:
Route%20with%20Egress-Only%20Internet%20Gateway.yml#L9
    Type: AWS::EC2::EgressOnlyInternetGateway
    Properties:
      VpcId: !Ref VPC
  RouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
template-core.yml#L82
    Type: 'AWS::EC2::EgressOnlyInternetGateway'
    Properties:
      VpcId: !Ref VPC

  InternetGateway:
    Type: 'AWS::EC2::InternetGateway'
aloa-network.yml#L63
    Type: "AWS::EC2::EgressOnlyInternetGateway"
    Properties:
      VpcId: !Ref VPC

# Attach Internet Gateway

EgressOnlyInternetGateway.yml#L2
  Type: AWS::EC2::EgressOnlyInternetGateway
  Properties:
    VpcId:
Ec2EgressOnlyInternetGatewaySpecification.json#L3
    "AWS::EC2::EgressOnlyInternetGateway": {
      "Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-egressonlyinternetgateway.html",
      "Properties": {
        "VpcId": {
          "Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-egressonlyinternetgateway.html#cfn-ec2-egressonlyinternetgateway-vpcid",
          "PrimitiveType": "String",
EC2EgressOnlyInternetGatewaySpecification.json#L3
    "AWS::EC2::EgressOnlyInternetGateway": {
      "Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-egressonlyinternetgateway.html",
      "Properties": {
        "VpcId": {
          "Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-egressonlyinternetgateway.html#cfn-ec2-egressonlyinternetgateway-vpcid",
          "PrimitiveType": "String",
Ec2EgressOnlyInternetGatewaySpecification.json#L3
    "AWS::EC2::EgressOnlyInternetGateway": {
      "Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-egressonlyinternetgateway.html",
      "Properties": {
        "VpcId": {
          "Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-egressonlyinternetgateway.html#cfn-ec2-egressonlyinternetgateway-vpcid",
          "PrimitiveType": "String",
aws-cloud-formation.json#L104
        "Type": "AWS::EC2::EgressOnlyInternetGateway",
        "Properties": {
            "VpcId": {
                "Ref": "OneVpcToRuleThemAll"
            }
        }
aws-cloud-formation.json#L104
        "Type": "AWS::EC2::EgressOnlyInternetGateway",
        "Properties": {
            "VpcId": {
                "Ref": "OneVpcToRuleThemAll"
            }
        }

Parameters

Explanation in CloudFormation Registry

[IPv6 only] Specifies an egress-only internet gateway for your VPC. An egress-only internet gateway is used to enable outbound communication over IPv6 from instances in your VPC to the internet, and prevents hosts outside of your VPC from initiating an IPv6 connection with your instance.

Frequently asked questions

What is AWS Amazon EC2 Only Internet Gateway?

AWS Amazon EC2 Only Internet Gateway 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 Only Internet Gateway?

For Terraform, the SebastianUA/terraform-aws-vpc, EmanuelBurgess/Spencerscode and romedawg/aws_infra source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the wlsivabudda/openexchange-integration-infra, j-crotty/CloudFormation and Display-Lab/micca-infrastructure source code examples are useful. See the CloudFormation Example section for further details.