AWS Amazon EC2 Route

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

aws_route (Terraform)

The Route in Amazon EC2 can be configured in Terraform with the resource name aws_route. The following sections describe 4 examples of how to use the resource and its parameters.

Example Usage from GitHub

route.tf#L1
resource "aws_route" "source-route-private" {
  provider                  = aws.source
  route_table_id            = var.source-route_table-private-id
  destination_cidr_block    = data.aws_vpc.peer.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.vpc_peering_connection.id
}
main.tf#L241
resource "aws_route" "bastion_vpc_dr" {
  provider                  = aws.us-east-1
  count                     = length(module.dr_cluster.public_subnets)
  route_table_id            = module.primary_cluster.bastion_route_table
  destination_cidr_block    = element(module.dr_cluster.public_subnets, count.index)
  vpc_peering_connection_id = aws_vpc_peering_connection.bastion_connectivity_dr.id
route_tables.tf#L27
resource "aws_route" "public_internet_gateway_a" {
  route_table_id         = aws_route_table.public_table_a.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.igw_a.id

  timeouts {
route-tables.tf#L32
resource "aws_route" "ngw-default-route" {
  for_each = {for sd in local.subnet_data:sd.name=>sd
           if sd.layer == "ngw" }
  route_table_id         = aws_route_table.routers[each.value.name].id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.inet-gw.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

Explanation in Terraform Registry

Provides a resource to create a routing table entry (a route) in a VPC routing table.

NOTE on Route Tables and Routes: Terraform currently provides both a standalone Route resource and a Route Table resource with routes defined in-line. At this time you cannot use a Route Table with in-line routes in conjunction with any Route resources. Doing so will cause a conflict of rule settings and will overwrite rules. NOTE on gateway_id attribute: The AWS API is very forgiving with the resource ID passed in the gateway_id attribute. For example an aws_route resource can be created with an aws_nat_gateway or aws_egress_only_internet_gateway ID specified for the gateway_id attribute. Specifying anything other than an aws_internet_gateway or aws_vpn_gateway ID will lead to Terraform reporting a permanent diff between your configuration and recorded state, as the AWS API returns the more-specific attribute. If you are experiencing constant diffs with an aws_route resource, the first thing to check is that the correct attribute is being specified.

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

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

Example Usage from GitHub

vpc-peering.yml#L92
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId:
        Ref: myPrivateVPC
  PeeringRoute1:
    Type: AWS::EC2::Route
routes.serverless.yml#L4
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId:
        Ref: Vpc
      Tags:
        - Key: Name
Nested-Stack-VPC-Route-Table-1.yml#L15
    Type: AWS::EC2::Route
    Properties:
       RouteTableId: !Ref VPC1RouteTable
       DestinationCidrBlock: 10.2.0.0/16
       TransitGatewayId: !Ref Region1TransitGateway
  VPC1TGWRoute3:
Nested-Stack-VPC-Route-Table-2.yml#L15
    Type: AWS::EC2::Route
    Properties:
       RouteTableId: !Ref VPC1RouteTable
       DestinationCidrBlock: 10.2.0.0/16
       TransitGatewayId: !Ref Region1TransitGateway
  VPC1TGWRoute3:
serverless.yml#L117
      Type: AWS::EC2::RouteTable
      Properties:
        VpcId:
          Ref: ServerlessVPC

    DefaultPrivateRouteA:
routes.json#L8
      "Type": "AWS::EC2::RouteTable",
      "Properties": {
        "VpcId": {"Fn::ImportValue": "VPCId"},
        "Tags": [
          {
            "Key": "Name",
routes.json#L8
      "Type": "AWS::EC2::RouteTable",
      "Properties": {
        "VpcId": {"Fn::ImportValue": "VPCId"},
        "Tags": [
          {
            "Key": "Name",
route_tables.json#L45
            "Type": "AWS::EC2::RouteTable"
        },
        "rtb00404b6df41dfc37aassociation1": {
            "Properties": {
                "RouteTableId": {
                    "Ref": "rtb00404b6df41dfc37a"
AWS-Bosh-010-VPC-Peering.json#L329
      "Type": "AWS::EC2::Route",
      "Condition": "Peer1Vpc",
      "Properties": {
        "DestinationCidrBlock": { "Ref": "Peer1VpcCidr" },
        "RouteTableId": { "Fn::ImportValue": { "Fn::Sub": "${DeploymentName}-InternetRouteTable" } },
        "VpcPeeringConnectionId": { "Ref": "VpcPeer1" }
routing.json#L2
    "Type" : "AWS::EC2::RouteTable",
    "Properties" : {
      "VpcId" : {"Ref" : "VPC"}
    }
  },

Parameters

Explanation in CloudFormation Registry

Specifies a route in a route table within a VPC.

You must specify either DestinationCidrBlock or DestinationIpv6CidrBlock, plus the ID of one of the target resources.

If you create a route that references a transit gateway in the same template where you create the transit gateway, you must declare a dependency on the transit gateway attachment. The route table cannot use the transit gateway until it has successfully attached to the VPC. Add a DependsOn Attribute in the AWS::EC2::Route resource to explicitly declare a dependency on the AWS::EC2::TransitGatewayAttachment resource.

Frequently asked questions

What is AWS Amazon EC2 Route?

AWS Amazon EC2 Route 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 Route?

For Terraform, the atc-labs/terraform-aws-vpc-peering, bstascavage/terraform-vault-consul-deployment and annagtaraujo/Terraform-Transit-Gateway source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the lijoyoung/cloudformation, bcx-exa/open_source_triple_continent_traditional and aobao32/transit-gateway-workshop source code examples are useful. See the CloudFormation Example section for further details.