Skip to content

feat: add retry_policy to synthetics canary #42710

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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/42710.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_synthetics_canary: Add `retry_config` attribute
```
21 changes: 14 additions & 7 deletions internal/service/synthetics/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ func ResourceCanary() *schema.Resource {
return (new == "rate(0 minute)" || new == "rate(0 minutes)") && old == "rate(0 hour)"
},
},
"retry_config": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"max_retries": {
Type: schema.TypeInt,
Optional: false,
ValidateFunc: validation.IntBetween(0, 2),
},
},
},
},
},
},
},
Expand Down Expand Up @@ -316,7 +330,6 @@ func resourceCanaryCreate(ctx context.Context, d *schema.ResourceData, meta any)
}

output, err := conn.CreateCanary(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Synthetics Canary (%s): %s", name, err)
}
Expand Down Expand Up @@ -345,7 +358,6 @@ func resourceCanaryCreate(ctx context.Context, d *schema.ResourceData, meta any)
return false, err
},
)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Synthetics Canary (%s): waiting for completion: %s", name, err)
}
Expand Down Expand Up @@ -491,7 +503,6 @@ func resourceCanaryUpdate(ctx context.Context, d *schema.ResourceData, meta any)
}

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

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Synthetics Canary (%s): %s", d.Id(), err)
}
Expand Down Expand Up @@ -558,7 +569,6 @@ func resourceCanaryDelete(ctx context.Context, d *schema.ResourceData, meta any)
}

_, err = waitCanaryDeleted(ctx, conn, d.Id())

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting Synthetics Canary (%s): waiting for completion: %s", d.Id(), err)
}
Expand Down Expand Up @@ -793,13 +803,11 @@ func startCanary(ctx context.Context, name string, conn *synthetics.Client) erro
_, err := conn.StartCanary(ctx, &synthetics.StartCanaryInput{
Name: aws.String(name),
})

if err != nil {
return fmt.Errorf("starting Synthetics Canary: %w", err)
}

_, err = waitCanaryRunning(ctx, conn, name)

if err != nil {
return fmt.Errorf("starting Synthetics Canary: waiting for completion: %w", err)
}
Expand All @@ -822,7 +830,6 @@ func stopCanary(ctx context.Context, name string, conn *synthetics.Client) error
}

_, err = waitCanaryStopped(ctx, conn, name)

if err != nil {
return fmt.Errorf("stopping Synthetics Canary: waiting for completion: %w", err)
}
Expand Down
56 changes: 55 additions & 1 deletion internal/service/synthetics/canary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestAccSyntheticsCanary_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "vpc_config.#", "0"),
resource.TestCheckResourceAttr(resourceName, "schedule.0.duration_in_seconds", "0"),
resource.TestCheckResourceAttr(resourceName, "schedule.0.expression", "rate(0 hour)"),
resource.TestCheckNoResourceAttr("schedule.0", "retry_config"),
acctest.MatchResourceAttrRegionalARN(ctx, resourceName, "engine_arn", "lambda", regexache.MustCompile(fmt.Sprintf(`function:cwsyn-%s.+`, rName))),
acctest.MatchResourceAttrRegionalARN(ctx, resourceName, "source_location_arn", "lambda", regexache.MustCompile(fmt.Sprintf(`layer:cwsyn-%s.+`, rName))),
resource.TestCheckResourceAttrPair(resourceName, names.AttrExecutionRoleARN, "aws_iam_role.test", names.AttrARN),
Expand Down Expand Up @@ -584,6 +585,35 @@ func TestAccSyntheticsCanary_tags(t *testing.T) {
})
}

func TestAccSyntheticsCanary_retryConfig(t *testing.T) {
ctx := acctest.Context(t)
var conf awstypes.Canary
rName := fmt.Sprintf("tf-acc-test-%s", sdkacctest.RandString(8))
resourceName := "aws_synthetics_canary.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.SyntheticsServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckCanaryDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccCanaryConfig_retryConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCanaryExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "schedule.0.retry_config.0.max_retries", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda", "run_config.0.environment_variables"},
},
},
})
}

func TestAccSyntheticsCanary_disappears(t *testing.T) {
ctx := acctest.Context(t)
var conf awstypes.Canary
Expand Down Expand Up @@ -649,7 +679,6 @@ func testAccCheckCanaryExists(ctx context.Context, n string, canary *awstypes.Ca
conn := acctest.Provider.Meta().(*conns.AWSClient).SyntheticsClient(ctx)

output, err := tfsynthetics.FindCanaryByName(ctx, conn, rs.Primary.ID)

if err != nil {
return err
}
Expand Down Expand Up @@ -1312,3 +1341,28 @@ resource "aws_synthetics_canary" "test" {
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2))
}

func testAccCanaryConfig_retryConfig(rName string) string {
return acctest.ConfigCompose(
testAccCanaryConfig_base(rName),
fmt.Sprintf(`
resource "aws_synthetics_canary" "test" {
name = %[1]q
artifact_s3_location = "s3://${aws_s3_bucket.test.bucket}/"
execution_role_arn = aws_iam_role.test.arn
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = data.aws_synthetics_runtime_version.test.version_name
delete_lambda = true

schedule {
expression = "rate(0 minute)"
retry_config {
max_retries = 1
}
}

depends_on = [aws_iam_role.test, aws_iam_role_policy.test]
}
`, rName))
}
5 changes: 5 additions & 0 deletions website/docs/r/synthetics_canary.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The following arguments are optional:

* `expression` - (Required) Rate expression or cron expression that defines how often the canary is to run. For rate expression, the syntax is `rate(number unit)`. _unit_ can be `minute`, `minutes`, or `hour`. For cron expression, the syntax is `cron(expression)`. For more information about the syntax for cron expressions, see [Scheduling canary runs using cron](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries_cron.html).
* `duration_in_seconds` - (Optional) Duration in seconds, for the canary to continue making regular runs according to the schedule in the Expression value.
* `retry_config` - (Optional) The retry policy for the canary in case of failure.

### run_config

Expand All @@ -83,6 +84,10 @@ If this canary tests an endpoint in a VPC, this structure contains information a
* `subnet_ids` - (Required) IDs of the subnets where this canary is to run.
* `security_group_ids` - (Required) IDs of the security groups for this canary.

### retry_config

* `max_retries` - (Required) The maximum number of retries. The value must be less than or equal to 2.

## Attribute Reference

This resource exports the following attributes in addition to the arguments above:
Expand Down
Loading