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
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 {
resource "aws_egress_only_internet_gateway" "egressinet" {
vpc_id = aws_vpc.default.id
tags = {
Name = var.egressinet_tag
}
resource "aws_egress_only_internet_gateway" "egress" {
vpc_id = aws_vpc.vpc.id
}
resource "aws_internet_gateway" "gateway" {
vpc_id = aws_vpc.vpc.id
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
resource "aws_egress_only_internet_gateway" "this" {
tags = var.tags
vpc_id = var.vpc_id
}
Parameters
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.
aws_default_vpc
Ensure to avoid using default VPC
It is better to define the own VPC and use it.
aws_network_acl_rule
Ensure your network ACL rule blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
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).
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.
aws_security_group
Ensure your security group blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
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
Type: 'AWS::EC2::EgressOnlyInternetGateway'
Properties:
VpcId: !ImportValue oe-integrations-vpc
VPCGatewayAttachment:
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
Type: AWS::EC2::EgressOnlyInternetGateway
Properties:
VpcId: !Ref VPC
RouteTable:
Type: AWS::EC2::RouteTable
Properties:
Type: 'AWS::EC2::EgressOnlyInternetGateway'
Properties:
VpcId: !Ref VPC
InternetGateway:
Type: 'AWS::EC2::InternetGateway'
Type: "AWS::EC2::EgressOnlyInternetGateway"
Properties:
VpcId: !Ref VPC
# Attach Internet Gateway
Type: AWS::EC2::EgressOnlyInternetGateway
Properties:
VpcId:
"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::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::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",
"Type": "AWS::EC2::EgressOnlyInternetGateway",
"Properties": {
"VpcId": {
"Ref": "OneVpcToRuleThemAll"
}
}
"Type": "AWS::EC2::EgressOnlyInternetGateway",
"Properties": {
"VpcId": {
"Ref": "OneVpcToRuleThemAll"
}
}
Parameters
-
VpcId
required - String
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.