-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
resource/aws_db_instance: Allow ARN for the replicate_source_db when in the same region #2386
Changes from all commits
fc9b9eb
90ab3b8
bb7aea7
25c3f37
a2e822f
55fa93e
c84c16d
f4ad1c6
20c3f8d
e0a390f
691a18b
0cd9a52
bcb2e50
c558651
bafedcd
86e6321
ccc6f0b
4b34d4e
bd9d42c
9919c30
0dd23da
65737f1
06a8353
5b0b146
038f0f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/rds" | ||
|
||
|
@@ -387,6 +388,7 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error | |
PubliclyAccessible: aws.Bool(d.Get("publicly_accessible").(bool)), | ||
Tags: tags, | ||
} | ||
|
||
if attr, ok := d.GetOk("iops"); ok { | ||
opts.Iops = aws.Int64(int64(attr.(int))) | ||
} | ||
|
@@ -399,6 +401,34 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error | |
opts.AvailabilityZone = aws.String(attr.(string)) | ||
} | ||
|
||
// | ||
// If we are called with a Source DB ARN, and the ARN is a different region | ||
// than the replica to be created, set SourceRegion. | ||
// | ||
// The correct way to do this would be to query the master, and see if it | ||
// is encrypted and in the same region. If it is encrypted and in the | ||
// same region, drop the source region and the kms_key_id. If the master is not | ||
// encrypted, behavior is kinda undefined. | ||
// | ||
// The CLI docs for kms_key_id state: | ||
// "If you specify this parameter when you create a Read Replica from an | ||
// unencrypted DB instance, the Read Replica is encrypted."" | ||
// | ||
// The RDS userguide states: | ||
// "You cannot have an encrypted Read Replica of an unencrypted DB instance | ||
// or an unencrypted Read Replica of an encrypted DB instance." | ||
// | ||
// go figure, eh? | ||
// | ||
replicaRegion := meta.(*AWSClient).region | ||
|
||
arnParts, arnErr := arn.Parse(d.Get("replicate_source_db").(string)) | ||
if arnErr == nil { | ||
if arnParts.Region != replicaRegion { | ||
opts.SourceRegion = aws.String(arnParts.Region) | ||
} | ||
} | ||
|
||
if attr, ok := d.GetOk("storage_type"); ok { | ||
opts.StorageType = aws.String(attr.(string)) | ||
} | ||
|
@@ -407,11 +437,11 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error | |
opts.DBSubnetGroupName = aws.String(attr.(string)) | ||
} | ||
|
||
// TODO: Only allow this param if the master is not encrypted or | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still an outstanding concern? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jen20 I wrote that based on the ambiguity in the AWS documentation I describe above. When creating a replica, they seem to have two situations where they allow an encryption key to be specified for a replica. When creating an encrypted replica in the same region as the master, you cannot specify the key. I had thought that maybe we should show an error if a key was specified for a same-region replica. However, maybe it is best to let the AWS API throw an error if this param is used when it is not allowed. Unless you have advice on which way to go, I can take those lines out. The PR is working as-is for us. |
||
// is in a different region than the replica | ||
|
||
if attr, ok := d.GetOk("kms_key_id"); ok { | ||
opts.KmsKeyId = aws.String(attr.(string)) | ||
if arnParts := strings.Split(v.(string), ":"); len(arnParts) >= 4 { | ||
opts.SourceRegion = aws.String(arnParts[3]) | ||
} | ||
} | ||
|
||
if attr, ok := d.GetOk("monitoring_role_arn"); ok { | ||
|
@@ -777,21 +807,21 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error { | |
// list tags for resource | ||
// set tags | ||
conn := meta.(*AWSClient).rdsconn | ||
arn, err := buildRDSARN(d.Id(), meta.(*AWSClient).partition, meta.(*AWSClient).accountid, meta.(*AWSClient).region) | ||
builtArn, err := buildRDSARN(d.Id(), meta.(*AWSClient).partition, meta.(*AWSClient).accountid, meta.(*AWSClient).region) | ||
if err != nil { | ||
name := "<empty>" | ||
if v.DBName != nil && *v.DBName != "" { | ||
name = *v.DBName | ||
} | ||
log.Printf("[DEBUG] Error building ARN for DB Instance, not setting Tags for DB %s", name) | ||
} else { | ||
d.Set("arn", arn) | ||
d.Set("arn", builtArn) | ||
resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{ | ||
ResourceName: aws.String(arn), | ||
ResourceName: aws.String(builtArn), | ||
}) | ||
|
||
if err != nil { | ||
log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn) | ||
log.Printf("[DEBUG] Error retrieving tags for ARN: %s", builtArn) | ||
} | ||
|
||
var dt []*rds.Tag | ||
|
@@ -828,7 +858,13 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error { | |
return fmt.Errorf("[DEBUG] Error setting replicas attribute: %#v, error: %#v", replicas, err) | ||
} | ||
|
||
d.Set("replicate_source_db", v.ReadReplicaSourceDBInstanceIdentifier) | ||
// If an ARN was passed in, do NOT use what AWS passes back for replicate_source_id, | ||
// as it passes back the master's ID- | ||
// see https://github.com/terraform-providers/terraform-provider-aws/issues/2399 | ||
_, arnErr := arn.Parse(d.Get("replicate_source_db").(string)) | ||
if arnErr != nil { | ||
d.Set("replicate_source_db", v.ReadReplicaSourceDBInstanceIdentifier) | ||
} | ||
|
||
d.Set("ca_cert_identifier", v.CACertificateIdentifier) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for the block comment explaining the rationale here.