AWS Amazon EC2 VPC

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

aws_default_vpc (Terraform)

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

Example Usage from GitHub

vpc-config.tf#L1
resource "aws_default_vpc" "default_vpc" {

}

resource "aws_default_subnet" "subnet_a" {
  availability_zone = "us-east-1a"
main.tf#L12
resource "aws_default_vpc" "default" {
  tags = {
    Name = "Default VPC"
  }
}

sg.tf#L1
resource "aws_default_vpc" "default" {}

resource "aws_security_group" "http" {
  vpc_id = aws_default_vpc.default.id

  ingress {
vpc.tf#L9
resource "aws_default_vpc" "default" {}

resource "aws_default_subnet" "default" {
  for_each = local.availability_zones

  availability_zone = each.value
network.tf#L10
resource "aws_default_vpc" "default" {
    tags = {
      "Name" = "Default vpc"
    }

}

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).

Security Best Practices for aws_default_vpc

There is 1 setting in aws_default_vpc that should be taken care of for security reasons. The following section explain an overview and example code.

risk-label

Ensure to avoid using default VPC

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

Review your AWS Amazon EC2 settings

You can check if the aws_default_vpc setting in your .tf file is correct in 3 min with Shisho Cloud.

Parameters

Explanation in Terraform Registry

Provides a resource to manage the default AWS VPC in the current region. For AWS accounts created after 2013-12-04, each region comes with a Default VPC. This is an advanced resource, and has special caveats to be aware of when using it. Please read this document in its entirety before using this resource. The aws_default_vpc behaves differently from normal resources, in that Terraform does not create this resource, but instead "adopts" it into management.

Tips: Best Practices for The Other AWS Amazon EC2 Resources

In addition to the aws_network_acl_rule, 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_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::VPC (CloudFormation)

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

Example Usage from GitHub

vpc_has_multi_attached_flowlog.yml#L4
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16

  FlowLog:
    Type: AWS::EC2::FlowLog
vpc_has_unattached_flowlog_multi.yml#L4
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16

  FlowLog:
    Type: AWS::EC2::FlowLog
peering.yml#L34
    Type: AWS::EC2::VPC
    Condition: IfUSWestRegion
    Properties:
      CidrBlock: 172.16.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
SimpleVPCPeer.yml#L3
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: '10.0.0.0/16'
  LnCustomVPC2:
    Type: AWS::EC2::VPC
    Properties:
VPC.yml#L2
  Type: AWS::EC2::VPC
  Properties:
    EnableDnsHostnames: true
    EnableDnsSupport: true
    CidrBlock: 10.0.10.0/24
    Tags:
config_payload.json#L22
          "resourceType": "AWS::EC2::VPC",
          "resourceId": "vpc-00daeeb328891ac24"
        },
        "orderingTimestamp": "2019-05-07T04:36:36.353Z"
      },
      "complianceType": "NON_COMPLIANT",
cfn-subnet-initial-stack.json#L15
           "Type" : "AWS::EC2::VPC",
           "Properties" : {
               "CidrBlock" : "10.0.0.0/16"
           }
        },
        "VPC2" : {
cfn-subnet-update-cidr-stack.json#L15
           "Type" : "AWS::EC2::VPC",
           "Properties" : {
               "CidrBlock" : "10.0.0.0/16"
           }
        },
        "VPC2" : {
cfn-networkacl-initial-stack.json#L14
            "Type" : "AWS::EC2::VPC",
            "Properties" : {
                "Tags" : [ {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} } ],
                "CidrBlock" : "10.0.0.0/16"
            }
        },
cfn-networkacl-update-stack.json#L14
            "Type" : "AWS::EC2::VPC",
            "Properties" : {
                "Tags" : [ {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} } ],
                "CidrBlock" : "10.0.0.0/16"
            }
        },

Parameters

Explanation in CloudFormation Registry

Specifies a VPC with the specified IPv4 CIDR block. The smallest VPC you can create uses a /28 netmask (16 IPv4 addresses), and the largest uses a /16 netmask (65,536 IPv4 addresses). For more information about how large to make your VPC, see Your VPC and Subnets in the Amazon Virtual Private Cloud User Guide.

Frequently asked questions

What is AWS Amazon EC2 VPC?

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

For Terraform, the BetaMedina/terraform-test-ecs, manvithis/Terraform and hugoalbertoramirez/tf-masterclass-demo source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the stelligent/cfn_nag, stelligent/cfn_nag and elnurm/aws_cloudformation_templates source code examples are useful. See the CloudFormation Example section for further details.