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/synthetics_canary - add delete_lambda argument #25284

Merged
merged 3 commits into from
Jun 23, 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
3 changes: 3 additions & 0 deletions .changelog/25284.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_synthetics_canary: Add `delete_lambda` argument
```
8 changes: 7 additions & 1 deletion internal/service/synthetics/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func ResourceCanary() *schema.Resource {
return strings.TrimPrefix(new, "s3://") == old
},
},
"delete_lambda": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"engine_arn": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -549,7 +554,8 @@ func resourceCanaryDelete(d *schema.ResourceData, meta interface{}) error {

log.Printf("[DEBUG] Deleting Synthetics Canary: (%s)", d.Id())
_, err := conn.DeleteCanary(&synthetics.DeleteCanaryInput{
Name: aws.String(d.Id()),
Name: aws.String(d.Id()),
DeleteLambda: aws.Bool(d.Get("delete_lambda").(bool)),
})

if tfawserr.ErrCodeEquals(err, synthetics.ErrCodeResourceNotFoundException) {
Expand Down
105 changes: 31 additions & 74 deletions internal/service/synthetics/canary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ package synthetics_test
import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/aws/aws-sdk-go/service/synthetics"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -60,7 +56,7 @@ func TestAccSyntheticsCanary_basic(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda"},
},
{
Config: testAccCanaryConfig_zipUpdated(rName),
Expand Down Expand Up @@ -117,7 +113,7 @@ func TestAccSyntheticsCanary_artifactEncryption(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda"},
},
{
Config: testAccCanaryConfig_artifactEncryptionKMS(rName),
Expand Down Expand Up @@ -155,7 +151,7 @@ func TestAccSyntheticsCanary_runtimeVersion(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda"},
},
{
Config: testAccCanaryConfig_runtimeVersion(rName, "syn-nodejs-puppeteer-3.2"),
Expand Down Expand Up @@ -191,7 +187,7 @@ func TestAccSyntheticsCanary_startCanary(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda"},
},
{
Config: testAccCanaryConfig_start(rName, false),
Expand Down Expand Up @@ -240,7 +236,7 @@ func TestAccSyntheticsCanary_StartCanary_codeChanges(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda"},
},
{
Config: testAccCanaryConfig_startZipUpdated(rName, true),
Expand Down Expand Up @@ -295,7 +291,7 @@ func TestAccSyntheticsCanary_s3(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"s3_bucket", "s3_key", "s3_version", "start_canary"},
ImportStateVerifyIgnore: []string{"s3_bucket", "s3_key", "s3_version", "start_canary", "delete_lambda"},
},
},
})
Expand Down Expand Up @@ -325,7 +321,7 @@ func TestAccSyntheticsCanary_run(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda"},
},
{
Config: testAccCanaryConfig_run2(rName),
Expand Down Expand Up @@ -369,7 +365,7 @@ func TestAccSyntheticsCanary_runTracing(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda"},
},
{
Config: testAccCanaryConfig_runTracing(rName, false),
Expand Down Expand Up @@ -404,30 +400,25 @@ func TestAccSyntheticsCanary_runEnvironmentVariables(t *testing.T) {
Config: testAccCanaryConfig_runEnvVariables1(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCanaryExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "run_config.0.environment_variables.%", "1"),
resource.TestCheckResourceAttr(resourceName, "run_config.0.environment_variables.test1", "result1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "run_config.0.environment_variables"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda", "run_config.0.environment_variables"},
},
{
Config: testAccCanaryConfig_runEnvVariables2(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCanaryExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "run_config.0.environment_variables.%", "2"),
resource.TestCheckResourceAttr(resourceName, "run_config.0.environment_variables.test1", "result1"),
resource.TestCheckResourceAttr(resourceName, "run_config.0.environment_variables.test2", "result2"),
),
},
{
Config: testAccCanaryConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCanaryExists(resourceName, &conf),
resource.TestCheckNoResourceAttr(resourceName, "run_config.0.environment_variables"),
),
},
},
})
}
Expand Down Expand Up @@ -460,7 +451,7 @@ func TestAccSyntheticsCanary_vpc(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda"},
},
{
Config: testAccCanaryConfig_vpc2(rName),
Expand All @@ -478,7 +469,6 @@ func TestAccSyntheticsCanary_vpc(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "vpc_config.0.subnet_ids.#", "1"),
resource.TestCheckResourceAttr(resourceName, "vpc_config.0.security_group_ids.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.vpc_id", "aws_vpc.test", "id"),
testAccCheckCanaryDeleteImplicitResources(resourceName),
),
},
},
Expand Down Expand Up @@ -508,7 +498,7 @@ func TestAccSyntheticsCanary_tags(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "delete_lambda"},
},
{
Config: testAccCanaryConfig_tags2(rName, "key1", "value1updated", "key2", "value2"),
Expand Down Expand Up @@ -604,57 +594,6 @@ func testAccCheckCanaryExists(n string, canary *synthetics.Canary) resource.Test
}
}

func testAccCheckCanaryDeleteImplicitResources(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("synthetics Canary not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("synthetics Canary name not set")
}

lambdaConn := acctest.Provider.Meta().(*conns.AWSClient).LambdaConn

layerArn := rs.Primary.Attributes["source_location_arn"]
layerArnObj, err := arn.Parse(layerArn)
if err != nil {
return fmt.Errorf("synthetics Canary Lambda Layer %s incorrect arn format: %w", layerArn, err)
}

layerName := strings.Split(layerArnObj.Resource, ":")

deleteLayerVersionInput := &lambda.DeleteLayerVersionInput{
LayerName: aws.String(layerName[1]),
VersionNumber: aws.Int64(1),
}

_, err = lambdaConn.DeleteLayerVersion(deleteLayerVersionInput)
if err != nil {
return fmt.Errorf("synthetics Canary Lambda Layer %s could not be deleted: %w", layerArn, err)
}

lambdaArn := rs.Primary.Attributes["engine_arn"]
lambdaArnObj, err := arn.Parse(layerArn)
if err != nil {
return fmt.Errorf("synthetics Canary Lambda %s incorrect arn format: %w", lambdaArn, err)
}
lambdaArnParts := strings.Split(lambdaArnObj.Resource, ":")

deleteLambdaInput := &lambda.DeleteFunctionInput{
FunctionName: aws.String(lambdaArnParts[1]),
}

_, err = lambdaConn.DeleteFunction(deleteLambdaInput)
if err != nil {
return fmt.Errorf("synthetics Canary Lambda %s could not be deleted: %w", lambdaArn, err)
}

return nil
}
}

func testAccCheckCanaryIsUpdated(first, second *synthetics.Canary) resource.TestCheckFunc {
return func(s *terraform.State) error {
if !second.Timeline.LastModified.After(*first.Timeline.LastModified) {
Expand Down Expand Up @@ -798,6 +737,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -821,6 +761,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -845,6 +786,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -869,6 +811,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -894,6 +837,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand Down Expand Up @@ -923,6 +867,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -940,6 +885,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.3"
delete_lambda = true

artifact_config {
s3_encryption {
Expand Down Expand Up @@ -970,6 +916,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.3"
delete_lambda = true

artifact_config {
s3_encryption {
Expand All @@ -996,6 +943,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = %[2]q
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -1015,6 +963,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest_modified.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -1035,6 +984,7 @@ resource "aws_synthetics_canary" "test" {
zip_file = "test-fixtures/lambdatest.zip"
start_canary = %[2]t
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -1055,6 +1005,7 @@ resource "aws_synthetics_canary" "test" {
zip_file = "test-fixtures/lambdatest_modified.zip"
start_canary = %[2]t
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -1076,6 +1027,7 @@ resource "aws_synthetics_canary" "test" {
s3_key = aws_s3_object.test.key
s3_version = aws_s3_object.test.version_id
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand Down Expand Up @@ -1162,6 +1114,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand Down Expand Up @@ -1189,6 +1142,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand Down Expand Up @@ -1216,6 +1170,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -1240,6 +1195,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand All @@ -1261,6 +1217,7 @@ resource "aws_synthetics_canary" "test" {
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
delete_lambda = true

schedule {
expression = "rate(0 minute)"
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/synthetics_canary.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The following arguments are required:

The following arguments are optional:

* `delete_lambda` - (Optional) Specifies whether to also delete the Lambda functions and layers used by this canary. The default is `false`.
* `vpc_config` - (Optional) Configuration block. Detailed below.
* `failure_retention_period` - (Optional) Number of days to retain data about failed runs of this canary. If you omit this field, the default of 31 days is used. The valid range is 1 to 455 days.
* `run_config` - (Optional) Configuration block for individual canary runs. Detailed below.
Expand Down