|
| 1 | +provider "aws" { |
| 2 | + region = local.region |
| 3 | +} |
| 4 | + |
| 5 | +data "aws_caller_identity" "current" {} |
| 6 | +data "aws_availability_zones" "available" {} |
| 7 | + |
| 8 | +locals { |
| 9 | + name = "role-association-invoke-lambda" |
| 10 | + region = "eu-west-1" |
| 11 | + |
| 12 | + vpc_cidr = "10.0.0.0/16" |
| 13 | + azs = slice(data.aws_availability_zones.available.names, 0, 3) |
| 14 | + |
| 15 | + tags = { |
| 16 | + Name = local.name |
| 17 | + Example = local.name |
| 18 | + Repository = "https://github.com/terraform-aws-modules/terraform-aws-rds" |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +################################################################################ |
| 23 | +# RDS Module |
| 24 | +################################################################################ |
| 25 | + |
| 26 | +module "db" { |
| 27 | + source = "../../" |
| 28 | + |
| 29 | + identifier = local.name |
| 30 | + |
| 31 | + # All available versions: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts |
| 32 | + engine = "postgres" |
| 33 | + engine_version = "14" |
| 34 | + family = "postgres14" # DB parameter group |
| 35 | + major_engine_version = "14" # DB option group |
| 36 | + instance_class = "db.t4g.large" |
| 37 | + |
| 38 | + allocated_storage = 20 |
| 39 | + |
| 40 | + # NOTE: Do NOT use 'user' as the value for 'username' as it throws: |
| 41 | + # "Error creating DB Instance: InvalidParameterValue: MasterUsername |
| 42 | + # user cannot be used as it is a reserved word used by the engine" |
| 43 | + db_name = "RoleAssociationInvokeLambda" |
| 44 | + username = "role_association_invoke_lambda" |
| 45 | + port = 5432 |
| 46 | + |
| 47 | + multi_az = true |
| 48 | + db_subnet_group_name = module.vpc.database_subnet_group |
| 49 | + vpc_security_group_ids = [module.security_group.security_group_id] |
| 50 | + |
| 51 | + maintenance_window = "Mon:00:00-Mon:03:00" |
| 52 | + backup_window = "03:00-06:00" |
| 53 | + backup_retention_period = 0 |
| 54 | + |
| 55 | + deletion_protection = false |
| 56 | + |
| 57 | + # https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/PostgreSQL-Lambda.html |
| 58 | + db_instance_role_associations = { |
| 59 | + Lambda = module.rds_invoke_lambda_role.iam_role_arn |
| 60 | + } |
| 61 | + |
| 62 | + parameters = [ |
| 63 | + { |
| 64 | + name = "rds.custom_dns_resolution" |
| 65 | + value = 1 |
| 66 | + apply_method = "pending-reboot" |
| 67 | + }, |
| 68 | + ] |
| 69 | + |
| 70 | + tags = local.tags |
| 71 | +} |
| 72 | + |
| 73 | +################################################################################ |
| 74 | +# Supporting Resources |
| 75 | +################################################################################ |
| 76 | + |
| 77 | +module "vpc" { |
| 78 | + source = "terraform-aws-modules/vpc/aws" |
| 79 | + version = "~> 5.0" |
| 80 | + |
| 81 | + name = local.name |
| 82 | + cidr = local.vpc_cidr |
| 83 | + |
| 84 | + azs = local.azs |
| 85 | + public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)] |
| 86 | + private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 3)] |
| 87 | + database_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 6)] |
| 88 | + |
| 89 | + create_database_subnet_group = true |
| 90 | + enable_nat_gateway = true |
| 91 | + |
| 92 | + tags = local.tags |
| 93 | +} |
| 94 | + |
| 95 | +module "security_group" { |
| 96 | + source = "terraform-aws-modules/security-group/aws" |
| 97 | + version = "~> 4.0" |
| 98 | + |
| 99 | + name = local.name |
| 100 | + description = "Complete PostgreSQL example security group" |
| 101 | + vpc_id = module.vpc.vpc_id |
| 102 | + |
| 103 | + # ingress |
| 104 | + ingress_with_cidr_blocks = [ |
| 105 | + { |
| 106 | + from_port = 5432 |
| 107 | + to_port = 5432 |
| 108 | + protocol = "tcp" |
| 109 | + description = "PostgreSQL access from within VPC" |
| 110 | + cidr_blocks = module.vpc.vpc_cidr_block |
| 111 | + }, |
| 112 | + ] |
| 113 | + |
| 114 | + # egress |
| 115 | + egress_with_cidr_blocks = [ |
| 116 | + { |
| 117 | + from_port = 443 |
| 118 | + to_port = 443 |
| 119 | + protocol = "tcp" |
| 120 | + description = "Egress to AWS Lambda VPC" |
| 121 | + cidr_blocks = "0.0.0.0/0" |
| 122 | + } |
| 123 | + ] |
| 124 | + |
| 125 | + tags = local.tags |
| 126 | +} |
| 127 | + |
| 128 | +module "rds_invoke_lambda_role" { |
| 129 | + source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role" |
| 130 | + version = "~> 5.28.0" |
| 131 | + |
| 132 | + create_role = true |
| 133 | + role_requires_mfa = false |
| 134 | + |
| 135 | + role_name_prefix = local.name |
| 136 | + |
| 137 | + custom_role_policy_arns = [ |
| 138 | + module.rds_invoke_lambda_policy.arn |
| 139 | + ] |
| 140 | + custom_role_trust_policy = data.aws_iam_policy_document.rds_invoke_lambda_assume_role.json |
| 141 | +} |
| 142 | + |
| 143 | +module "rds_invoke_lambda_policy" { |
| 144 | + source = "terraform-aws-modules/iam/aws//modules/iam-policy" |
| 145 | + version = "~> 5.28.0" |
| 146 | + |
| 147 | + name = "${local.name}-policy" |
| 148 | + path = "/" |
| 149 | + description = "Invoke Lambda from RDS Postgresql policy" |
| 150 | + |
| 151 | + policy = data.aws_iam_policy_document.rds_invoke_lambda.json |
| 152 | +} |
| 153 | + |
| 154 | +data "aws_iam_policy_document" "rds_invoke_lambda" { |
| 155 | + statement { |
| 156 | + sid = "InvokeLambda" |
| 157 | + actions = [ |
| 158 | + "lambda:InvokeFunction" |
| 159 | + ] |
| 160 | + resources = [ |
| 161 | + module.lambda.lambda_function_arn |
| 162 | + ] |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +data "aws_iam_policy_document" "rds_invoke_lambda_assume_role" { |
| 167 | + statement { |
| 168 | + sid = "AssumeRole" |
| 169 | + |
| 170 | + principals { |
| 171 | + type = "Service" |
| 172 | + identifiers = ["rds.amazonaws.com"] |
| 173 | + } |
| 174 | + |
| 175 | + condition { |
| 176 | + test = "StringEquals" |
| 177 | + values = [data.aws_caller_identity.current.id] |
| 178 | + variable = "aws:SourceAccount" |
| 179 | + } |
| 180 | + |
| 181 | + effect = "Allow" |
| 182 | + |
| 183 | + actions = ["sts:AssumeRole"] |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +module "lambda" { |
| 188 | + source = "terraform-aws-modules/lambda/aws" |
| 189 | + version = "~> 6.0" |
| 190 | + |
| 191 | + function_name = local.name |
| 192 | + handler = "lambda_function.lambda_handler" |
| 193 | + runtime = "python3.10" |
| 194 | + source_path = "${path.module}/fixtures/lambda_function.py" |
| 195 | +} |
0 commit comments