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_lambda_provisioned_concurrency_configuration: add skip_destroy argument #31646

Merged
merged 3 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/31646.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_lambda_provisioned_concurrency_configuration: Add `skip_destroy` argument
```
1 change: 1 addition & 0 deletions docs/acc-test-environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Environment variables (beyond standard AWS Go SDK ones) used by acceptance testi
| `SERVICEQUOTAS_INCREASE_ON_CREATE_VALUE` | Value of quota increase for Service Quotas testing (submits support case). |
| `SES_DOMAIN_IDENTITY_ROOT_DOMAIN` | Root domain name of publicly accessible and Route 53 configurable domain for SES Domain Identity testing. |
| `SES_DEDICATED_IP` | Dedicated IP address for testing IP assignment with a "Standard" (non-managed) SES dedicated IP pool. |
| `SKIP_DESTROY_ENABLED` | Enable tests which skip resource destruction (must be manually destroyed instead). |
| `SWF_DOMAIN_TESTING_ENABLED` | Enables SWF Domain testing (API does not support deletions). |
| `TEST_AWS_ORGANIZATION_ACCOUNT_EMAIL_DOMAIN` | Email address for Organizations Account testing. |
| `TEST_AWS_SES_VERIFIED_EMAIL_ARN` | Verified SES Email Identity for use in Cognito User Pool testing. |
Expand Down
10 changes: 10 additions & 0 deletions internal/service/lambda/provisioned_concurrency_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func ResourceProvisionedConcurrencyConfig() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"skip_destroy": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
Expand Down Expand Up @@ -147,6 +152,11 @@ func resourceProvisionedConcurrencyConfigUpdate(ctx context.Context, d *schema.R

func resourceProvisionedConcurrencyConfigDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
if v, ok := d.GetOk("skip_destroy"); ok && v.(bool) {
log.Printf("[DEBUG] Retaining Lambda Provisioned Concurrency Config %q", d.Id())
return diags
}

conn := meta.(*conns.AWSClient).LambdaConn()

functionName, qualifier, err := ProvisionedConcurrencyConfigParseID(d.Id())
Expand Down
145 changes: 128 additions & 17 deletions internal/service/lambda/provisioned_concurrency_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -31,18 +32,20 @@ func TestAccLambdaProvisionedConcurrencyConfig_basic(t *testing.T) {
CheckDestroy: testAccCheckProvisionedConcurrencyConfigDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccProvisionedConcurrencyConfigConfig_qualifierFunctionVersion(rName),
Config: testAccProvisionedConcurrencyConfigConfig_concurrentExecutions(rName, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckProvisionedConcurrencyExistsConfig(ctx, resourceName),
resource.TestCheckResourceAttrPair(resourceName, "function_name", lambdaFunctionResourceName, "function_name"),
resource.TestCheckResourceAttr(resourceName, "provisioned_concurrent_executions", "1"),
resource.TestCheckResourceAttrPair(resourceName, "qualifier", lambdaFunctionResourceName, "version"),
resource.TestCheckResourceAttr(resourceName, "skip_destroy", "false"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"skip_destroy"},
},
},
})
Expand Down Expand Up @@ -122,9 +125,10 @@ func TestAccLambdaProvisionedConcurrencyConfig_provisionedConcurrentExecutions(t
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"skip_destroy"},
},
{
Config: testAccProvisionedConcurrencyConfigConfig_concurrentExecutions(rName, 2),
Expand Down Expand Up @@ -159,9 +163,40 @@ func TestAccLambdaProvisionedConcurrencyConfig_Qualifier_aliasName(t *testing.T)
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"skip_destroy"},
},
},
})
}

func TestAccLambdaProvisionedConcurrencyConfig_skipDestroy(t *testing.T) {
ctx := acctest.Context(t)
if os.Getenv("SKIP_DESTROY_ENABLED") == "" {
t.Skip("Environment variable SKIP_DESTROY_ENABLED is not set")
}

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
lambdaFunctionResourceName := "aws_lambda_function.test"
resourceName := "aws_lambda_provisioned_concurrency_config.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.LambdaEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckProvisionedConcurrencyConfigNoDestroy(ctx),
Steps: []resource.TestStep{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could test this without the environment variable by using the following test steps, which would also test the originally reported problem

  1. Create
  2. Publish a new version of the function, but validate that the old version of the function still has concurrency set
  3. Delete if from the new version, and validate that it's still set on the new version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion - I've updated to this pattern and we shouldn't have any dangling resources leftover now.

{
Config: testAccProvisionedConcurrencyConfigConfig_skipDestroy(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckProvisionedConcurrencyExistsConfig(ctx, resourceName),
resource.TestCheckResourceAttrPair(resourceName, "function_name", lambdaFunctionResourceName, "function_name"),
resource.TestCheckResourceAttr(resourceName, "provisioned_concurrent_executions", "1"),
resource.TestCheckResourceAttrPair(resourceName, "qualifier", lambdaFunctionResourceName, "version"),
resource.TestCheckResourceAttr(resourceName, "skip_destroy", "true"),
),
},
},
})
Expand Down Expand Up @@ -209,6 +244,34 @@ func testAccCheckProvisionedConcurrencyConfigDestroy(ctx context.Context) resour
}
}

func testAccCheckProvisionedConcurrencyConfigNoDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).LambdaClient()

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_lambda_provisioned_concurrency_config" {
continue
}

functionName, qualifier, err := tflambda.ProvisionedConcurrencyConfigParseID(rs.Primary.ID)

if err != nil {
return err
}

input := &lambda.GetProvisionedConcurrencyConfigInput{
FunctionName: aws.String(functionName),
Qualifier: aws.String(qualifier),
}

_, err = conn.GetProvisionedConcurrencyConfig(ctx, input)
return err
}

return nil
}
}

func testAccCheckProvisionedConcurrencyDisappearsConfig(ctx context.Context, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down Expand Up @@ -320,17 +383,22 @@ resource "aws_lambda_function" "test" {
}

func testAccProvisionedConcurrencyConfigConfig_concurrentExecutions(rName string, provisionedConcurrentExecutions int) string {
return testAccProvisionedConcurrencyConfig_base(rName) + fmt.Sprintf(`
return acctest.ConfigCompose(
testAccProvisionedConcurrencyConfig_base(rName),
fmt.Sprintf(`
resource "aws_lambda_provisioned_concurrency_config" "test" {
function_name = aws_lambda_function.test.function_name
provisioned_concurrent_executions = %[1]d
qualifier = aws_lambda_function.test.version
}
`, provisionedConcurrentExecutions)
`, provisionedConcurrentExecutions),
)
}

func testAccProvisionedConcurrencyConfigConfig_qualifierAliasName(rName string) string {
return testAccProvisionedConcurrencyConfig_base(rName) + `
return acctest.ConfigCompose(
testAccProvisionedConcurrencyConfig_base(rName),
`
resource "aws_lambda_alias" "test" {
function_name = aws_lambda_function.test.function_name
function_version = aws_lambda_function.test.version
Expand All @@ -342,15 +410,58 @@ resource "aws_lambda_provisioned_concurrency_config" "test" {
provisioned_concurrent_executions = 1
qualifier = aws_lambda_alias.test.name
}
`
`,
)
}

func testAccProvisionedConcurrencyConfigConfig_skipDestroy(rName string, skipDestroy bool) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}

resource "aws_iam_role" "test" {
name = %[1]q

assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
POLICY
}

resource "aws_iam_role_policy_attachment" "test" {
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
role = aws_iam_role.test.id
}

resource "aws_lambda_function" "test" {
depends_on = [aws_iam_role_policy_attachment.test]

filename = "test-fixtures/lambdapinpoint.zip"
function_name = %[1]q
role = aws_iam_role.test.arn
handler = "lambdapinpoint.handler"
publish = true
runtime = "nodejs16.x"

skip_destroy = %[2]t
}

func testAccProvisionedConcurrencyConfigConfig_qualifierFunctionVersion(rName string) string {
return testAccProvisionedConcurrencyConfig_base(rName) + `
resource "aws_lambda_provisioned_concurrency_config" "test" {
function_name = aws_lambda_function.test.function_name
provisioned_concurrent_executions = 1
qualifier = aws_lambda_function.test.version

skip_destroy = %[2]t
}
`
`, rName, skipDestroy)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ description: |-

Manages a Lambda Provisioned Concurrency Configuration.

~> **NOTE:** Setting `skip_destroy` to `true` means that the AWS Provider will _not_ destroy a provisioned concurrency configuration, even when running `terraform destroy`. The configuration is thus an intentional dangling resource that is _not_ managed by Terraform and may incur extra expense in your AWS account.

## Example Usage

### Alias Name
Expand Down Expand Up @@ -40,6 +42,10 @@ The following arguments are required:
* `provisioned_concurrent_executions` - (Required) Amount of capacity to allocate. Must be greater than or equal to `1`.
* `qualifier` - (Required) Lambda Function version or Lambda Alias name.

The following arguments are optional:

* `skip_destroy` - (Optional) Whether to retain the provisoned concurrency configuration upon destruction. Defaults to `false`. If set to `true`, the resource in simply removed from state instead.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:
Expand Down