-
Notifications
You must be signed in to change notification settings - Fork 0
Add VPC component with configurable subnets and optional NAT gateways #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1cb9050
Initial plan
Copilot 5b77b11
Add VPC component with unit tests and staging instantiation
Copilot f2d012e
Merge branch 'main' of https://github.com/ManagedKube/devops-with-ai …
8e920a3
updating var
a011a63
Change availability zones from us-west-2 to us-east-1
Copilot 06681c6
Add support for flexible subnet count (2-3+ subnets)
Copilot 391b74f
Fix subnet indexing bug causing duplicate CIDR blocks and AZs
Copilot 378996a
Fix lambda closure issue with default parameters for subnet indexing
Copilot 9e4f73f
setting to destroy
92b76f6
setting back to preview
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| runtime: python |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| # VPC Component | ||
|
|
||
| A reusable Pulumi component for creating AWS VPC infrastructure with public and private subnets. | ||
|
|
||
| ## Features | ||
|
|
||
| - **VPC**: Creates a VPC with configurable CIDR block and DNS support | ||
| - **Public Subnets**: Configurable number of public subnets (2-3 typical) across availability zones with auto-assign public IP | ||
| - **Private Subnets**: Configurable number of private subnets (2-3 typical) across availability zones | ||
| - **Internet Gateway**: For public subnet internet connectivity | ||
| - **Route Tables**: Properly configured route tables for public and private subnets | ||
| - **Default Security Group**: Allows all inbound and outbound traffic (configurable) | ||
| - **Optional NAT Gateways**: One NAT Gateway per availability zone for private subnet internet access | ||
| - **Flexible Subnet Count**: Supports 2 or 3 subnets (or more) - simply provide the desired number of CIDR blocks | ||
|
|
||
| ## Usage | ||
|
|
||
| ### In Pulumi YAML | ||
|
|
||
| ```yaml | ||
| name: my-vpc | ||
| runtime: yaml | ||
| packages: | ||
| vpc: https://github.com/ManagedKube/devops-with-ai.git/pulumi/components/aws/vpc@0.0.1 | ||
|
|
||
| resources: | ||
| my-vpc: | ||
| type: vpc:index:Vpc | ||
| properties: | ||
| vpcCidr: "10.0.0.0/16" | ||
| publicSubnetCidrs: | ||
| - "10.0.1.0/24" | ||
| - "10.0.2.0/24" | ||
| - "10.0.3.0/24" | ||
| privateSubnetCidrs: | ||
| - "10.0.11.0/24" | ||
| - "10.0.12.0/24" | ||
| - "10.0.13.0/24" | ||
| availabilityZones: | ||
| - "us-west-2a" | ||
| - "us-west-2b" | ||
| - "us-west-2c" | ||
| enableNatGateway: true | ||
| vpcName: "my-application-vpc" | ||
| tagsAdditional: | ||
| Environment: "production" | ||
| ManagedBy: "pulumi" | ||
| ``` | ||
|
|
||
| ### In Python | ||
|
|
||
| ```python | ||
| from vpc import Vpc | ||
|
|
||
| vpc = Vpc( | ||
| "my-vpc", | ||
| { | ||
| "vpcCidr": "10.0.0.0/16", | ||
| "publicSubnetCidrs": [ | ||
| "10.0.1.0/24", | ||
| "10.0.2.0/24", | ||
| "10.0.3.0/24", | ||
| ], | ||
| "privateSubnetCidrs": [ | ||
| "10.0.11.0/24", | ||
| "10.0.12.0/24", | ||
| "10.0.13.0/24", | ||
| ], | ||
| "availabilityZones": [ | ||
| "us-west-2a", | ||
| "us-west-2b", | ||
| "us-west-2c", | ||
| ], | ||
| "enableNatGateway": True, | ||
| "vpcName": "my-application-vpc", | ||
| "tagsAdditional": { | ||
| "Environment": "production", | ||
| "ManagedBy": "pulumi", | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| # Access outputs | ||
| vpc_id = vpc.vpc_id | ||
| public_subnet_ids = vpc.public_subnet_ids | ||
| private_subnet_ids = vpc.private_subnet_ids | ||
| ``` | ||
|
|
||
| ### Example with 2 Subnets | ||
|
|
||
| You can create a VPC with just 2 subnets by providing 2 CIDR blocks: | ||
|
|
||
| ```yaml | ||
| resources: | ||
| my-vpc-two-subnets: | ||
| type: vpc:index:Vpc | ||
| properties: | ||
| vpcCidr: "10.0.0.0/16" | ||
| publicSubnetCidrs: | ||
| - "10.0.1.0/24" | ||
| - "10.0.2.0/24" | ||
| privateSubnetCidrs: | ||
| - "10.0.11.0/24" | ||
| - "10.0.12.0/24" | ||
| availabilityZones: | ||
| - "us-east-1a" | ||
| - "us-east-1b" | ||
| enableNatGateway: false | ||
| vpcName: "my-two-subnet-vpc" | ||
| tagsAdditional: | ||
| Environment: "development" | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| ### Required Parameters | ||
|
|
||
| - **vpcCidr** (string): CIDR block for the VPC (e.g., "10.0.0.0/16") | ||
| - **publicSubnetCidrs** (list[string]): List of CIDR blocks for public subnets (e.g., 2-3 subnets) | ||
| - **privateSubnetCidrs** (list[string]): List of CIDR blocks for private subnets (e.g., 2-3 subnets) | ||
| - **availabilityZones** (list[string]): List of availability zones to use (must match the number of subnets) | ||
| - **tagsAdditional** (dict): Additional tags to apply to all resources | ||
|
|
||
| **Note**: The number of subnets is determined by the length of the `publicSubnetCidrs` list. Provide 2 CIDR blocks for 2 subnets, 3 for 3 subnets, etc. | ||
|
|
||
| ### Optional Parameters | ||
|
|
||
| - **enableNatGateway** (boolean): Whether to create NAT Gateways in each private subnet. Default: `false` | ||
| - **vpcName** (string): Name for the VPC. If not provided, uses the resource name | ||
|
|
||
| ## Outputs | ||
|
|
||
| - **vpc_id**: The ID of the VPC | ||
| - **vpc_arn**: The ARN of the VPC | ||
| - **public_subnet_ids**: List of public subnet IDs | ||
| - **private_subnet_ids**: List of private subnet IDs | ||
| - **internet_gateway_id**: The ID of the Internet Gateway | ||
| - **nat_gateway_ids**: List of NAT Gateway IDs (empty if NAT Gateways are disabled) | ||
| - **default_security_group_id**: The ID of the default security group | ||
|
|
||
| ## Architecture | ||
|
|
||
| ### Without NAT Gateways (enableNatGateway: false) | ||
|
|
||
| ``` | ||
| ┌─────────────────────────────────────────────────────────────┐ | ||
| │ VPC │ | ||
| │ │ | ||
| │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ | ||
| │ │ Public │ │ Public │ │ Public │ │ | ||
| │ │ Subnet AZ-1 │ │ Subnet AZ-2 │ │ Subnet AZ-3 │ │ | ||
| │ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │ | ||
| │ │ │ │ │ | ||
| │ └──────────────────┴──────────────────┘ │ | ||
| │ │ │ | ||
| │ ┌───────▼────────┐ │ | ||
| │ │ Internet │ │ | ||
| │ │ Gateway │ │ | ||
| │ └────────────────┘ │ | ||
| │ │ | ||
| │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ | ||
| │ │ Private │ │ Private │ │ Private │ │ | ||
| │ │ Subnet AZ-1 │ │ Subnet AZ-2 │ │ Subnet AZ-3 │ │ | ||
| │ └──────────────┘ └──────────────┘ └──────────────┘ │ | ||
| │ │ | ||
| └─────────────────────────────────────────────────────────────┘ | ||
| ``` | ||
|
|
||
| ### With NAT Gateways (enableNatGateway: true) | ||
|
|
||
| ``` | ||
| ┌─────────────────────────────────────────────────────────────┐ | ||
| │ VPC │ | ||
| │ │ | ||
| │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ | ||
| │ │ Public │ │ Public │ │ Public │ │ | ||
| │ │ Subnet AZ-1 │ │ Subnet AZ-2 │ │ Subnet AZ-3 │ │ | ||
| │ │ ┌──────────┐ │ │ ┌──────────┐ │ │ ┌──────────┐ │ │ | ||
| │ │ │ NAT GW │ │ │ │ NAT GW │ │ │ │ NAT GW │ │ │ | ||
| │ │ └────┬─────┘ │ │ └────┬─────┘ │ │ └────┬─────┘ │ │ | ||
| │ └──────┼───────┘ └──────┼───────┘ └──────┼───────┘ │ | ||
| │ │ │ │ │ | ||
| │ └──────────────────┴──────────────────┘ │ | ||
| │ │ │ | ||
| │ ┌───────▼────────┐ │ | ||
| │ │ Internet │ │ | ||
| │ │ Gateway │ │ | ||
| │ └────────────────┘ │ | ||
| │ │ | ||
| │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ | ||
| │ │ Private │ │ Private │ │ Private │ │ | ||
| │ │ Subnet AZ-1 │ │ Subnet AZ-2 │ │ Subnet AZ-3 │ │ | ||
| │ │ ▲ │ │ ▲ │ │ ▲ │ │ | ||
| │ │ │ │ │ │ │ │ │ │ │ | ||
| │ └──────┼───────┘ └──────┼───────┘ └──────┼───────┘ │ | ||
| │ │ │ │ │ | ||
| │ └──────────────────┴──────────────────┘ │ | ||
| │ Routes to respective NAT Gateway │ | ||
| └─────────────────────────────────────────────────────────────┘ | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| To run the unit tests: | ||
|
|
||
| ```bash | ||
| cd pulumi/components/aws/vpc | ||
| python3 -m venv venv | ||
| source venv/bin/activate | ||
| pip install -r requirements-test.txt | ||
| python -m pytest tests/ -v | ||
| ``` | ||
|
|
||
| See [tests/README.md](tests/README.md) for more information on testing. | ||
|
|
||
| ## Notes | ||
|
|
||
| - The default security group allows all inbound and outbound traffic. This is suitable for development but should be restricted for production use. | ||
| - NAT Gateways incur additional costs. Only enable them if private subnets need internet access. | ||
| - The component creates one NAT Gateway per availability zone when enabled, providing high availability but at higher cost. | ||
| - All resources are tagged with the provided `tagsAdditional` for easy identification and cost tracking. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from pulumi.provider.experimental import component_provider_host | ||
| from vpc import Vpc | ||
|
|
||
| if __name__ == "__main__": | ||
| component_provider_host(name="vpc", components=[Vpc]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| pulumi>=3.130.0 | ||
| pulumi-aws>=6.50.1 | ||
| pytest>=7.4.0 | ||
| pytest-asyncio>=0.21.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pulumi>=3.130.0 | ||
| pulumi-aws>=6.50.1 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The component’s "default security group" is documented as allowing all inbound and outbound traffic, which in the implementation corresponds to an
ec2.SecurityGroupwith all protocols/ports open to0.0.0.0/0. Any resource attached to this group will be fully exposed to the public internet, so an attacker can scan and reach any listening service. Consider making this security group least-privilege by default (no or minimal inbound rules) and/or exposing configuration to define restrictive ingress/egress rules, with an open SG only as an explicit opt-in for development.