Skip to content

Commit d3efc96

Browse files
authored
Merge pull request #1502 from gruntwork-io/issue-1139
Add Support for Database
2 parents ece065c + 2a72262 commit d3efc96

File tree

8 files changed

+383
-52
lines changed

8 files changed

+383
-52
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Terraform Database Example
2+
3+
This creates an example postgres instance using docker.
4+
5+
Check out [test/terraform_database_example_test.go](/test/terraform_database_example_test.go) to see how you can write automated tests for database. In order to make go test code work, you need to provide host, port, username, password and database name of a existing database, which you have already created on cloud platform or using docker before testing. Only Microsoft SQL Server, PostgreSQL and MySQL are supported.
6+
7+
## Running this module manually
8+
9+
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH`.
10+
2. Run `terraform init`.
11+
3. Run `terraform apply`.
12+
4. When you're done, run `terraform destroy`.
13+
14+
## Running automated tests against this module
15+
16+
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH`.
17+
2. Install [Golang](https://golang.org/) and make sure this code is checked out into your `GOPATH`.
18+
3. `go mod tidy`
19+
4. `go test -v test/terraform_database_example_test.go`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
terraform {
2+
required_providers {
3+
docker = {
4+
source = "kreuzwerker/docker"
5+
version = "~> 3.0"
6+
}
7+
}
8+
9+
required_version = ">= 1.3.0"
10+
}
11+
12+
provider "docker" {
13+
}
14+
15+
resource "docker_network" "postgres_network" {
16+
name = "postgres_network"
17+
}
18+
19+
resource "docker_volume" "postgres_volume" {
20+
name = "postgres_data"
21+
}
22+
23+
resource "docker_container" "postgres" {
24+
name = "postgres"
25+
image = "postgres:15"
26+
27+
env = [
28+
"POSTGRES_USER=${var.username}",
29+
"POSTGRES_PASSWORD=${var.password}",
30+
"POSTGRES_DB=${var.database_name}"
31+
]
32+
33+
ports {
34+
internal = 5432
35+
external = var.port
36+
}
37+
38+
networks_advanced {
39+
name = docker_network.postgres_network.name
40+
}
41+
42+
restart = "always"
43+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "host" {
2+
value = var.host
3+
}
4+
5+
output "port" {
6+
value = var.port
7+
}
8+
9+
output "username" {
10+
value = var.username
11+
}
12+
13+
output "password" {
14+
value = var.password
15+
}
16+
17+
output "database_name" {
18+
value = var.database_name
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "host" {
2+
default = "localhost"
3+
}
4+
5+
variable "port" {
6+
default = "32768"
7+
}
8+
9+
variable "username" {
10+
default = "docker"
11+
}
12+
13+
variable "password" {
14+
default = "docker"
15+
}
16+
17+
variable "database_name" {
18+
default = "docker"
19+
}

go.mod

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ require (
3434
github.com/tmccombs/hcl2json v0.6.4
3535
github.com/urfave/cli v1.22.16
3636
github.com/zclconf/go-cty v1.15.0
37-
golang.org/x/crypto v0.31.0
37+
golang.org/x/crypto v0.29.0
3838
golang.org/x/net v0.31.0
3939
golang.org/x/oauth2 v0.24.0
4040
google.golang.org/api v0.206.0
@@ -46,9 +46,6 @@ require (
4646

4747
require (
4848
cloud.google.com/go/cloudbuild v1.19.0
49-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0
50-
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0
51-
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appcontainers/armappcontainers/v3 v3.0.0
5249
github.com/aws/aws-sdk-go-v2 v1.32.5
5350
github.com/aws/aws-sdk-go-v2/config v1.28.5
5451
github.com/aws/aws-sdk-go-v2/credentials v1.17.46
@@ -71,10 +68,12 @@ require (
7168
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.1
7269
github.com/aws/aws-sdk-go-v2/service/ssm v1.56.0
7370
github.com/aws/aws-sdk-go-v2/service/sts v1.33.1
71+
github.com/denisenkom/go-mssqldb v0.12.3
7472
github.com/gonvenience/ytbx v1.4.4
7573
github.com/hashicorp/go-getter/v2 v2.2.3
7674
github.com/homeport/dyff v1.6.0
7775
github.com/jackc/pgx/v5 v5.7.1
76+
github.com/lib/pq v1.10.9
7877
github.com/slack-go/slack v0.15.0
7978
gotest.tools/v3 v3.5.1
8079
)
@@ -88,14 +87,12 @@ require (
8887
cloud.google.com/go/longrunning v0.6.2 // indirect
8988
cloud.google.com/go/monitoring v1.21.2 // indirect
9089
filippo.io/edwards25519 v1.1.0 // indirect
91-
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
9290
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
9391
github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect
9492
github.com/Azure/go-autorest/autorest/azure/cli v0.4.2 // indirect
9593
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
9694
github.com/Azure/go-autorest/logger v0.2.1 // indirect
9795
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
98-
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
9996
github.com/BurntSushi/toml v1.4.0 // indirect
10097
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.24.1 // indirect
10198
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48.1 // indirect
@@ -139,7 +136,8 @@ require (
139136
github.com/go-openapi/jsonreference v0.20.2 // indirect
140137
github.com/go-openapi/swag v0.22.3 // indirect
141138
github.com/gogo/protobuf v1.3.2 // indirect
142-
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
139+
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
140+
github.com/golang-sql/sqlexp v0.1.0 // indirect
143141
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
144142
github.com/golang/protobuf v1.5.4 // indirect
145143
github.com/gonvenience/bunt v1.3.5 // indirect
@@ -165,7 +163,6 @@ require (
165163
github.com/josharian/intern v1.0.0 // indirect
166164
github.com/json-iterator/go v1.1.12 // indirect
167165
github.com/klauspost/compress v1.16.5 // indirect
168-
github.com/kylelemons/godebug v1.1.0 // indirect
169166
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
170167
github.com/mailru/easyjson v0.7.7 // indirect
171168
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 // indirect
@@ -180,7 +177,6 @@ require (
180177
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
181178
github.com/opencontainers/go-digest v1.0.0 // indirect
182179
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
183-
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
184180
github.com/pkg/errors v0.9.1 // indirect
185181
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
186182
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -202,10 +198,10 @@ require (
202198
go.opentelemetry.io/otel/sdk/metric v1.29.0 // indirect
203199
go.opentelemetry.io/otel/trace v1.29.0 // indirect
204200
golang.org/x/mod v0.18.0 // indirect
205-
golang.org/x/sync v0.10.0 // indirect
206-
golang.org/x/sys v0.28.0 // indirect
207-
golang.org/x/term v0.27.0 // indirect
208-
golang.org/x/text v0.21.0 // indirect
201+
golang.org/x/sync v0.9.0 // indirect
202+
golang.org/x/sys v0.27.0 // indirect
203+
golang.org/x/term v0.26.0 // indirect
204+
golang.org/x/text v0.20.0 // indirect
209205
golang.org/x/time v0.8.0 // indirect
210206
golang.org/x/tools v0.22.0 // indirect
211207
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 // indirect

0 commit comments

Comments
 (0)