Skip to content

Commit 070d831

Browse files
committed
feat: Add support to create VPCE security group
1 parent 3f7a337 commit 070d831

File tree

8 files changed

+132
-4
lines changed

8 files changed

+132
-4
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Dynamic Terraform module, which creates a Opensearch Serverless Collection and r
1515
+ [Encryption Policy](#encryption-policy)
1616
+ [Network Policy](#network-policy)
1717
- [VPC Access](#vpc-access)
18+
- [Security Group](#security-group)
1819
+ [Data Access Policy](#data-access-policy)
1920
+ [Data Lifecycle Policy](#data-lifecycle-policy)
2021
+ [Security Config](#security-config)
@@ -95,6 +96,33 @@ To change the network policy use variable `network_policy_type`. The supported v
9596
If the variable `network_policy_type` is different from "AllPublic", the module will create Opensearch Serverless Endpoint to private access.
9697
In this case it's necessary configure the following variables: `vpce_subnet_ids` and `vpce_vpc_id`. `vpce_security_group_ids` is optional.
9798

99+
##### Security Group
100+
101+
* To add existing security group, please use the variable `vpce_security_group_ids`.
102+
* By Default, module creates a new security group. To disable this put the variable `vpce_create_security_group = false`.
103+
* To choose the allowed sources for the created security group, you should use the variable `vpce_security_group_sources`.
104+
* This variable supports two fields:
105+
106+
| Field | Allowed Values |
107+
|---------|---------------------------------------------------------------------------------------------|
108+
| type | IPv4, IPv6, PrefixLists, SGs |
109+
| sources | List of sources to be allowed. For example: To type IPv4 should be list of IPv4 CIDR blocks |
110+
111+
* Example:
112+
113+
```hcl
114+
vpce_security_group_sources = [
115+
{
116+
type = "IPv4"
117+
sources = ["0.0.0.0/0"]
118+
},
119+
{
120+
type = "IPv6"
121+
sources = ["::/0"]
122+
}
123+
]
124+
```
125+
98126
### Data Access Policy
99127

100128
To configure data access policy use variable `access_policy_rules`. This variable is a list of data access rules.
@@ -175,6 +203,7 @@ No modules.
175203
| [aws_opensearchserverless_security_policy.encryption](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/opensearchserverless_security_policy) | resource |
176204
| [aws_opensearchserverless_security_policy.network](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/opensearchserverless_security_policy) | resource |
177205
| [aws_opensearchserverless_vpc_endpoint.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/opensearchserverless_vpc_endpoint) | resource |
206+
| [aws_security_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
178207

179208
## Inputs
180209

@@ -208,8 +237,12 @@ No modules.
208237
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to assign to the collection. If configured with a provider default\_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level. | `map(string)` | `{}` | no |
209238
| <a name="input_type"></a> [type](#input\_type) | Type of collection. One of SEARCH, TIMESERIES, or VECTORSEARCH. Defaults to TIMESERIES. | `string` | `"TIMESERIES"` | no |
210239
| <a name="input_use_standby_replicas"></a> [use\_standby\_replicas](#input\_use\_standby\_replicas) | Indicates whether standby replicas should be used for a collection. | `bool` | `true` | no |
240+
| <a name="input_vpce_create_security_group"></a> [vpce\_create\_security\_group](#input\_vpce\_create\_security\_group) | Creates a security group for VPCE. | `bool` | `true` | no |
211241
| <a name="input_vpce_name"></a> [vpce\_name](#input\_vpce\_name) | Name of the interface endpoint. | `string` | `null` | no |
242+
| <a name="input_vpce_security_group_description"></a> [vpce\_security\_group\_description](#input\_vpce\_security\_group\_description) | Security Group description for VPCE. | `string` | `null` | no |
212243
| <a name="input_vpce_security_group_ids"></a> [vpce\_security\_group\_ids](#input\_vpce\_security\_group\_ids) | One or more security groups that define the ports, protocols, and sources for inbound traffic that you are authorizing into your endpoint. Up to 5 security groups can be provided. | `list(string)` | `null` | no |
244+
| <a name="input_vpce_security_group_name"></a> [vpce\_security\_group\_name](#input\_vpce\_security\_group\_name) | Security Group name for VPCE. | `string` | `null` | no |
245+
| <a name="input_vpce_security_group_sources"></a> [vpce\_security\_group\_sources](#input\_vpce\_security\_group\_sources) | Sources for inbound traffic to Opensearch Serverless | <pre>list(object({<br> type = string<br> sources = list(string)<br> }))</pre> | `[]` | no |
213246
| <a name="input_vpce_subnet_ids"></a> [vpce\_subnet\_ids](#input\_vpce\_subnet\_ids) | One or more subnet IDs from which you'll access OpenSearch Serverless. Up to 6 subnets can be provided. | `list(string)` | `[]` | no |
214247
| <a name="input_vpce_vpc_id"></a> [vpce\_vpc\_id](#input\_vpce\_vpc\_id) | ID of the VPC from which you'll access OpenSearch Serverless. | `string` | `null` | no |
215248

@@ -232,6 +265,8 @@ No modules.
232265
| <a name="output_network_policy_version"></a> [network\_policy\_version](#output\_network\_policy\_version) | Version of the network policy. |
233266
| <a name="output_security_config_name"></a> [security\_config\_name](#output\_security\_config\_name) | Name of the security config. |
234267
| <a name="output_security_config_version"></a> [security\_config\_version](#output\_security\_config\_version) | Version of the security config. |
268+
| <a name="output_security_group_id"></a> [security\_group\_id](#output\_security\_group\_id) | Id of the security group. |
269+
| <a name="output_security_group_name"></a> [security\_group\_name](#output\_security\_group\_name) | Name of the security group. |
235270
| <a name="output_vpce_id"></a> [vpce\_id](#output\_vpce\_id) | Id of the vpce. |
236271
| <a name="output_vpce_name"></a> [vpce\_name](#output\_vpce\_name) | Name of the interface endpoint. |
237272
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/complete/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,7 @@ Note that this example may create resources which cost money. Run `terraform des
6363
| <a name="output_data_lifecycle_policy_version"></a> [data\_lifecycle\_policy\_version](#output\_data\_lifecycle\_policy\_version) | Data Lifecycle policy version. |
6464
| <a name="output_encrypt_policy_version"></a> [encrypt\_policy\_version](#output\_encrypt\_policy\_version) | Encryption policy version. |
6565
| <a name="output_network_policy_version"></a> [network\_policy\_version](#output\_network\_policy\_version) | Network policy version. |
66+
| <a name="output_security_config_version"></a> [security\_config\_version](#output\_security\_config\_version) | Security Config version. |
67+
| <a name="output_security_group_id"></a> [security\_group\_id](#output\_security\_group\_id) | Id of the security group. |
68+
| <a name="output_security_group_name"></a> [security\_group\_name](#output\_security\_group\_name) | Name of the security group. |
6669
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/complete/main.tf

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ module "opensearch_serverless" {
1515
network_policy_type = "PublicCollectionPrivateDashboard"
1616
vpce_vpc_id = module.vpc.vpc_id
1717
vpce_subnet_ids = [module.vpc.private_subnets[0]]
18+
vpce_security_group_sources = [
19+
{
20+
type = "IPv4"
21+
sources = ["0.0.0.0/0"]
22+
},
23+
{
24+
type = "IPv6"
25+
sources = ["::/0"]
26+
}
27+
]
1828
access_policy_rules = [
1929
{
2030
type = "collection"

examples/complete/outputs.tf

+14-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@ output "data_lifecycle_policy_version" {
3838
value = module.opensearch_serverless.data_lifecycle_policy_version
3939
}
4040

41-
#output "security_config_version" {
42-
# description = "Security Config version."
43-
# value = module.opensearch_serverless.security_config_version
44-
#}
41+
output "security_config_version" {
42+
description = "Security Config version."
43+
value = module.opensearch_serverless.security_config_version
44+
}
45+
46+
output "security_group_id" {
47+
description = "Id of the security group."
48+
value = module.opensearch_serverless.security_group_id
49+
}
50+
51+
output "security_group_name" {
52+
description = "Name of the security group."
53+
value = module.opensearch_serverless.security_group_name
54+
}

locals.tf

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
locals {
22
create_vpce = var.create_network_policy && var.network_policy_type != "AllPublic" ? true : false
3+
crate_sg = local.create_vpce && var.vpce_create_security_group
4+
sg_name = coalesce(var.vpce_security_group_name, "${var.name}-sg")
35
network_policy_vpces = local.create_vpce ? [aws_opensearchserverless_vpc_endpoint.this[0].id] : null
46
network_policies = {
57
AllPublic = [{

main.tf

+26
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,29 @@ resource "aws_opensearchserverless_security_config" "this" {
7272
session_timeout = var.saml_session_timeout
7373
}
7474
}
75+
76+
##################
77+
# Security Group
78+
##################
79+
resource "aws_security_group" "this" {
80+
count = local.crate_sg ? 1 : 0
81+
name = local.sg_name
82+
description = var.vpce_security_group_description
83+
vpc_id = var.vpce_vpc_id
84+
ingress {
85+
from_port = 443
86+
to_port = 443
87+
protocol = "tcp"
88+
cidr_blocks = flatten([for i, item in var.vpce_security_group_sources : [for k, source in item.sources : source] if item.type == "IPv4"])
89+
ipv6_cidr_blocks = flatten([for i, item in var.vpce_security_group_sources : [for k, source in item.sources : source] if item.type == "IPv6"])
90+
prefix_list_ids = flatten([for i, item in var.vpce_security_group_sources : [for k, source in item.sources : source] if item.type == "PrefixLists"])
91+
security_groups = flatten([for i, item in var.vpce_security_group_sources : [for k, source in item.sources : source] if item.type == "SGs"])
92+
description = "Allow Inbound HTTPS Traffic"
93+
}
94+
tags = merge(
95+
var.tags,
96+
{
97+
Name : local.sg_name
98+
}
99+
)
100+
}

outputs.tf

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ output "vpce_id" {
6464
value = local.create_vpce ? aws_opensearchserverless_vpc_endpoint.this[0].id : null
6565
}
6666

67+
#######
68+
## Security Group
69+
#######
70+
output "security_group_id" {
71+
description = "Id of the security group."
72+
value = local.crate_sg ? aws_security_group.this[0].id : null
73+
}
74+
75+
output "security_group_name" {
76+
description = "Name of the security group."
77+
value = local.crate_sg ? aws_security_group.this[0].name : null
78+
}
6779
#######
6880
## Data Access Policy
6981
#######

variables.tf

+30
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,36 @@ variable "vpce_security_group_ids" {
119119
default = null
120120
}
121121

122+
######
123+
# VPCE Security Group
124+
######
125+
variable "vpce_create_security_group" {
126+
description = "Creates a security group for VPCE."
127+
type = bool
128+
default = true
129+
}
130+
131+
variable "vpce_security_group_name" {
132+
description = "Security Group name for VPCE."
133+
type = string
134+
default = null
135+
}
136+
137+
variable "vpce_security_group_description" {
138+
description = "Security Group description for VPCE."
139+
type = string
140+
default = null
141+
}
142+
143+
variable "vpce_security_group_sources" {
144+
description = "Sources for inbound traffic to Opensearch Serverless"
145+
type = list(object({
146+
type = string
147+
sources = list(string)
148+
}))
149+
default = []
150+
}
151+
122152
######
123153
# Data Access Policy
124154
######

0 commit comments

Comments
 (0)