AWS Amazon EC2 VPC

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

aws_vpc (Terraform)

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

Example Usage from GitHub

main.tf#L1
resource "aws_vpc" "not_ok_vpc" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_vpc" "not_ok_vpc_2" {
  cidr_block = "10.0.0.0/16"
vpc.tf#L1
resource "aws_vpc" "tappo-vpc" {
    cidr_block           = "192.168.0.0/24"
    enable_dns_hostnames = true
    enable_dns_support   = true
    instance_tenancy     = "default"

main.tf#L1
resource "aws_vpc" "symbol-mainnet_us-east-1" {
  provider = aws.us-east-1
  cidr_block       = var.vpc_cidr
  instance_tenancy = "default"
  enable_dns_support = true
  enable_dns_hostnames = true
main.tf#L14
resource "aws_vpc" "test1" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "foo"
    awsrm = "test-acc"

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 VPC resource.

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::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

multi-region.yml#L7
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.100.0.0/16

  MyStackSet:
    Type: AWS::CloudFormation::StackSet
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
1_Create_VPC_Subnets.yml#L36
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 192.168.0.0/16
      EnableDnsSupport: true
      Tags:
        - Key: Name
example.yml#L15
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.100.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
test_cloudformer.json#L5
      "Type": "AWS::EC2::VPC",
      "Properties": {
      }
    },
    "vpcb545fcb": {
      "Type": "AWS::EC2::VPC",
cf-multi-vpc.json#L4
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "EnableDnsSupport": "true",
        "EnableDnsHostnames": "true",
        "CidrBlock": "10.0.0.0/16",
        "Tags": [
config_payload.json#L22
          "resourceType": "AWS::EC2::VPC",
          "resourceId": "vpc-00daeeb328891ac24"
        },
        "orderingTimestamp": "2019-05-07T04:36:36.353Z"
      },
      "complianceType": "NON_COMPLIANT",
cfn-subnet-update-cidr-stack.json#L15
           "Type" : "AWS::EC2::VPC",
           "Properties" : {
               "CidrBlock" : "10.0.0.0/16"
           }
        },
        "VPC2" : {
cfn-subnet-initial-stack.json#L15
           "Type" : "AWS::EC2::VPC",
           "Properties" : {
               "CidrBlock" : "10.0.0.0/16"
           }
        },
        "VPC2" : {

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 SnidermanIndustries/checkov-fork, tappoflw/tappo1 and symbol/symbol-infra source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the dgaydukov/cert-aws, stelligent/cfn_nag and stelligent/cfn_nag source code examples are useful. See the CloudFormation Example section for further details.