Skip to content

Commit

Permalink
feat: look up CIDR block address range by VPC ID
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmclaurin committed Nov 30, 2022
1 parent fb2fbde commit bdef0be
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,7 @@ ensure the security group exists.

## `getVPCIdBySubnetId(options)`

Ensures that a security group with the specified name exists in the VPC, creating
it if needed.
Fetches and returns the ID of the VPC where the specified subnet is located.

### `options` object

Expand All @@ -370,3 +369,31 @@ AWS region. Either `ec2` or `region` must be provided.
### Returns

A `Promise` that resolves to a `string` with the ID of the VPC

## `getCIDRByVPCId(options)`

Fetches and returns the CIDR block IP address range of the specified VPC.

### `options` object

#### `awsConfig` (`ConfigurationOptions`, _optional_)

General AWS service configuration options like `region` and `credentials`

#### `vpcId` (`string`, **required**)

ID of the VPC

#### `ec2` (`AWS.EC2`, **conditional**)

Optional EC2 class instance to use for API calls. If no EC2 class instance
is provided, one will be created using the `region` property. Either
`ec2` or `region` must be provided.

#### `region` (`string`, **conditional**)

AWS region. Either `ec2` or `region` must be provided.

### Returns

A `Promise` that resolves to a `string` with the IP address range of the VPC
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export { default as getStackResources } from './getStackResources'
export { default as printStackResources } from './printStackResources'
export { getSubnetInfo } from './subnet'
export { getSecurityGroupId, upsertSecurityGroup } from './securityGroups'
export { getVPCIdBySubnetId } from './vpc'
export { getVPCIdBySubnetId, getCIDRByVPCId } from './vpc'
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export { default as printStackResources } from './printStackResources'
export { upsertSecurityGroup, getSecurityGroupId } from './securityGroups'
export { getSubnetInfo } from './subnet'
export type { SubnetInfo } from './subnet'
export { getVPCIdBySubnetId } from './vpc'
export { getVPCIdBySubnetId, getCIDRByVPCId } from './vpc'
33 changes: 33 additions & 0 deletions src/vpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,36 @@ export async function getVPCIdBySubnetId({
}): Promise<string> {
return (await getSubnetInfo({ subnetId, ec2, awsConfig, region })).VpcId
}

export async function getCIDRByVPCId({
vpcId,
ec2,
region,
awsConfig,
}: {
vpcId: string,
ec2?: ?AWS.EC2,
region?: ?string,
awsConfig?: ?{ ... },
}): Promise<string> {
if (!vpcId) throw Error('vpcId is required')
if (!awsConfig) awsConfig = { ...(region ? { region } : {}) }
if (!ec2) ec2 = new AWS.EC2(awsConfig)
const { Vpcs } = await ec2
.describeVpcs({
VpcIds: [vpcId],
})
.promise()
if (!Vpcs) throw Error('missing Vpcs in result')
const [vpc] = Vpcs
if (!vpc)
throw Error(
`could not look up CIDR for VPC ${vpcId}: missing VPC in result`
)
const { CidrBlock } = vpc
if (!CidrBlock)
throw Error(
`could not look up CIDR for VPC ${vpcId}: missing CidrBlock on VPC object`
)
return CidrBlock
}
7 changes: 7 additions & 0 deletions vpc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ export function getVPCIdBySubnetId(options: {
region?: string | null | undefined
awsConfig?: AWS.ConfigurationOptions | null
}): Promise<AWS.EC2.VpcId>

export function getCIDRByVPCId(options: {
vpcId: AWS.EC2.VpcId
ec2?: AWS.EC2 | null | undefined
region?: string | null | undefined
awsConfig?: AWS.ConfigurationOptions | null
}): Promise<string>

0 comments on commit bdef0be

Please sign in to comment.