Debian Codegraph @

mysql/awsmysql/main.tf

raw
1 # Copyright 2018 The Go Cloud Development Kit Authors
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 # https://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 # Harness for MySQL tests.
16
17 terraform {
18 required_version = "~>1.13.2"
19 required_providers {
20 aws = {
21 source = "hashicorp/aws"
22 version = "~> 6.0"
23 }
24 random = {
25 source = "hashicorp/random"
26 }
27 }
28 }
29
30 provider "aws" {
31 region = var.region
32 }
33
34 provider "random" {
35 }
36
37 variable "region" {
38 type = string
39 description = "Region to create resources in. See https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html for valid values."
40 }
41
42 resource "aws_security_group" "main" {
43 name_prefix = "testdb"
44 description = "Security group for the Go CDK MySQL test database."
45
46 ingress {
47 from_port = 3306
48 to_port = 3306
49 protocol = "tcp"
50 cidr_blocks = ["0.0.0.0/0"]
51 description = "Public MySQL access"
52 }
53
54 egress {
55 from_port = 0
56 to_port = 0
57 protocol = "-1"
58 cidr_blocks = ["0.0.0.0/0"]
59 description = "All outgoing traffic allowed"
60 }
61 }
62
63 resource "random_string" "db_password" {
64 keepers = {
65 region = var.region
66 }
67
68 special = false
69 length = 20
70 }
71
72 locals {
73 iamUser = "iam-user"
74 }
75
76 resource "aws_db_instance" "main" {
77 identifier_prefix = "go-cloud-test"
78 engine = "mysql"
79 engine_version = "8.0.43"
80 instance_class = "db.t3.micro"
81 allocated_storage = 20
82 username = "root"
83 password = random_string.db_password.result
84 db_name = "testdb"
85 publicly_accessible = true
86 vpc_security_group_ids = [aws_security_group.main.id]
87 skip_final_snapshot = true
88
89 iam_database_authentication_enabled = true
90 }
91
92 # Data source to get current AWS account information
93 data "aws_caller_identity" "current" {}
94
95 # IAM Role that can be assumed to connect to the database
96 resource "aws_iam_role" "rds_connect" {
97 name_prefix = "rds-connect-"
98
99 assume_role_policy = jsonencode({
100 Version = "2012-10-17"
101 Statement = [
102 {
103 Action = "sts:AssumeRole"
104 Effect = "Allow"
105 Principal = {
106 AWS = data.aws_caller_identity.current.arn
107 }
108 }
109 ]
110 })
111 }
112
113 # IAM Policy to allow RDS connection using IAM authentication
114 resource "aws_iam_policy" "rds_connect" {
115 name_prefix = "rds-connect-"
116 description = "Allow connecting to RDS MySQL instance using IAM authentication"
117
118 policy = jsonencode({
119 Version = "2012-10-17"
120 Statement = [
121 {
122 Effect = "Allow"
123 Action = [
124 "rds-db:connect"
125 ]
126 Resource = format("arn:aws:rds-db:%s:%s:dbuser:%s/%s",
127 var.region, data.aws_caller_identity.current.account_id,
128 aws_db_instance.main.resource_id,
129 local.iamUser,
130 )
131 }
132 ]
133 })
134 }
135
136 # Attach the policy to the role
137 resource "aws_iam_role_policy_attachment" "rds_connect" {
138 role = aws_iam_role.rds_connect.name
139 policy_arn = aws_iam_policy.rds_connect.arn
140 }
141
142 # Automatically create the IAM database user
143 resource "null_resource" "create_iam_user" {
144 depends_on = [aws_db_instance.main]
145
146 provisioner "local-exec" {
147 command = <<-EOT
148 docker run --rm mysql:8.0 \
149 mysql -h ${split(":", aws_db_instance.main.endpoint)[0]} \
150 -P ${split(":", aws_db_instance.main.endpoint)[1]} \
151 -u ${aws_db_instance.main.username} \
152 -p${random_string.db_password.result} \
153 -e "CREATE USER IF NOT EXISTS '${local.iamUser}' IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS'; GRANT ALL PRIVILEGES ON ${aws_db_instance.main.db_name}.* TO 'iam-user'@'%'; FLUSH PRIVILEGES;"
154 EOT
155 }
156
157 triggers = {
158 endpoint = aws_db_instance.main.endpoint
159 }
160 }
161
162 output "endpoint" {
163 value = aws_db_instance.main.endpoint
164 description = "The RDS instance's host/port."
165 }
166
167 output "username" {
168 value = "root"
169 description = "The MySQL username to connect with."
170 }
171
172 output "password" {
173 value = random_string.db_password.result
174 sensitive = true
175 description = "The RDS instance password for the user."
176 }
177
178 output "database" {
179 value = "testdb"
180 description = "The name of the database inside the RDS instance."
181 }
182
183 output "iam_role_arn" {
184 value = aws_iam_role.rds_connect.arn
185 description = "The ARN of the IAM role that can connect to the database"
186 }
187
188 output "iam_db_username" {
189 value = local.iamUser
190 description = "The IAM database username to use for IAM authentication"
191 }