-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from FIAP-3SOAT-G15/setup-rds
Setup RDS
- Loading branch information
Showing
7 changed files
with
162 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,39 +1,37 @@ | ||
version: '3.7' | ||
|
||
services: | ||
# api: | ||
# image: tech-challenge:latest | ||
# container_name: app | ||
# build: | ||
# context: . | ||
# dockerfile: Dockerfile | ||
# depends_on: | ||
# - db | ||
# environment: | ||
# - DB_ENDPOINT=db:5432 | ||
# - DB_NAME=selforder | ||
# - DB_USERNAME=selforder | ||
# - DB_PASSWORD=self@Order123! | ||
# - ADMIN_ACCESS_TOKEN=token | ||
# - MOCK_PAYMENT_PROVIDER=true | ||
# - MP_TOKEN=token | ||
# - MP_USER_ID=userId | ||
# - MP_POS_ID=postId | ||
# - MP_WEBHOOK_BASE_URL=webhookBaseUrl | ||
# ports: | ||
# - "8080:8080" | ||
# restart: always | ||
|
||
payment_db: | ||
image: amazon/dynamodb-local:latest | ||
stock_api: | ||
image: fiap-3soat-g15-stock-api:latest | ||
container_name: stock_api | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
depends_on: | ||
- stock_db | ||
environment: | ||
- DB_ENDPOINT=stock_db:5432 | ||
- DB_NAME=selforder | ||
- DB_USERNAME=selforder | ||
- DB_PASSWORD=self@Order123! | ||
- ADMIN_ACCESS_TOKEN=token | ||
ports: | ||
- "54000:8000" | ||
- "8080:8080" | ||
restart: always | ||
|
||
stock_db: | ||
image: postgres:15.4 | ||
container_name: stock_db | ||
volumes: | ||
- db:/var/lib/postgresql/data | ||
environment: | ||
AWS_ACCESS_KEY_ID: "fakekey" | ||
AWS_SECRET_ACCESS_KEY: "fakeaccesskey" | ||
AWS_REGION: us-east-1 | ||
command: ["-D\"java.library.path\"=./DynamoDBLocal_lib", "-jar", "DynamoDBLocal.jar", "-inMemory", "-sharedDb"] | ||
- POSTGRES_DB=selforder | ||
- POSTGRES_USER=selforder | ||
- POSTGRES_PASSWORD=self@Order123! | ||
ports: | ||
- "5432:5432" | ||
restart: always | ||
|
||
volumes: | ||
db: | ||
stock_db: | ||
driver: local |
This file contains 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 |
---|---|---|
@@ -1,4 +1 @@ | ||
spring: | ||
|
||
payment-provider: | ||
mock: true |
This file contains 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 |
---|---|---|
@@ -1,5 +1,2 @@ | ||
admin: | ||
access-token: token | ||
|
||
payment-provider: | ||
mock: true |
This file contains 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 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,19 @@ | ||
output "db_instance_master_user_secret_arn" { | ||
description = "The ARN of the master user secret (Only available when manage_master_user_password is set to true)" | ||
value = module.db.db_instance_master_user_secret_arn | ||
} | ||
|
||
output "rds_ssm_parameter_name" { | ||
description = "The name of the SSM parameter for RDS parameters" | ||
value = module.rds_params.ssm_parameter_name | ||
} | ||
|
||
output "rds_secrets_read_only_policy_arn" { | ||
description = "The ARN of the RDS secrets" | ||
value = aws_iam_policy.rds_secrets_read_only_policy.arn | ||
} | ||
|
||
output "rds_params_read_only_policy_arn" { | ||
description = "The ARN of the RDS params" | ||
value = aws_iam_policy.rds_params_read_only_policy.arn | ||
} |
This file contains 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 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,112 @@ | ||
locals { | ||
name = "stock-db" | ||
username = "master" | ||
port = 5432 | ||
} | ||
|
||
data "terraform_remote_state" "tech-challenge" { | ||
backend = "s3" | ||
|
||
config = { | ||
bucket = "fiap-3soat-g15-iac-tech-challenge" | ||
key = "live/terraform.tfstate" | ||
region = var.region | ||
} | ||
} | ||
|
||
module "db" { | ||
source = "terraform-aws-modules/rds/aws" | ||
version = "6.5.2" | ||
|
||
identifier = local.name | ||
|
||
engine = "postgres" | ||
engine_version = "15" | ||
family = "postgres15" | ||
major_engine_version = "15" | ||
instance_class = "db.t3.micro" | ||
|
||
allocated_storage = 10 | ||
max_allocated_storage = 20 | ||
|
||
storage_encrypted = false | ||
|
||
db_name = local.name | ||
username = local.username | ||
port = local.port | ||
|
||
manage_master_user_password = true | ||
|
||
multi_az = false | ||
db_subnet_group_name = data.terraform_remote_state.tech-challenge.outputs.database_subnet_group_name | ||
vpc_security_group_ids = [module.security_group.security_group_id] | ||
|
||
backup_retention_period = 0 | ||
skip_final_snapshot = true | ||
deletion_protection = false | ||
} | ||
|
||
module "security_group" { | ||
source = "terraform-aws-modules/security-group/aws" | ||
version = "~> 5.0" | ||
|
||
name = local.name | ||
vpc_id = data.terraform_remote_state.tech-challenge.outputs.vpc_id | ||
|
||
ingress_with_cidr_blocks = [ | ||
{ | ||
from_port = local.port | ||
to_port = local.port | ||
protocol = "tcp" | ||
cidr_blocks = data.terraform_remote_state.tech-challenge.outputs.vpc_cidr_block | ||
}, | ||
] | ||
} | ||
|
||
module "rds_params" { | ||
source = "terraform-aws-modules/ssm-parameter/aws" | ||
name = "/live/stock/db" | ||
type = "String" | ||
|
||
value = jsonencode({ | ||
name : local.name, | ||
endpoint : module.db.db_instance_endpoint, | ||
port : local.port | ||
}) | ||
} | ||
|
||
resource "aws_iam_policy" "rds_secrets_read_only_policy" { | ||
name = "StockRDSSecretsReadOnlyPolicy" | ||
|
||
policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [ | ||
{ | ||
Effect = "Allow" | ||
Action = [ | ||
"secretsmanager:DescribeSecret", | ||
"secretsmanager:GetSecretValue" | ||
], | ||
Resource = module.db.db_instance_master_user_secret_arn | ||
} | ||
] | ||
}) | ||
} | ||
|
||
resource "aws_iam_policy" "rds_params_read_only_policy" { | ||
name = "StockRDSParamsReadOnlyPolicy" | ||
|
||
policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [ | ||
{ | ||
Effect = "Allow" | ||
Action = [ | ||
"ssm:GetParameter", | ||
"ssm:GetParameters" | ||
], | ||
Resource = module.rds_params.ssm_parameter_arn | ||
} | ||
] | ||
}) | ||
} |