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

Fix updates on empty env vars in aws_lambda_func #29839

Merged
merged 1 commit into from
Mar 10, 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/29839.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_lambda_function: Fix empty environment variable update
```
11 changes: 11 additions & 0 deletions internal/service/lambda/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ func ResourceFunction() *schema.Resource {
},
},
},
// Suppress diff if change is to an empty list
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "0" && new == "1" {
_, n := d.GetChange("environment.0.variables")
newn, ok := n.(map[string]interface{})
if ok && len(newn) == 0 {
return true
}
}
return false
},
},
"ephemeral_storage": {
Type: schema.TypeList,
Expand Down
64 changes: 64 additions & 0 deletions internal/service/lambda/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,52 @@ func TestAccLambdaFunction_EnvironmentVariables_noValue(t *testing.T) {
})
}

func TestAccLambdaFunction_EnvironmentVariables_emptyUpgrade(t *testing.T) {
ctx := acctest.Context(t)
var conf lambda.GetFunctionOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_lambda_function.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.LambdaEndpointID),
CheckDestroy: testAccCheckFunctionDestroy(ctx),
Steps: []resource.TestStep{
{
ExternalProviders: map[string]resource.ExternalProvider{
"aws": {
Source: "hashicorp/aws",
VersionConstraint: "4.56.0",
},
},
Config: testAccFunctionConfig_EmptyEnv_envVariables(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckFunctionExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "environment.#", "0"),
),
// We know a persistent diff is present in this version
ExpectNonEmptyPlan: true,
},
{
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Config: testAccFunctionConfig_EmptyEnv_envVariables(rName),
ResourceName: resourceName,
// Newer versions of the provider should suppress the diff without requiring an apply
ExpectNonEmptyPlan: false,
PlanOnly: true,
},
{
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Config: testAccFunctionConfig_envVariables(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckFunctionExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "environment.0.variables.foo", "bar"),
),
},
},
})
}

func TestAccLambdaFunction_encryptedEnvVariables(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
Expand Down Expand Up @@ -2665,6 +2711,24 @@ resource "aws_lambda_function" "test" {
`, rName))
}

func testAccFunctionConfig_EmptyEnv_envVariables(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLambdaBase(rName, rName, rName),
fmt.Sprintf(`
resource "aws_lambda_function" "test" {
filename = "test-fixtures/lambdatest.zip"
function_name = %[1]q
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.example"
runtime = "nodejs16.x"

environment {
variables = {}
}
}
`, rName))
}

func testAccFunctionConfig_envVariablesModified(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLambdaBase(rName, rName, rName),
Expand Down