1. Packages
  2. AWS
  3. API Docs
  4. ec2
  5. getVpc
AWS v6.77.0 published on Wednesday, Apr 9, 2025 by Pulumi

aws.ec2.getVpc

Explore with Pulumi AI

AWS v6.77.0 published on Wednesday, Apr 9, 2025 by Pulumi

aws.ec2.Vpc provides details about a specific VPC.

This resource can prove useful when a module accepts a vpc id as an input variable and needs to, for example, determine the CIDR block of that VPC.

Example Usage

The following example shows how one might accept a VPC id as a variable and use this data source to obtain the data necessary to create a subnet within it.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as std from "@pulumi/std";

const config = new pulumi.Config();
const vpcId = config.requireObject<any>("vpcId");
const selected = aws.ec2.getVpc({
    id: vpcId,
});
const example = new aws.ec2.Subnet("example", {
    vpcId: selected.then(selected => selected.id),
    availabilityZone: "us-west-2a",
    cidrBlock: selected.then(selected => std.cidrsubnet({
        input: selected.cidrBlock,
        newbits: 4,
        netnum: 1,
    })).then(invoke => invoke.result),
});
Copy
import pulumi
import pulumi_aws as aws
import pulumi_std as std

config = pulumi.Config()
vpc_id = config.require_object("vpcId")
selected = aws.ec2.get_vpc(id=vpc_id)
example = aws.ec2.Subnet("example",
    vpc_id=selected.id,
    availability_zone="us-west-2a",
    cidr_block=std.cidrsubnet(input=selected.cidr_block,
        newbits=4,
        netnum=1).result)
Copy
package main

import (
	"github.com/pulumi/pulumi-aws/sdk/v6/go/aws/ec2"
	"github.com/pulumi/pulumi-std/sdk/go/std"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		cfg := config.New(ctx, "")
		vpcId := cfg.RequireObject("vpcId")
		selected, err := ec2.LookupVpc(ctx, &ec2.LookupVpcArgs{
			Id: pulumi.StringRef(vpcId),
		}, nil)
		if err != nil {
			return err
		}
		invokeCidrsubnet, err := std.Cidrsubnet(ctx, &std.CidrsubnetArgs{
			Input:   selected.CidrBlock,
			Newbits: 4,
			Netnum:  1,
		}, nil)
		if err != nil {
			return err
		}
		_, err = ec2.NewSubnet(ctx, "example", &ec2.SubnetArgs{
			VpcId:            pulumi.String(selected.Id),
			AvailabilityZone: pulumi.String("us-west-2a"),
			CidrBlock:        pulumi.String(invokeCidrsubnet.Result),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
Copy
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Aws = Pulumi.Aws;
using Std = Pulumi.Std;

return await Deployment.RunAsync(() => 
{
    var config = new Config();
    var vpcId = config.RequireObject<dynamic>("vpcId");
    var selected = Aws.Ec2.GetVpc.Invoke(new()
    {
        Id = vpcId,
    });

    var example = new Aws.Ec2.Subnet("example", new()
    {
        VpcId = selected.Apply(getVpcResult => getVpcResult.Id),
        AvailabilityZone = "us-west-2a",
        CidrBlock = Std.Cidrsubnet.Invoke(new()
        {
            Input = selected.Apply(getVpcResult => getVpcResult.CidrBlock),
            Newbits = 4,
            Netnum = 1,
        }).Apply(invoke => invoke.Result),
    });

});
Copy
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.ec2.Ec2Functions;
import com.pulumi.aws.ec2.inputs.GetVpcArgs;
import com.pulumi.aws.ec2.Subnet;
import com.pulumi.aws.ec2.SubnetArgs;
import com.pulumi.std.StdFunctions;
import com.pulumi.std.inputs.CidrsubnetArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }

    public static void stack(Context ctx) {
        final var config = ctx.config();
        final var vpcId = config.get("vpcId");
        final var selected = Ec2Functions.getVpc(GetVpcArgs.builder()
            .id(vpcId)
            .build());

        var example = new Subnet("example", SubnetArgs.builder()
            .vpcId(selected.id())
            .availabilityZone("us-west-2a")
            .cidrBlock(StdFunctions.cidrsubnet(CidrsubnetArgs.builder()
                .input(selected.cidrBlock())
                .newbits(4)
                .netnum(1)
                .build()).result())
            .build());

    }
}
Copy
configuration:
  vpcId:
    type: dynamic
resources:
  example:
    type: aws:ec2:Subnet
    properties:
      vpcId: ${selected.id}
      availabilityZone: us-west-2a
      cidrBlock:
        fn::invoke:
          function: std:cidrsubnet
          arguments:
            input: ${selected.cidrBlock}
            newbits: 4
            netnum: 1
          return: result
variables:
  selected:
    fn::invoke:
      function: aws:ec2:getVpc
      arguments:
        id: ${vpcId}
Copy

Using getVpc

Two invocation forms are available. The direct form accepts plain arguments and either blocks until the result value is available, or returns a Promise-wrapped result. The output form accepts Input-wrapped arguments and returns an Output-wrapped result.

function getVpc(args: GetVpcArgs, opts?: InvokeOptions): Promise<GetVpcResult>
function getVpcOutput(args: GetVpcOutputArgs, opts?: InvokeOptions): Output<GetVpcResult>
Copy
def get_vpc(cidr_block: Optional[str] = None,
            default: Optional[bool] = None,
            dhcp_options_id: Optional[str] = None,
            filters: Optional[Sequence[GetVpcFilter]] = None,
            id: Optional[str] = None,
            state: Optional[str] = None,
            tags: Optional[Mapping[str, str]] = None,
            opts: Optional[InvokeOptions] = None) -> GetVpcResult
def get_vpc_output(cidr_block: Optional[pulumi.Input[str]] = None,
            default: Optional[pulumi.Input[bool]] = None,
            dhcp_options_id: Optional[pulumi.Input[str]] = None,
            filters: Optional[pulumi.Input[Sequence[pulumi.Input[GetVpcFilterArgs]]]] = None,
            id: Optional[pulumi.Input[str]] = None,
            state: Optional[pulumi.Input[str]] = None,
            tags: Optional[pulumi.Input[Mapping[str, pulumi.Input[str]]]] = None,
            opts: Optional[InvokeOptions] = None) -> Output[GetVpcResult]
Copy
func LookupVpc(ctx *Context, args *LookupVpcArgs, opts ...InvokeOption) (*LookupVpcResult, error)
func LookupVpcOutput(ctx *Context, args *LookupVpcOutputArgs, opts ...InvokeOption) LookupVpcResultOutput
Copy

> Note: This function is named LookupVpc in the Go SDK.

public static class GetVpc 
{
    public static Task<GetVpcResult> InvokeAsync(GetVpcArgs args, InvokeOptions? opts = null)
    public static Output<GetVpcResult> Invoke(GetVpcInvokeArgs args, InvokeOptions? opts = null)
}
Copy
public static CompletableFuture<GetVpcResult> getVpc(GetVpcArgs args, InvokeOptions options)
public static Output<GetVpcResult> getVpc(GetVpcArgs args, InvokeOptions options)
Copy
fn::invoke:
  function: aws:ec2/getVpc:getVpc
  arguments:
    # arguments dictionary
Copy

The following arguments are supported:

CidrBlock string
Cidr block of the desired VPC.
Default bool
Boolean constraint on whether the desired VPC is the default VPC for the region.
DhcpOptionsId string
DHCP options id of the desired VPC.
Filters List<GetVpcFilter>
Custom filter block as described below.
Id string
ID of the specific VPC to retrieve.
State string
Current state of the desired VPC. Can be either "pending" or "available".
Tags Dictionary<string, string>

Map of tags, each pair of which must exactly match a pair on the desired VPC.

More complex filters can be expressed using one or more filter sub-blocks, which take the following arguments:

CidrBlock string
Cidr block of the desired VPC.
Default bool
Boolean constraint on whether the desired VPC is the default VPC for the region.
DhcpOptionsId string
DHCP options id of the desired VPC.
Filters []GetVpcFilter
Custom filter block as described below.
Id string
ID of the specific VPC to retrieve.
State string
Current state of the desired VPC. Can be either "pending" or "available".
Tags map[string]string

Map of tags, each pair of which must exactly match a pair on the desired VPC.

More complex filters can be expressed using one or more filter sub-blocks, which take the following arguments:

cidrBlock String
Cidr block of the desired VPC.
default_ Boolean
Boolean constraint on whether the desired VPC is the default VPC for the region.
dhcpOptionsId String
DHCP options id of the desired VPC.
filters List<GetVpcFilter>
Custom filter block as described below.
id String
ID of the specific VPC to retrieve.
state String
Current state of the desired VPC. Can be either "pending" or "available".
tags Map<String,String>

Map of tags, each pair of which must exactly match a pair on the desired VPC.

More complex filters can be expressed using one or more filter sub-blocks, which take the following arguments:

cidrBlock string
Cidr block of the desired VPC.
default boolean
Boolean constraint on whether the desired VPC is the default VPC for the region.
dhcpOptionsId string
DHCP options id of the desired VPC.
filters GetVpcFilter[]
Custom filter block as described below.
id string
ID of the specific VPC to retrieve.
state string
Current state of the desired VPC. Can be either "pending" or "available".
tags {[key: string]: string}

Map of tags, each pair of which must exactly match a pair on the desired VPC.

More complex filters can be expressed using one or more filter sub-blocks, which take the following arguments:

cidr_block str
Cidr block of the desired VPC.
default bool
Boolean constraint on whether the desired VPC is the default VPC for the region.
dhcp_options_id str
DHCP options id of the desired VPC.
filters Sequence[GetVpcFilter]
Custom filter block as described below.
id str
ID of the specific VPC to retrieve.
state str
Current state of the desired VPC. Can be either "pending" or "available".
tags Mapping[str, str]

Map of tags, each pair of which must exactly match a pair on the desired VPC.

More complex filters can be expressed using one or more filter sub-blocks, which take the following arguments:

cidrBlock String
Cidr block of the desired VPC.
default Boolean
Boolean constraint on whether the desired VPC is the default VPC for the region.
dhcpOptionsId String
DHCP options id of the desired VPC.
filters List<Property Map>
Custom filter block as described below.
id String
ID of the specific VPC to retrieve.
state String
Current state of the desired VPC. Can be either "pending" or "available".
tags Map<String>

Map of tags, each pair of which must exactly match a pair on the desired VPC.

More complex filters can be expressed using one or more filter sub-blocks, which take the following arguments:

getVpc Result

The following output properties are available:

Arn string
ARN of VPC
CidrBlock string
CIDR block for the association.
CidrBlockAssociations List<GetVpcCidrBlockAssociation>
Default bool
DhcpOptionsId string
EnableDnsHostnames bool
Whether or not the VPC has DNS hostname support
EnableDnsSupport bool
Whether or not the VPC has DNS support
EnableNetworkAddressUsageMetrics bool
Whether Network Address Usage metrics are enabled for your VPC
Id string
InstanceTenancy string
Allowed tenancy of instances launched into the selected VPC. May be any of "default", "dedicated", or "host".
Ipv6AssociationId string
Association ID for the IPv6 CIDR block.
Ipv6CidrBlock string
IPv6 CIDR block.
MainRouteTableId string
ID of the main route table associated with this VPC.
OwnerId string
ID of the AWS account that owns the VPC.
State string
State of the association.
Tags Dictionary<string, string>
Filters List<GetVpcFilter>
Arn string
ARN of VPC
CidrBlock string
CIDR block for the association.
CidrBlockAssociations []GetVpcCidrBlockAssociation
Default bool
DhcpOptionsId string
EnableDnsHostnames bool
Whether or not the VPC has DNS hostname support
EnableDnsSupport bool
Whether or not the VPC has DNS support
EnableNetworkAddressUsageMetrics bool
Whether Network Address Usage metrics are enabled for your VPC
Id string
InstanceTenancy string
Allowed tenancy of instances launched into the selected VPC. May be any of "default", "dedicated", or "host".
Ipv6AssociationId string
Association ID for the IPv6 CIDR block.
Ipv6CidrBlock string
IPv6 CIDR block.
MainRouteTableId string
ID of the main route table associated with this VPC.
OwnerId string
ID of the AWS account that owns the VPC.
State string
State of the association.
Tags map[string]string
Filters []GetVpcFilter
arn String
ARN of VPC
cidrBlock String
CIDR block for the association.
cidrBlockAssociations List<GetVpcCidrBlockAssociation>
default_ Boolean
dhcpOptionsId String
enableDnsHostnames Boolean
Whether or not the VPC has DNS hostname support
enableDnsSupport Boolean
Whether or not the VPC has DNS support
enableNetworkAddressUsageMetrics Boolean
Whether Network Address Usage metrics are enabled for your VPC
id String
instanceTenancy String
Allowed tenancy of instances launched into the selected VPC. May be any of "default", "dedicated", or "host".
ipv6AssociationId String
Association ID for the IPv6 CIDR block.
ipv6CidrBlock String
IPv6 CIDR block.
mainRouteTableId String
ID of the main route table associated with this VPC.
ownerId String
ID of the AWS account that owns the VPC.
state String
State of the association.
tags Map<String,String>
filters List<GetVpcFilter>
arn string
ARN of VPC
cidrBlock string
CIDR block for the association.
cidrBlockAssociations GetVpcCidrBlockAssociation[]
default boolean
dhcpOptionsId string
enableDnsHostnames boolean
Whether or not the VPC has DNS hostname support
enableDnsSupport boolean
Whether or not the VPC has DNS support
enableNetworkAddressUsageMetrics boolean
Whether Network Address Usage metrics are enabled for your VPC
id string
instanceTenancy string
Allowed tenancy of instances launched into the selected VPC. May be any of "default", "dedicated", or "host".
ipv6AssociationId string
Association ID for the IPv6 CIDR block.
ipv6CidrBlock string
IPv6 CIDR block.
mainRouteTableId string
ID of the main route table associated with this VPC.
ownerId string
ID of the AWS account that owns the VPC.
state string
State of the association.
tags {[key: string]: string}
filters GetVpcFilter[]
arn str
ARN of VPC
cidr_block str
CIDR block for the association.
cidr_block_associations Sequence[GetVpcCidrBlockAssociation]
default bool
dhcp_options_id str
enable_dns_hostnames bool
Whether or not the VPC has DNS hostname support
enable_dns_support bool
Whether or not the VPC has DNS support
enable_network_address_usage_metrics bool
Whether Network Address Usage metrics are enabled for your VPC
id str
instance_tenancy str
Allowed tenancy of instances launched into the selected VPC. May be any of "default", "dedicated", or "host".
ipv6_association_id str
Association ID for the IPv6 CIDR block.
ipv6_cidr_block str
IPv6 CIDR block.
main_route_table_id str
ID of the main route table associated with this VPC.
owner_id str
ID of the AWS account that owns the VPC.
state str
State of the association.
tags Mapping[str, str]
filters Sequence[GetVpcFilter]
arn String
ARN of VPC
cidrBlock String
CIDR block for the association.
cidrBlockAssociations List<Property Map>
default Boolean
dhcpOptionsId String
enableDnsHostnames Boolean
Whether or not the VPC has DNS hostname support
enableDnsSupport Boolean
Whether or not the VPC has DNS support
enableNetworkAddressUsageMetrics Boolean
Whether Network Address Usage metrics are enabled for your VPC
id String
instanceTenancy String
Allowed tenancy of instances launched into the selected VPC. May be any of "default", "dedicated", or "host".
ipv6AssociationId String
Association ID for the IPv6 CIDR block.
ipv6CidrBlock String
IPv6 CIDR block.
mainRouteTableId String
ID of the main route table associated with this VPC.
ownerId String
ID of the AWS account that owns the VPC.
state String
State of the association.
tags Map<String>
filters List<Property Map>

Supporting Types

GetVpcCidrBlockAssociation

AssociationId This property is required. string
Association ID for the IPv4 CIDR block.
CidrBlock This property is required. string
Cidr block of the desired VPC.
State This property is required. string
Current state of the desired VPC. Can be either "pending" or "available".
AssociationId This property is required. string
Association ID for the IPv4 CIDR block.
CidrBlock This property is required. string
Cidr block of the desired VPC.
State This property is required. string
Current state of the desired VPC. Can be either "pending" or "available".
associationId This property is required. String
Association ID for the IPv4 CIDR block.
cidrBlock This property is required. String
Cidr block of the desired VPC.
state This property is required. String
Current state of the desired VPC. Can be either "pending" or "available".
associationId This property is required. string
Association ID for the IPv4 CIDR block.
cidrBlock This property is required. string
Cidr block of the desired VPC.
state This property is required. string
Current state of the desired VPC. Can be either "pending" or "available".
association_id This property is required. str
Association ID for the IPv4 CIDR block.
cidr_block This property is required. str
Cidr block of the desired VPC.
state This property is required. str
Current state of the desired VPC. Can be either "pending" or "available".
associationId This property is required. String
Association ID for the IPv4 CIDR block.
cidrBlock This property is required. String
Cidr block of the desired VPC.
state This property is required. String
Current state of the desired VPC. Can be either "pending" or "available".

GetVpcFilter

Name This property is required. string
Name of the field to filter by, as defined by the underlying AWS API.
Values This property is required. List<string>
Set of values that are accepted for the given field. A VPC will be selected if any one of the given values matches.
Name This property is required. string
Name of the field to filter by, as defined by the underlying AWS API.
Values This property is required. []string
Set of values that are accepted for the given field. A VPC will be selected if any one of the given values matches.
name This property is required. String
Name of the field to filter by, as defined by the underlying AWS API.
values This property is required. List<String>
Set of values that are accepted for the given field. A VPC will be selected if any one of the given values matches.
name This property is required. string
Name of the field to filter by, as defined by the underlying AWS API.
values This property is required. string[]
Set of values that are accepted for the given field. A VPC will be selected if any one of the given values matches.
name This property is required. str
Name of the field to filter by, as defined by the underlying AWS API.
values This property is required. Sequence[str]
Set of values that are accepted for the given field. A VPC will be selected if any one of the given values matches.
name This property is required. String
Name of the field to filter by, as defined by the underlying AWS API.
values This property is required. List<String>
Set of values that are accepted for the given field. A VPC will be selected if any one of the given values matches.

Package Details

Repository
AWS Classic pulumi/pulumi-aws
License
Apache-2.0
Notes
This Pulumi package is based on the aws Terraform Provider.
AWS v6.77.0 published on Wednesday, Apr 9, 2025 by Pulumi