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
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"
resource "aws_vpc" "tappo-vpc" {
cidr_block = "192.168.0.0/24"
enable_dns_hostnames = true
enable_dns_support = true
instance_tenancy = "default"
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
resource "aws_vpc" "test1" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "foo"
awsrm = "test-acc"
Parameters
-
arn
optional computed - string -
assign_generated_ipv6_cidr_block
optional - bool -
cidr_block
required - string -
default_network_acl_id
optional computed - string -
default_route_table_id
optional computed - string -
default_security_group_id
optional computed - string -
dhcp_options_id
optional computed - string -
enable_classiclink
optional computed - bool -
enable_classiclink_dns_support
optional computed - bool -
enable_dns_hostnames
optional computed - bool -
enable_dns_support
optional - bool -
id
optional computed - string -
instance_tenancy
optional - string -
ipv6_association_id
optional computed - string -
ipv6_cidr_block
optional computed - string -
main_route_table_id
optional computed - string -
owner_id
optional computed - string -
tags
optional - map from string to string -
tags_all
optional computed - map from string to string
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.
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::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.100.0.0/16
MyStackSet:
Type: AWS::CloudFormation::StackSet
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
Properties:
CidrBlock: 192.168.0.0/16
EnableDnsSupport: true
Tags:
- Key: Name
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.100.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
"Type": "AWS::EC2::VPC",
"Properties": {
}
},
"vpcb545fcb": {
"Type": "AWS::EC2::VPC",
"Type": "AWS::EC2::VPC",
"Properties": {
"EnableDnsSupport": "true",
"EnableDnsHostnames": "true",
"CidrBlock": "10.0.0.0/16",
"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" : {
Parameters
-
CidrBlock
required - String -
EnableDnsHostnames
optional - Boolean -
EnableDnsSupport
optional - Boolean -
InstanceTenancy
optional - String -
Tags
optional - 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 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.