Skip to content

Commit 8b7202b

Browse files
authored
feat: add module for dns and bucket (#145)
<!-- ~ Copyright 2023 StreamNative, Inc. ~ ~ Licensed under the Apache License, Version 2.0 (the "License"); ~ you may not use this file except in compliance with the License. ~ You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, software ~ distributed under the License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~ See the License for the specific language governing permissions and ~ limitations under the License. --> <!-- ### Contribution Checklist - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review. - Each pull request should address only one issue, not mix up code from multiple issues. - Each commit in the pull request has a meaningful commit message - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below. **(The sections below can be removed for hotfixes of typos)** --> ### Motivation For support StreamNative Cloud. ### Modifications - Add new submodule for creating dns zone and s3 bucket It will backport as `v3.4.0` in https://github.com/streamnative/terraform-aws-cloud/tree/release/v3.4.0 ### Verifying this change - [x] Verified ### Documentation - [x] `doc`
1 parent 3dff859 commit 8b7202b

File tree

6 files changed

+292
-0
lines changed

6 files changed

+292
-0
lines changed

modules/dns-bucket/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!--
2+
~ Copyright 2023 StreamNative, Inc.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
# DNS and Bucket Module
18+
A basic module used to create Route53 Zone and S3 Buckets.
19+
20+
<!-- BEGIN_TF_DOCS -->
21+
## Requirements
22+
23+
| Name | Version |
24+
|------|---------|
25+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >=1.2.0 |
26+
27+
## Providers
28+
29+
| Name | Version |
30+
|------|---------|
31+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.76.0 |
32+
| <a name="provider_aws.source"></a> [aws.source](#provider\_aws.source) | 5.76.0 |
33+
| <a name="provider_aws.target"></a> [aws.target](#provider\_aws.target) | 5.76.0 |
34+
35+
## Modules
36+
37+
No modules.
38+
39+
## Resources
40+
41+
| Name | Type |
42+
|------|------|
43+
| [aws_route53_record.delegate](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
44+
| [aws_route53_zone.zone](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_zone) | resource |
45+
| [aws_s3_bucket.tiered_storage](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
46+
| [aws_s3_bucket.velero](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
47+
| [aws_s3_bucket_server_side_encryption_configuration.velero](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
48+
| [aws_kms_key.s3_default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/kms_key) | data source |
49+
| [aws_route53_zone.sn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
50+
51+
## Inputs
52+
53+
| Name | Description | Type | Default | Required |
54+
|------|-------------|------|---------|:--------:|
55+
| <a name="input_custom_dns_zone_id"></a> [custom\_dns\_zone\_id](#input\_custom\_dns\_zone\_id) | if specified, then a streamnative zone will not be created, and this zone will be used instead. Otherwise, we will provision a new zone and delegate access | `string` | `""` | no |
56+
| <a name="input_custom_dns_zone_name"></a> [custom\_dns\_zone\_name](#input\_custom\_dns\_zone\_name) | must be passed if custom\_dns\_zone\_id is passed, this is the zone name to use | `string` | `""` | no |
57+
| <a name="input_extra_aws_tags"></a> [extra\_aws\_tags](#input\_extra\_aws\_tags) | Additional to apply to the resources. Note that this module sets the tags Name, Type, and Vendor by default. They can be overwritten, but it is not recommended. | `map(string)` | `{}` | no |
58+
| <a name="input_parent_zone_name"></a> [parent\_zone\_name](#input\_parent\_zone\_name) | The parent zone in which we create the delegation records | `string` | n/a | yes |
59+
| <a name="input_pm_name"></a> [pm\_name](#input\_pm\_name) | The name of the poolmember, for new clusters, this should be like `pm-<xxxxx>` | `string` | n/a | yes |
60+
| <a name="input_s3_encryption_kms_key_arn"></a> [s3\_encryption\_kms\_key\_arn](#input\_s3\_encryption\_kms\_key\_arn) | KMS key ARN to use for S3 encryption. If not set, the default AWS S3 key will be used. | `string` | `""` | no |
61+
62+
## Outputs
63+
64+
| Name | Description |
65+
|------|-------------|
66+
| <a name="output_backup_bucket"></a> [backup\_bucket](#output\_backup\_bucket) | n/a |
67+
| <a name="output_backup_bucket_kms_key_id"></a> [backup\_bucket\_kms\_key\_id](#output\_backup\_bucket\_kms\_key\_id) | n/a |
68+
| <a name="output_tiered_storage_bucket"></a> [tiered\_storage\_bucket](#output\_tiered\_storage\_bucket) | n/a |
69+
| <a name="output_zone_id"></a> [zone\_id](#output\_zone\_id) | n/a |
70+
| <a name="output_zone_name"></a> [zone\_name](#output\_zone\_name) | n/a |
71+
<!-- END_TF_DOCS -->

modules/dns-bucket/bucket.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2023 StreamNative, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
resource "aws_s3_bucket" "velero" {
16+
bucket = format("%s-cluster-backup-snc", var.pm_name)
17+
tags = merge({ "Attributes" = "backup", "Name" = "velero-backups" }, local.tags)
18+
force_destroy = true
19+
20+
lifecycle {
21+
ignore_changes = [
22+
bucket,
23+
]
24+
}
25+
}
26+
27+
resource "aws_s3_bucket" "tiered_storage" {
28+
bucket = format("%s-tiered-storage-snc", var.pm_name)
29+
tags = merge({ "Attributes" = "tiered-storage" }, local.tags)
30+
force_destroy = true
31+
32+
lifecycle {
33+
ignore_changes = [
34+
bucket,
35+
]
36+
}
37+
}
38+
39+
data "aws_kms_key" "s3_default" {
40+
key_id = "alias/aws/s3"
41+
}
42+
43+
locals {
44+
s3_kms_key = var.s3_encryption_kms_key_arn == "" ? data.aws_kms_key.s3_default.arn : var.s3_encryption_kms_key_arn
45+
}
46+
47+
resource "aws_s3_bucket_server_side_encryption_configuration" "velero" {
48+
bucket = aws_s3_bucket.velero.bucket
49+
50+
rule {
51+
apply_server_side_encryption_by_default {
52+
kms_master_key_id = local.s3_kms_key
53+
sse_algorithm = "aws:kms"
54+
}
55+
}
56+
}

modules/dns-bucket/dns.tf

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2023 StreamNative, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
locals {
16+
new_zone_name = "${var.pm_name}.${var.parent_zone_name}"
17+
zone_name = var.custom_dns_zone_name != "" ? var.custom_dns_zone_name : try(aws_route53_zone.zone[0].name, "")
18+
zone_id = var.custom_dns_zone_id != "" ? var.custom_dns_zone_id : try(aws_route53_zone.zone[0].id, "")
19+
}
20+
21+
resource "aws_route53_zone" "zone" {
22+
count = var.custom_dns_zone_id == "" ? 1 : 0
23+
provider = aws.target
24+
25+
name = local.new_zone_name
26+
tags = local.tags
27+
force_destroy = true
28+
}
29+
30+
data "aws_route53_zone" "sn" {
31+
count = var.custom_dns_zone_id == "" ? 1 : 0
32+
provider = aws.source
33+
34+
name = var.parent_zone_name
35+
}
36+
37+
resource "aws_route53_record" "delegate" {
38+
count = var.custom_dns_zone_id == "" ? 1 : 0
39+
provider = aws.source
40+
41+
zone_id = data.aws_route53_zone.sn[0].zone_id
42+
name = aws_route53_zone.zone[0].name
43+
type = "NS"
44+
ttl = "300"
45+
records = aws_route53_zone.zone[0].name_servers
46+
}

modules/dns-bucket/outputs.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2023 StreamNative, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
output "zone_id" {
16+
value = local.zone_id
17+
}
18+
19+
output "zone_name" {
20+
value = local.zone_name
21+
}
22+
23+
output "backup_bucket" {
24+
value = aws_s3_bucket.velero.bucket
25+
}
26+
27+
output "backup_bucket_kms_key_id" {
28+
value = local.s3_kms_key
29+
}
30+
31+
output "tiered_storage_bucket" {
32+
value = aws_s3_bucket.tiered_storage.bucket
33+
}

modules/dns-bucket/variables.tf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2023 StreamNative, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
variable "pm_name" {
16+
description = "The name of the poolmember, for new clusters, this should be like `pm-<xxxxx>`"
17+
type = string
18+
}
19+
20+
variable "parent_zone_name" {
21+
type = string
22+
description = "The parent zone in which we create the delegation records"
23+
}
24+
25+
variable "custom_dns_zone_id" {
26+
type = string
27+
default = ""
28+
description = "if specified, then a streamnative zone will not be created, and this zone will be used instead. Otherwise, we will provision a new zone and delegate access"
29+
}
30+
31+
variable "custom_dns_zone_name" {
32+
type = string
33+
default = ""
34+
description = "must be passed if custom_dns_zone_id is passed, this is the zone name to use"
35+
}
36+
37+
variable "s3_encryption_kms_key_arn" {
38+
default = ""
39+
description = "KMS key ARN to use for S3 encryption. If not set, the default AWS S3 key will be used."
40+
type = string
41+
}
42+
43+
variable "extra_aws_tags" {
44+
default = {}
45+
description = "Additional to apply to the resources. Note that this module sets the tags Name, Type, and Vendor by default. They can be overwritten, but it is not recommended."
46+
type = map(string)
47+
}
48+
49+
locals {
50+
tags = merge({
51+
"Vendor" = "StreamNative"
52+
}, var.extra_aws_tags)
53+
}

modules/dns-bucket/versions.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2023 StreamNative, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
terraform {
16+
required_version = ">=1.2.0"
17+
18+
required_providers {
19+
aws = {
20+
source = "hashicorp/aws"
21+
# NOTE! we required two different providers in this module this is because we need to create a zone in the target and then create the delegations in the source
22+
configuration_aliases = [aws.target, aws.source]
23+
}
24+
}
25+
}
26+
27+
provider "aws" {
28+
alias = "target"
29+
}
30+
31+
provider "aws" {
32+
alias = "source"
33+
}

0 commit comments

Comments
 (0)