Skip to content

Commit f671e2e

Browse files
Merge pull request #6 from amanravi-squareops/release-1.1.3
fixed restore database
2 parents dc6c6a7 + a8b75b3 commit f671e2e

File tree

10 files changed

+87
-28
lines changed

10 files changed

+87
-28
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ The required IAM permissions to create resources from this module can be found [
196196
| <a name="output_db_instance_name"></a> [db\_instance\_name](#output\_db\_instance\_name) | Name of the database instance |
197197
| <a name="output_db_instance_password"></a> [db\_instance\_password](#output\_db\_instance\_password) | Password for accessing the database. |
198198
| <a name="output_db_instance_username"></a> [db\_instance\_username](#output\_db\_instance\_username) | Master username for accessing the database. |
199+
| <a name="output_db_name"></a> [db\_name](#output\_db\_name) | The database name used in the RDS module |
199200
| <a name="output_db_parameter_group_id"></a> [db\_parameter\_group\_id](#output\_db\_parameter\_group\_id) | ID of the parameter group associated with the RDS instance. |
200201
| <a name="output_db_subnet_group_id"></a> [db\_subnet\_group\_id](#output\_db\_subnet\_group\_id) | ID of the subnet group associated with the RDS instance. |
201202
| <a name="output_master_credential_secret_arn"></a> [master\_credential\_secret\_arn](#output\_master\_credential\_secret\_arn) | The ARN of the master user secret (Only available when manage\_master\_user\_password is set to true) |

examples/complete/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This example will be very useful for users who are new to a module and want to q
2727
| Name | Source | Version |
2828
|------|--------|---------|
2929
| <a name="module_kms"></a> [kms](#module\_kms) | terraform-aws-modules/kms/aws | ~> 1.0 |
30-
| <a name="module_rds-pg"></a> [rds-pg](#module\_rds-pg) | squareops/rds-postgresql/aws | n/a |
30+
| <a name="module_rds-pg"></a> [rds-pg](#module\_rds-pg) | squareops/rds-postgresql/aws | 2.0.0 |
3131
| <a name="module_vpc"></a> [vpc](#module\_vpc) | squareops/vpc/aws | n/a |
3232

3333
## Resources
@@ -47,6 +47,7 @@ No inputs.
4747

4848
| Name | Description |
4949
|------|-------------|
50+
| <a name="output_db_name"></a> [db\_name](#output\_db\_name) | Database name |
5051
| <a name="output_instance_endpoint"></a> [instance\_endpoint](#output\_instance\_endpoint) | Connection endpoint of the RDS instance. |
5152
| <a name="output_instance_name"></a> [instance\_name](#output\_instance\_name) | Name of the database instance. |
5253
| <a name="output_instance_password"></a> [instance\_password](#output\_instance\_password) | Password for accessing the database (Note: Terraform does not track this password after initial creation). |

examples/complete/main.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ locals {
66
environment = "prod"
77
create_namespace = true
88
namespace = "pg"
9-
engine_version = "15.4"
9+
engine_version = "15.7"
1010
instance_class = "db.t4g.micro"
1111
storage_type = "gp3"
1212
cluster_name = ""
@@ -92,7 +92,7 @@ module "vpc" {
9292
name = local.name
9393
vpc_cidr = local.vpc_cidr
9494
environment = local.environment
95-
availability_zones = ["us-east-1a", "us-east-1b"]
95+
availability_zones = ["us-east-2a", "us-east-2b"]
9696
public_subnet_enabled = true
9797
auto_assign_public_ip = true
9898
intra_subnet_enabled = false
@@ -141,13 +141,13 @@ module "rds-pg" {
141141
create_namespace = local.create_namespace
142142
postgresdb_backup_enabled = false
143143
postgresdb_backup_config = {
144-
postgres_database_name = "" # Specify the database name or Leave empty if you wish to backup all databases
145-
cron_for_full_backup = "*/2 * * * *" # set cronjob for backup
146-
bucket_uri = "s3://postgres-backups-atmosly" # s3 bucket uri
144+
postgres_database_name = "" # Specify the database name or Leave empty if you wish to backup all databases
145+
cron_for_full_backup = "*/2 * * * *" # set cronjob for backup
146+
bucket_uri = "s3://my-backup-dumps-databases" # s3 bucket uri
147147
}
148-
postgresdb_restore_enabled = false
148+
postgresdb_restore_enabled = true
149149
postgresdb_restore_config = {
150-
bucket_uri = "s3://postgres-backups-atmosly" #S3 bucket URI (without a trailing slash /) containing the backup dump file.
151-
backup_file_name = "db5_20241114111607.sql" #Give .sql or .zip file for restore
150+
bucket_uri = "s3://my-backup-dumps-databases" #S3 bucket URI (without a trailing slash /) containing the backup dump file.
151+
backup_file_name = "atmosly_db1.sql" #Give .sql or .zip file for restore
152152
}
153153
}

examples/complete/provider.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ provider "aws" {
55
}
66
}
77
data "aws_eks_cluster" "cluster" {
8-
name = ""
8+
name = local.cluster_name
99

1010
}
1111
data "aws_eks_cluster_auth" "cluster" {
12-
name = ""
12+
name = local.cluster_name
1313
}
1414

1515
provider "kubernetes" {

main.tf

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ resource "aws_lambda_permission" "sns_lambda_slack_invoke" {
323323
module "backup_restore" {
324324
depends_on = [module.db]
325325
source = "./modules/db-backup-restore"
326+
name = var.name
326327
cluster_name = var.cluster_name
327328
namespace = var.namespace
328329
create_namespace = var.create_namespace
@@ -331,20 +332,17 @@ module "backup_restore" {
331332
db_username = var.master_username
332333
db_password = var.custom_user_password != "" ? var.custom_user_password : nonsensitive(random_password.master[0].result)
333334
postgres_database_name = var.postgresdb_backup_config.postgres_database_name
334-
# s3_bucket_region = var.postgresdb_backup_config.s3_bucket_region
335-
cron_for_full_backup = var.postgresdb_backup_config.cron_for_full_backup
336-
bucket_uri = var.postgresdb_backup_config.bucket_uri
337-
db_endpoint = replace(var.replica_enable ? module.db_replica[0].db_instance_endpoint : module.db.db_instance_endpoint, ":5432", "")
335+
cron_for_full_backup = var.postgresdb_backup_config.cron_for_full_backup
336+
bucket_uri = var.postgresdb_backup_config.bucket_uri
337+
db_endpoint = replace(var.replica_enable ? module.db_replica[0].db_instance_endpoint : module.db.db_instance_endpoint, ":5432", "")
338338
}
339339

340340
postgresdb_restore_enabled = var.postgresdb_restore_enabled
341341
postgresdb_restore_config = {
342-
db_endpoint = replace(var.replica_enable ? module.db_replica[0].db_instance_endpoint : module.db.db_instance_endpoint, ":5432", "")
343-
db_username = var.master_username
344-
db_password = var.custom_user_password != "" ? var.custom_user_password : nonsensitive(random_password.master[0].result)
345-
bucket_uri = var.postgresdb_restore_config.bucket_uri
346-
# s3_bucket_region = var.postgresdb_restore_config.s3_bucket_region
347-
# DB_NAME = var.postgresdb_restore_config.DB_NAME,
342+
db_endpoint = replace(var.replica_enable ? module.db_replica[0].db_instance_endpoint : module.db.db_instance_endpoint, ":5432", "")
343+
db_username = var.master_username
344+
db_password = var.custom_user_password != "" ? var.custom_user_password : nonsensitive(random_password.master[0].result)
345+
bucket_uri = var.postgresdb_restore_config.bucket_uri
348346
backup_file_name = var.postgresdb_restore_config.backup_file_name,
349347
}
350348
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# db-backup-restore
2+
3+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
4+
## Requirements
5+
6+
No requirements.
7+
8+
## Providers
9+
10+
| Name | Version |
11+
|------|---------|
12+
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
13+
| <a name="provider_helm"></a> [helm](#provider\_helm) | n/a |
14+
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | n/a |
15+
16+
## Modules
17+
18+
No modules.
19+
20+
## Resources
21+
22+
| Name | Type |
23+
|------|------|
24+
| [aws_iam_role.postgres_backup_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
25+
| [aws_iam_role.postgres_restore_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
26+
| [helm_release.postgresdb_backup](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
27+
| [helm_release.postgresdb_restore](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
28+
| [kubernetes_namespace.postgresdb](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace) | resource |
29+
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
30+
| [aws_eks_cluster.kubernetes_cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster) | data source |
31+
32+
## Inputs
33+
34+
| Name | Description | Type | Default | Required |
35+
|------|-------------|------|---------|:--------:|
36+
| <a name="input_azure_container_name"></a> [azure\_container\_name](#input\_azure\_container\_name) | Azure container name | `string` | `""` | no |
37+
| <a name="input_azure_storage_account_key"></a> [azure\_storage\_account\_key](#input\_azure\_storage\_account\_key) | Azure storage account key | `string` | `""` | no |
38+
| <a name="input_azure_storage_account_name"></a> [azure\_storage\_account\_name](#input\_azure\_storage\_account\_name) | Azure storage account name | `string` | `""` | no |
39+
| <a name="input_bucket_provider_type"></a> [bucket\_provider\_type](#input\_bucket\_provider\_type) | Choose what type of provider you want (s3, gcs) | `string` | `"s3"` | no |
40+
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Specifies the name of the EKS cluster to deploy the MySQL application on. | `string` | `""` | no |
41+
| <a name="input_create_namespace"></a> [create\_namespace](#input\_create\_namespace) | Specify whether or not to create the namespace if it does not already exist. Set it to true to create the namespace. | `string` | `false` | no |
42+
| <a name="input_iam_role_arn_backup"></a> [iam\_role\_arn\_backup](#input\_iam\_role\_arn\_backup) | IAM role ARN for backup (AWS) | `string` | `""` | no |
43+
| <a name="input_iam_role_arn_restore"></a> [iam\_role\_arn\_restore](#input\_iam\_role\_arn\_restore) | IAM role ARN for restore (AWS) | `string` | `""` | no |
44+
| <a name="input_name"></a> [name](#input\_name) | Name identifier for module to be added as suffix to resources | `string` | `"test"` | no |
45+
| <a name="input_namespace"></a> [namespace](#input\_namespace) | Name of the Kubernetes namespace where the MYSQL deployment will be deployed. | `string` | `"postgresdb"` | no |
46+
| <a name="input_postgresdb_backup_config"></a> [postgresdb\_backup\_config](#input\_postgresdb\_backup\_config) | configuration options for MySQL database backups. It includes properties such as the S3 bucket URI, the S3 bucket region, and the cron expression for full backups. | `map(string)` | <pre>{<br/> "bucket_uri": "",<br/> "cron_for_full_backup": "",<br/> "postgres_database_name": "",<br/> "s3_bucket_region": ""<br/>}</pre> | no |
47+
| <a name="input_postgresdb_backup_enabled"></a> [postgresdb\_backup\_enabled](#input\_postgresdb\_backup\_enabled) | Specifies whether to enable backups for MySQL database. | `bool` | `false` | no |
48+
| <a name="input_postgresdb_permission"></a> [postgresdb\_permission](#input\_postgresdb\_permission) | access | `bool` | `false` | no |
49+
| <a name="input_postgresdb_restore_config"></a> [postgresdb\_restore\_config](#input\_postgresdb\_restore\_config) | Configuration options for restoring dump to the MySQL database. | `any` | <pre>{<br/> "DB_NAME": "",<br/> "backup_file_name": "",<br/> "bucket_uri": "",<br/> "file_name": ""<br/>}</pre> | no |
50+
| <a name="input_postgresdb_restore_enabled"></a> [postgresdb\_restore\_enabled](#input\_postgresdb\_restore\_enabled) | Specifies whether to enable restoring dump to the MySQL database. | `bool` | `false` | no |
51+
| <a name="input_service_account_backup"></a> [service\_account\_backup](#input\_service\_account\_backup) | Service account for backup (GCP) | `string` | `""` | no |
52+
| <a name="input_service_account_restore"></a> [service\_account\_restore](#input\_service\_account\_restore) | Service account for restore (GCP) | `string` | `""` | no |
53+
54+
## Outputs
55+
56+
No outputs.
57+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/db-backup-restore/backup/templates/cronjob.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ spec:
1818
serviceAccountName: sa-postgres-backup
1919
containers:
2020
- name: backup-postgresdb
21-
image: squareops01/rds-postgresql-backup:v1
21+
image: squareops01/rds-postgresql-backup:v2
2222
imagePullPolicy: Always
2323
command: ["/backup/backup_script.sh"]
2424
env:

modules/db-backup-restore/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ resource "helm_release" "postgresdb_backup" {
2222
db_username = var.postgresdb_backup_config.db_username,
2323
# s3_bucket_region = var.postgresdb_backup_config.s3_bucket_region ,
2424
cron_for_full_backup = var.postgresdb_backup_config.cron_for_full_backup,
25-
annotations = var.bucket_provider_type == "s3" ? "eks.amazonaws.com/role-arn: ${aws_iam_role.postgres_backup_role.arn}" : "iam.gke.io/gcp-service-account: ${var.service_account_backup}"
25+
annotations = var.bucket_provider_type == "s3" ? "eks.amazonaws.com/role-arn: ${aws_iam_role.postgres_backup_role[count.index].arn}" : "iam.gke.io/gcp-service-account: ${var.service_account_backup}"
2626
})
2727
]
2828
}
@@ -43,7 +43,7 @@ resource "helm_release" "postgresdb_restore" {
4343
db_password = var.postgresdb_restore_config.db_password,
4444
db_username = var.postgresdb_restore_config.db_username,
4545
backup_file_name = var.postgresdb_restore_config.backup_file_name,
46-
annotations = var.bucket_provider_type == "s3" ? "eks.amazonaws.com/role-arn: ${aws_iam_role.postgres_restore_role.arn}" : "iam.gke.io/gcp-service-account: ${var.service_account_restore}"
46+
annotations = var.bucket_provider_type == "s3" ? "eks.amazonaws.com/role-arn: ${aws_iam_role.postgres_restore_role[count.index].arn}" : "iam.gke.io/gcp-service-account: ${var.service_account_restore}"
4747
})
4848
]
4949
}

modules/db-backup-restore/restore/templates/job.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ spec:
99
serviceAccountName: sa-postgres-restore
1010
containers:
1111
- name: restore-postgresdb
12-
image: squareops01/rds-postgresql-restore:v1
12+
image: squareops01/rds-postgresql-restore:v2
1313
imagePullPolicy: Always
1414
command: ["/restore/restore_script.sh"]
1515
env:

modules/db-backup-restore/roles.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ data "aws_eks_cluster" "kubernetes_cluster" {
1313
}
1414

1515
resource "aws_iam_role" "postgres_backup_role" {
16-
name = format("%s-%s-%s", var.cluster_name, var.name, "postgres-backup-rds")
16+
count = var.postgresdb_backup_enabled ? 1 : 0
17+
name = format("%s-%s-%s", var.cluster_name, var.name, "postgres-backup-rds")
1718
assume_role_policy = jsonencode({
1819
Version = "2012-10-17",
1920
Statement = [
@@ -56,7 +57,8 @@ resource "aws_iam_role" "postgres_backup_role" {
5657

5758

5859
resource "aws_iam_role" "postgres_restore_role" {
59-
name = format("%s-%s-%s", var.cluster_name, var.name, "postgres-restore")
60+
count = var.postgresdb_restore_enabled ? 1 : 0
61+
name = format("%s-%s-%s", var.cluster_name, var.name, "postgres-restore")
6062
assume_role_policy = jsonencode({
6163
Version = "2012-10-17",
6264
Statement = [
@@ -95,4 +97,4 @@ resource "aws_iam_role" "postgres_restore_role" {
9597
]
9698
})
9799
}
98-
}
100+
}

0 commit comments

Comments
 (0)