Skip to content

Commit 8bcbc8e

Browse files
shanewxythxCode
authored andcommitted
feat: support creating vpc if not provided
1 parent 63da192 commit 8bcbc8e

File tree

6 files changed

+48
-56
lines changed

6 files changed

+48
-56
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ No modules.
6363
| [aws_service_discovery_instance.reader](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/service_discovery_instance) | resource |
6464
| [aws_service_discovery_service.primary](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/service_discovery_service) | resource |
6565
| [aws_service_discovery_service.reader](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/service_discovery_service) | resource |
66+
| [aws_subnet.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource |
67+
| [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc) | resource |
6668
| [random_password.password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |
6769
| [random_string.name_suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
70+
| [aws_availability_zones.selected](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
6871
| [aws_kms_key.selected](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/kms_key) | data source |
6972
| [aws_service_discovery_dns_namespace.selected](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/service_discovery_dns_namespace) | data source |
7073
| [aws_subnets.selected](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
@@ -78,7 +81,7 @@ No modules.
7881
| <a name="input_context"></a> [context](#input\_context) | Receive contextual information. When Walrus deploys, Walrus will inject specific contextual information into this field.<br><br>Examples:<pre>context:<br> project:<br> name: string<br> id: string<br> environment:<br> name: string<br> id: string<br> resource:<br> name: string<br> id: string</pre> | `map(any)` | `{}` | no |
7982
| <a name="input_engine_parameters"></a> [engine\_parameters](#input\_engine\_parameters) | Specify the deployment parameters, see https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/ParameterGroups.Redis.html. | <pre>list(object({<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
8083
| <a name="input_engine_version"></a> [engine\_version](#input\_engine\_version) | Specify the deployment engine version. | `string` | `"7.0"` | no |
81-
| <a name="input_infrastructure"></a> [infrastructure](#input\_infrastructure) | Specify the infrastructure information for deploying.<br><br>Examples:<pre>infrastructure:<br> vpc_id: string # the ID of the VPC where the Redis service applies<br> kms_key_id: string, optional # the ID of the KMS key which to encrypt the Redis data<br> domain_suffix: string, optional # a private DNS namespace of the CloudMap where to register the applied Redis service</pre> | <pre>object({<br> vpc_id = string<br> kms_key_id = optional(string)<br> domain_suffix = optional(string)<br> })</pre> | n/a | yes |
84+
| <a name="input_infrastructure"></a> [infrastructure](#input\_infrastructure) | Specify the infrastructure information for deploying.<br><br>Examples:<pre>infrastructure:<br> vpc_id: string, optional # the ID of the VPC where the Redis service applies<br> kms_key_id: string, optional # the ID of the KMS key which to encrypt the Redis data<br> domain_suffix: string, optional # a private DNS namespace of the CloudMap where to register the applied Redis service</pre> | <pre>object({<br> vpc_id = optional(string)<br> kms_key_id = optional(string)<br> domain_suffix = optional(string)<br> })</pre> | `{}` | no |
8285
| <a name="input_password"></a> [password](#input\_password) | Specify the account password. The password must be 16-32 characters long and start with any letter, number, or the following symbols: ! # $ % ^ & * ( ) \_ + - =.<br>If not specified, it will generate a random password. | `string` | `null` | no |
8386
| <a name="input_replication_readonly_replicas"></a> [replication\_readonly\_replicas](#input\_replication\_readonly\_replicas) | Specify the number of read-only replicas under the replication deployment. | `number` | `1` | no |
8487
| <a name="input_resources"></a> [resources](#input\_resources) | Specify the computing resources.<br>Examples:<pre>resources:<br> class: string, optional # https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/CacheNodes.SupportedTypes.html</pre> | <pre>object({<br> class = optional(string, "cache.t3.micro")<br> })</pre> | <pre>{<br> "class": "cache.t3.micro"<br>}</pre> | no |

examples/replication/README.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
Deploy Redis service in replication architecture by root module.
44

55
```bash
6-
# setup infra
7-
$ terraform apply -auto-approve \
8-
-target=aws_vpc.example \
9-
-target=aws_subnet.example
10-
116
# create service
127
$ terraform apply -auto-approve
138
```
@@ -23,9 +18,7 @@ $ terraform apply -auto-approve
2318

2419
## Providers
2520

26-
| Name | Version |
27-
|------|---------|
28-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.24.0 |
21+
No providers.
2922

3023
## Modules
3124

@@ -35,11 +28,7 @@ $ terraform apply -auto-approve
3528

3629
## Resources
3730

38-
| Name | Type |
39-
|------|------|
40-
| [aws_subnet.example](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource |
41-
| [aws_vpc.example](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc) | resource |
42-
| [aws_availability_zones.selected](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
31+
No resources.
4332

4433
## Inputs
4534

examples/replication/main.tf

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,11 @@ terraform {
1515

1616
provider "aws" {}
1717

18-
data "aws_availability_zones" "selected" {
19-
state = "available"
20-
21-
lifecycle {
22-
postcondition {
23-
condition = length(self.names) > 0
24-
error_message = "Failed to get Avaialbe Zones"
25-
}
26-
}
27-
}
28-
29-
# create vpc.
30-
31-
resource "aws_vpc" "example" {
32-
instance_tenancy = "default"
33-
enable_dns_hostnames = true
34-
enable_dns_support = true
35-
cidr_block = "10.0.0.0/16"
36-
}
37-
38-
resource "aws_subnet" "example" {
39-
for_each = {
40-
for k, v in data.aws_availability_zones.selected.names : v => cidrsubnet(aws_vpc.example.cidr_block, 8, k)
41-
}
42-
43-
vpc_id = aws_vpc.example.id
44-
availability_zone = each.key
45-
cidr_block = each.value
46-
}
47-
4818
# create redis service.
4919

5020
module "this" {
5121
source = "../.."
5222

53-
infrastructure = {
54-
vpc_id = aws_vpc.example.id
55-
}
56-
5723
architecture = "replication"
5824
replication_readonly_replicas = 3
5925
}

main.tf

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,47 @@ locals {
2323
architecture = coalesce(var.architecture, "standalone")
2424
}
2525

26+
# create vpc.
27+
28+
resource "aws_vpc" "default" {
29+
count = var.infrastructure.vpc_id == null ? 1 : 0
30+
31+
instance_tenancy = "default"
32+
enable_dns_hostnames = true
33+
enable_dns_support = true
34+
cidr_block = "10.0.0.0/16"
35+
}
36+
37+
38+
data "aws_availability_zones" "selected" {
39+
count = var.infrastructure.vpc_id == null ? 1 : 0
40+
41+
state = "available"
42+
43+
lifecycle {
44+
postcondition {
45+
condition = length(self.names) > 0
46+
error_message = "Failed to get Avaialbe Zones"
47+
}
48+
}
49+
}
50+
51+
resource "aws_subnet" "default" {
52+
for_each = var.infrastructure.vpc_id == null ? {
53+
for k, v in data.aws_availability_zones.selected[0].names : v => cidrsubnet(aws_vpc.default[0].cidr_block, 8, k)
54+
} : {}
55+
56+
vpc_id = aws_vpc.default[0].id
57+
availability_zone = each.key
58+
cidr_block = each.value
59+
}
60+
2661
#
2762
# Ensure
2863
#
2964

3065
data "aws_vpc" "selected" {
31-
id = var.infrastructure.vpc_id
66+
id = var.infrastructure.vpc_id != null ? var.infrastructure.vpc_id : aws_vpc.default[0].id
3267

3368
state = "available"
3469

schema.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ components:
55
schemas:
66
variables:
77
type: object
8-
required:
9-
- infrastructure
108
properties:
119
infrastructure:
1210
title: Infrastructure
1311
type: object
1412
description: |
1513
Specify the infrastructure information for deploying.
16-
required:
17-
- vpc_id
14+
default: {}
1815
properties:
1916
domain_suffix:
2017
description: |
@@ -31,7 +28,8 @@ components:
3128
x-walrus-ui:
3229
order: 2
3330
vpc_id:
34-
description: "Specify the VPC ID where the Redis service applies. \n"
31+
description: Specify the VPC ID where the Redis service applies. If
32+
not specified, a new VPC will be created.
3533
title: Vpc Id
3634
type: string
3735
x-walrus-ui:

variables.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ Specify the infrastructure information for deploying.
3535
Examples:
3636
```
3737
infrastructure:
38-
vpc_id: string # the ID of the VPC where the Redis service applies
38+
vpc_id: string, optional # the ID of the VPC where the Redis service applies
3939
kms_key_id: string, optional # the ID of the KMS key which to encrypt the Redis data
4040
domain_suffix: string, optional # a private DNS namespace of the CloudMap where to register the applied Redis service
4141
```
4242
EOF
4343
type = object({
44-
vpc_id = string
44+
vpc_id = optional(string)
4545
kms_key_id = optional(string)
4646
domain_suffix = optional(string)
4747
})
48+
default = {}
4849
}
4950

5051
#

0 commit comments

Comments
 (0)