Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to configure AWS Region with AWS IAM Auth #260

Merged
merged 3 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions postgresql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ func Provider() *schema.Provider {
Description: "AWS profile to use for IAM auth",
},

"aws_rds_iam_region": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "AWS region to use for IAM auth",
},

// Conection username can be different than database username with user name mapas (e.g.: in Azure)
// See https://www.postgresql.org/docs/current/auth-username-maps.html
"database_username": {
Expand Down Expand Up @@ -179,7 +186,7 @@ func validateExpectedVersion(v interface{}, key string) (warnings []string, erro
return
}

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

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

if profile != "" {
awscfg, err = awsConfig.LoadDefaultConfig(ctx, awsConfig.WithSharedConfigProfile(profile))
} else if region != "" {
awscfg, err = awsConfig.LoadDefaultConfig(ctx, awsConfig.WithRegion(region))
} else {
awscfg, err = awsConfig.LoadDefaultConfig(ctx)
}
Expand Down Expand Up @@ -221,8 +230,9 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
var password string
if d.Get("aws_rds_iam_auth").(bool) {
profile := d.Get("aws_rds_iam_profile").(string)
region := d.Get("aws_rds_iam_region").(string)
var err error
password, err = getRDSAuthToken(profile, username, host, port)
password, err = getRDSAuthToken(region, profile, username, host, port)
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ The following arguments are supported:
Version](https://www.postgresql.org/support/versioning/) or `current`. Once a
connection has been established, Terraform will fingerprint the actual
version. Default: `9.0.0`.
* `aws_rds_iam_auth` - (Optional) If set to `true`, call the AWS RDS API to grab a temporary password, using AWS Credentials
from the environment (or the given profile, see `aws_rds_iam_profile`)
* `aws_rds_iam_profile` - (Optional) The AWS IAM Profile to use while using AWS RDS IAM Auth.
* `aws_rds_iam_region` - (Optional) The AWS region to use while using AWS RDS IAM Auth.

## GoCloud

Expand Down