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

resource/aws_lambda_function: Prevent crash with missing environment variable value #17056

Merged
merged 1 commit into from
Jan 20, 2021
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
14 changes: 14 additions & 0 deletions aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
47 changes: 47 additions & 0 deletions aws/resource_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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" {
Expand Down
18 changes: 0 additions & 18 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down