diff --git a/aws/resource_aws_lambda_function.go b/aws/resource_aws_lambda_function.go index d3f7ca7e29c..44cb88241a5 100644 --- a/aws/resource_aws_lambda_function.go +++ b/aws/resource_aws_lambda_function.go @@ -1275,6 +1275,20 @@ func waitForLambdaFunctionUpdate(conn *lambda.Lambda, functionName string, timeo return err } +func flattenLambdaEnvironment(apiObject *lambda.EnvironmentResponse) []interface{} { + if apiObject == nil { + return nil + } + + tfMap := map[string]interface{}{} + + if v := apiObject.Variables; v != nil { + tfMap["variables"] = aws.StringValueMap(v) + } + + return []interface{}{tfMap} +} + func flattenLambdaFileSystemConfigs(fscList []*lambda.FileSystemConfig) []map[string]interface{} { results := make([]map[string]interface{}, 0, len(fscList)) for _, fsc := range fscList { diff --git a/aws/resource_aws_lambda_function_test.go b/aws/resource_aws_lambda_function_test.go index a2c9e2c0936..262dce47fc6 100644 --- a/aws/resource_aws_lambda_function_test.go +++ b/aws/resource_aws_lambda_function_test.go @@ -416,6 +416,33 @@ func TestAccAWSLambdaFunction_envVariables(t *testing.T) { }) } +func TestAccAWSLambdaFunction_Environment_Variables_NoValue(t *testing.T) { + var conf lambda.GetFunctionOutput + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_lambda_function.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLambdaFunctionDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLambdaConfigEnvironmentVariablesNoValue(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaFunctionExists(resourceName, rName, &conf), + resource.TestCheckResourceAttr(resourceName, "environment.0.variables.key1", ""), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"filename", "publish"}, + }, + }, + }) +} + func TestAccAWSLambdaFunction_encryptedEnvVariables(t *testing.T) { var conf lambda.GetFunctionOutput @@ -2241,6 +2268,26 @@ resource "aws_lambda_function" "test" { `, funcName) } +func testAccAWSLambdaConfigEnvironmentVariablesNoValue(rName string) string { + return composeConfig( + baseAccAWSLambdaConfig(rName, rName, rName), + fmt.Sprintf(` +resource "aws_lambda_function" "test" { + filename = "test-fixtures/lambdatest.zip" + function_name = %[1]q + handler = "exports.example" + role = aws_iam_role.iam_for_lambda.arn + runtime = "nodejs12.x" + + environment { + variables = { + key1 = "" + } + } +} +`, rName)) +} + func testAccAWSLambdaConfigEncryptedEnvVariables(keyDesc, funcName, policyName, roleName, sgName string) string { return fmt.Sprintf(baseAccAWSLambdaConfig(policyName, roleName, sgName)+` resource "aws_kms_key" "foo" { diff --git a/aws/structure.go b/aws/structure.go index 10a7f881773..ee109b380bf 100644 --- a/aws/structure.go +++ b/aws/structure.go @@ -1622,24 +1622,6 @@ func flattenDSVpcSettings( return []map[string]interface{}{settings} } -func flattenLambdaEnvironment(lambdaEnv *lambda.EnvironmentResponse) []interface{} { - envs := make(map[string]interface{}) - en := make(map[string]string) - - if lambdaEnv == nil { - return nil - } - - for k, v := range lambdaEnv.Variables { - en[k] = *v - } - if len(en) > 0 { - envs["variables"] = en - } - - return []interface{}{envs} -} - func expandLambdaEventSourceMappingDestinationConfig(vDest []interface{}) *lambda.DestinationConfig { if len(vDest) == 0 { return nil