Skip to content

Commit 68b8be8

Browse files
authored
Allow to configure AWS Region with AWS IAM Auth (#260)
1 parent 654544b commit 68b8be8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

postgresql/provider.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ func Provider() *schema.Provider {
7878
Description: "AWS profile to use for IAM auth",
7979
},
8080

81+
"aws_rds_iam_region": {
82+
Type: schema.TypeString,
83+
Optional: true,
84+
Default: "",
85+
Description: "AWS region to use for IAM auth",
86+
},
87+
8188
// Conection username can be different than database username with user name mapas (e.g.: in Azure)
8289
// See https://www.postgresql.org/docs/current/auth-username-maps.html
8390
"database_username": {
@@ -179,7 +186,7 @@ func validateExpectedVersion(v interface{}, key string) (warnings []string, erro
179186
return
180187
}
181188

182-
func getRDSAuthToken(profile string, username string, host string, port int) (string, error) {
189+
func getRDSAuthToken(region string, profile string, username string, host string, port int) (string, error) {
183190
endpoint := fmt.Sprintf("%s:%d", host, port)
184191

185192
ctx := context.Background()
@@ -189,6 +196,8 @@ func getRDSAuthToken(profile string, username string, host string, port int) (st
189196

190197
if profile != "" {
191198
awscfg, err = awsConfig.LoadDefaultConfig(ctx, awsConfig.WithSharedConfigProfile(profile))
199+
} else if region != "" {
200+
awscfg, err = awsConfig.LoadDefaultConfig(ctx, awsConfig.WithRegion(region))
192201
} else {
193202
awscfg, err = awsConfig.LoadDefaultConfig(ctx)
194203
}
@@ -221,8 +230,9 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
221230
var password string
222231
if d.Get("aws_rds_iam_auth").(bool) {
223232
profile := d.Get("aws_rds_iam_profile").(string)
233+
region := d.Get("aws_rds_iam_region").(string)
224234
var err error
225-
password, err = getRDSAuthToken(profile, username, host, port)
235+
password, err = getRDSAuthToken(region, profile, username, host, port)
226236
if err != nil {
227237
return nil, err
228238
}

website/docs/index.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ The following arguments are supported:
111111
Version](https://www.postgresql.org/support/versioning/) or `current`. Once a
112112
connection has been established, Terraform will fingerprint the actual
113113
version. Default: `9.0.0`.
114+
* `aws_rds_iam_auth` - (Optional) If set to `true`, call the AWS RDS API to grab a temporary password, using AWS Credentials
115+
from the environment (or the given profile, see `aws_rds_iam_profile`)
116+
* `aws_rds_iam_profile` - (Optional) The AWS IAM Profile to use while using AWS RDS IAM Auth.
117+
* `aws_rds_iam_region` - (Optional) The AWS region to use while using AWS RDS IAM Auth.
114118

115119
## GoCloud
116120

0 commit comments

Comments
 (0)