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
resource "aws_default_vpc" "default_vpc" {
}
resource "aws_default_subnet" "subnet_a" {
availability_zone = "us-east-1a"
resource "aws_default_vpc" "default" {
tags = {
Name = "Default VPC"
}
}
resource "aws_default_vpc" "default" {}
resource "aws_security_group" "http" {
vpc_id = aws_default_vpc.default.id
ingress {
resource "aws_default_vpc" "default" {}
resource "aws_default_subnet" "default" {
for_each = local.availability_zones
availability_zone = each.value
resource "aws_default_vpc" "default" {
tags = {
"Name" = "Default vpc"
}
}
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.
Ensure to avoid using default VPC
It is better to define the own VPC and use it.
Parameters
-
arnoptional computed - string -
assign_generated_ipv6_cidr_blockoptional computed - bool -
cidr_blockoptional computed - string -
default_network_acl_idoptional computed - string -
default_route_table_idoptional computed - string -
default_security_group_idoptional computed - string -
dhcp_options_idoptional computed - string -
enable_classiclinkoptional computed - bool -
enable_classiclink_dns_supportoptional computed - bool -
enable_dns_hostnamesoptional computed - bool -
enable_dns_supportoptional - bool -
idoptional computed - string -
instance_tenancyoptional computed - string -
ipv6_association_idoptional computed - string -
ipv6_cidr_blockoptional computed - string -
main_route_table_idoptional computed - string -
owner_idoptional computed - string -
tagsoptional - map from string to string -
tags_alloptional computed - map from string to string
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_vpcbehaves 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.
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::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
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
FlowLog:
Type: AWS::EC2::FlowLog
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
FlowLog:
Type: AWS::EC2::FlowLog
Type: AWS::EC2::VPC
Condition: IfUSWestRegion
Properties:
CidrBlock: 172.16.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
Type: AWS::EC2::VPC
Properties:
CidrBlock: '10.0.0.0/16'
LnCustomVPC2:
Type: AWS::EC2::VPC
Properties:
Type: AWS::EC2::VPC
Properties:
EnableDnsHostnames: true
EnableDnsSupport: true
CidrBlock: 10.0.10.0/24
Tags:
"resourceType": "AWS::EC2::VPC",
"resourceId": "vpc-00daeeb328891ac24"
},
"orderingTimestamp": "2019-05-07T04:36:36.353Z"
},
"complianceType": "NON_COMPLIANT",
"Type" : "AWS::EC2::VPC",
"Properties" : {
"CidrBlock" : "10.0.0.0/16"
}
},
"VPC2" : {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"CidrBlock" : "10.0.0.0/16"
}
},
"VPC2" : {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"Tags" : [ {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} } ],
"CidrBlock" : "10.0.0.0/16"
}
},
"Type" : "AWS::EC2::VPC",
"Properties" : {
"Tags" : [ {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} } ],
"CidrBlock" : "10.0.0.0/16"
}
},
Parameters
-
CidrBlockrequired - String -
EnableDnsHostnamesoptional - Boolean -
EnableDnsSupportoptional - Boolean -
InstanceTenancyoptional - String -
Tagsoptional - List of Tag
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.