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

r/aws_secretsmanager_secret_rotation: support managed secrets #34180

Merged
merged 3 commits into from
Nov 1, 2023
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
3 changes: 3 additions & 0 deletions .changelog/34180.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_secretsmanager_secret_rotation: The `rotation_lambda_arn` argument is now optional to support modifying the rotation schedule of AWS-managed secrets.
```
76 changes: 35 additions & 41 deletions internal/service/secretsmanager/secret_rotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

// @SDKResource("aws_secretsmanager_secret_rotation")
Expand All @@ -37,8 +38,9 @@ func ResourceSecretRotation() *schema.Resource {
Computed: true,
},
"rotation_lambda_arn": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidARN,
},
"rotation_rules": {
Type: schema.TypeList,
Expand Down Expand Up @@ -87,25 +89,26 @@ func resourceSecretRotationCreate(ctx context.Context, d *schema.ResourceData, m
conn := meta.(*conns.AWSClient).SecretsManagerConn(ctx)
secretID := d.Get("secret_id").(string)

if v, ok := d.GetOk("rotation_lambda_arn"); ok && v.(string) != "" {
input := &secretsmanager.RotateSecretInput{
RotationLambdaARN: aws.String(v.(string)),
RotationRules: expandRotationRules(d.Get("rotation_rules").([]interface{})),
SecretId: aws.String(secretID),
}
input := &secretsmanager.RotateSecretInput{
RotationRules: expandRotationRules(d.Get("rotation_rules").([]interface{})),
SecretId: aws.String(secretID),
}

// AccessDeniedException: Secrets Manager cannot invoke the specified Lambda function.
outputRaw, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, 1*time.Minute, func() (interface{}, error) {
return conn.RotateSecretWithContext(ctx, input)
}, "AccessDeniedException")
if v, ok := d.GetOk("rotation_lambda_arn"); ok {
input.RotationLambdaARN = aws.String(v.(string))
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Secrets Manager Secret Rotation (%s): %s", secretID, err)
}
// AccessDeniedException: Secrets Manager cannot invoke the specified Lambda function.
outputRaw, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, 1*time.Minute, func() (interface{}, error) {
return conn.RotateSecretWithContext(ctx, input)
}, "AccessDeniedException")

d.SetId(aws.StringValue(outputRaw.(*secretsmanager.RotateSecretOutput).ARN))
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Secrets Manager Secret Rotation (%s): %s", secretID, err)
}

d.SetId(aws.StringValue(outputRaw.(*secretsmanager.RotateSecretOutput).ARN))

return append(diags, resourceSecretRotationRead(ctx, d, meta)...)
}

Expand Down Expand Up @@ -150,31 +153,22 @@ func resourceSecretRotationUpdate(ctx context.Context, d *schema.ResourceData, m
secretID := d.Get("secret_id").(string)

if d.HasChanges("rotation_lambda_arn", "rotation_rules") {
if v, ok := d.GetOk("rotation_lambda_arn"); ok && v.(string) != "" {
input := &secretsmanager.RotateSecretInput{
RotationLambdaARN: aws.String(v.(string)),
RotationRules: expandRotationRules(d.Get("rotation_rules").([]interface{})),
SecretId: aws.String(secretID),
}

// AccessDeniedException: Secrets Manager cannot invoke the specified Lambda function.
_, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, 1*time.Minute, func() (interface{}, error) {
return conn.RotateSecretWithContext(ctx, input)
}, "AccessDeniedException")

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Secrets Manager Secret Rotation (%s): %s", d.Id(), err)
}
} else {
input := &secretsmanager.CancelRotateSecretInput{
SecretId: aws.String(d.Id()),
}

_, err := conn.CancelRotateSecretWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "cancelling Secrets Manager Secret Rotation (%s): %s", d.Id(), err)
}
input := &secretsmanager.RotateSecretInput{
RotationRules: expandRotationRules(d.Get("rotation_rules").([]interface{})),
SecretId: aws.String(secretID),
}

if v, ok := d.GetOk("rotation_lambda_arn"); ok {
input.RotationLambdaARN = aws.String(v.(string))
}

// AccessDeniedException: Secrets Manager cannot invoke the specified Lambda function.
_, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, 1*time.Minute, func() (interface{}, error) {
return conn.RotateSecretWithContext(ctx, input)
}, "AccessDeniedException")

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Secrets Manager Secret Rotation (%s): %s", d.Id(), err)
}
}

Expand Down
Loading
Loading