2018-01-18 15:51:06 +00:00
---
title: About the aws_vpcs Resource
2018-02-14 01:42:39 +00:00
platform: aws
2018-01-18 15:51:06 +00:00
---
2018-02-15 02:23:29 +00:00
# aws\_vpcs
2018-01-18 15:51:06 +00:00
2019-04-26 18:24:29 +00:00
Use the `aws_vpcs` Chef InSpec audit resource to test properties of some or all AWS Virtual Private Clouds (VPCs).
2018-01-18 15:51:06 +00:00
2018-02-20 03:09:51 +00:00
A VPC is a networking construct that provides an isolated environment. A VPC is contained in a geographic region, but spans availability zones in that region. A VPC may have multiple subnets, internet gateways, and other networking resources. Computing resources--such as EC2 instances--reside on subnets within the VPC.
2018-01-18 15:51:06 +00:00
2018-02-14 01:42:39 +00:00
Each VPC is uniquely identified by its VPC ID. In addition, each VPC has a non-unique CIDR IP Address range (such as 10.0.0.0/16) which it manages.
2018-01-18 15:51:06 +00:00
Every AWS account has at least one VPC, the "default" VPC, in every region.
<br>
2018-08-09 12:34:49 +00:00
## Availability
### Installation
2019-04-26 18:24:29 +00:00
This resource is distributed along with Chef InSpec itself. You can use it automatically.
2018-08-09 12:34:49 +00:00
### Version
This resource first became available in v2.0.16 of InSpec.
2018-01-18 15:51:06 +00:00
## Syntax
An `aws_vpcs` resource block uses an optional filter to select a group of VPCs and then tests that group.
2018-02-14 01:42:39 +00:00
# The control will pass if the filter returns at least one result. Use `should_not` if you expect zero matches.
2018-04-12 19:48:55 +00:00
# Since you always have at least one VPC, this will always pass.
2018-01-18 15:51:06 +00:00
describe aws_vpcs do
it { should exist }
end
2018-04-12 19:48:55 +00:00
# Insist that all VPCs use the same DHCP option set.
describe aws_vpcs.where { dhcp_options_id != 'dopt-12345678' } do
it { should_not exist }
end
2018-01-18 15:51:06 +00:00
<br>
## Examples
2019-04-26 18:24:29 +00:00
The following examples show how to use this Chef InSpec audit resource.
2018-01-18 15:51:06 +00:00
2018-04-12 19:48:55 +00:00
### Check for a Particular VPC ID
describe aws_vpcs do
its('vpc_ids') { should include 'vpc-12345678' }
end
### Use the VPC IDs to Get a List of Default Security Groups
aws_vpcs.vpc_ids.each do |vpc_id|
describe aws_security_group(vpc_id: vpc_id, group_name: 'default') do
it { should_not allow_in(port: 22) }
end
end
2018-01-18 15:51:06 +00:00
<br>
2018-04-12 19:48:55 +00:00
## Filter Criteria
### cidr_block
Filters the results to include only those VPCs that match the given IPv4 range. This is a string value.
# We shun the 10.0.0.0/8 space
describe aws_vpcs.where { cidr_block.start_with?('10') } do
it { should_not exist }
end
### dhcp_option_id
Filters the results to include only those VPCs that have the given DHCP Option Set.
# Insist on one DHCP option set for all VPCs.
describe aws_vpcs.where { dhcp_options_id != 'dopt-12345678' } do
it { should_not exist }
end
## Properties
### cidr_blocks
The cidr_blocks property provides a list of the CIDR blocks that the matched VPCs serve as strings.
describe aws_vpcs do
# This is simple array membership checking - not subnet membership
its('cidr_blocks') { should include '179.0.0.0/16' }
end
### dhcp_options_ids
The dhcp_option_set_ids property provides a de-duplicated list of the DHCP Option Set IDs that the matched VPCs use when assigning IPs to resources.
describe aws_vpcs do
its('dhcp_options_ids') { should include 'dopt-12345678' }
end
### vpc_ids
The vpc_ids property provides a list of the IDs of the matched VPCs.
describe aws_vpcs do
its('vpc_ids') { should include 'vpc-12345678' }
end
# Get a list of all VPC IDs
aws_vpcs.vpc_ids.each do |vpc_id|
# Do something with vpc_id
end
2018-01-18 15:51:06 +00:00
## Matchers
2019-04-26 18:24:29 +00:00
This Chef InSpec audit resource has the following special matchers. For a full list of available matchers, please visit our [Universal Matchers page](https://www.inspec.io/docs/reference/matchers/).
2018-02-14 01:42:39 +00:00
2018-01-18 15:51:06 +00:00
### exists
2018-02-14 01:42:39 +00:00
The control will pass if the filter returns at least one result. Use `should_not` if you expect zero matches.
2018-01-18 15:51:06 +00:00
# You will always have at least one VPC
describe aws_vpcs
it { should exist }
end
2018-05-10 18:57:53 +00:00
## AWS Permissions
Your [Principal](https://docs.aws.amazon.com/IAM/latest/UserGuide/intro-structure.html#intro-structure-principal) will need the `ec2:DescribeVpcs` action with Effect set to Allow.
You can find detailed documentation at [Actions, Resources, and Condition Keys for Amazon EC2](https://docs.aws.amazon.com/IAM/latest/UserGuide/list_amazonec2.html).