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 lambda function Zip type attribute checks #20575

Merged
merged 3 commits into from
Aug 17, 2021
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/20575.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_lambda_function: Fix `handler`, `runtime` attribute validation for `package_type` is `Zip`
```
2 changes: 1 addition & 1 deletion aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func checkHandlerRuntimeForZipFunction(_ context.Context, d *schema.ResourceDiff
_, handlerOk := d.GetOk("handler")
_, runtimeOk := d.GetOk("runtime")

if packageType == lambda.PackageTypeZip && !handlerOk && !runtimeOk {
if packageType == lambda.PackageTypeZip && (!handlerOk || !runtimeOk) {
return fmt.Errorf("handler and runtime must be set when PackageType is Zip")
}
return nil
Expand Down
45 changes: 45 additions & 0 deletions aws/resource_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,31 @@ func TestAccAWSLambdaFunction_runtimes(t *testing.T) {
})
}

func TestAccAWSLambdaFunction_zip_validation(t *testing.T) {
rString := acctest.RandString(8)
funcName := fmt.Sprintf("tf_acc_lambda_func_expect_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_lambda_func_expect_%s", rString)
roleName := fmt.Sprintf("tf_acc_role_lambda_func_expect_%s", rString)
sgName := fmt.Sprintf("tf_acc_sg_lambda_func_expect_%s", rString)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, lambda.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaFunctionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLambdaZipConfigWithoutHandler(funcName, policyName, roleName, sgName),
ExpectError: regexp.MustCompile("handler and runtime must be set when PackageType is Zip"),
},
{
Config: testAccAWSLambdaZipConfigWithoutRuntime(funcName, policyName, roleName, sgName),
ExpectError: regexp.MustCompile("handler and runtime must be set when PackageType is Zip"),
},
},
})
}

func testAccCheckLambdaFunctionDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).lambdaconn

Expand Down Expand Up @@ -3305,6 +3330,26 @@ resource "aws_lambda_function" "test" {
`, rName, runtime))
}

func testAccAWSLambdaZipConfigWithoutHandler(funcName, policyName, roleName, sgName string) string {
return fmt.Sprintf(baseAccAWSLambdaConfig(policyName, roleName, sgName)+`
resource "aws_lambda_function" "test" {
function_name = "%s"
role = aws_iam_role.iam_for_lambda.arn
runtime = "nodejs12.x"
}
`, funcName)
}

func testAccAWSLambdaZipConfigWithoutRuntime(funcName, policyName, roleName, sgName string) string {
return fmt.Sprintf(baseAccAWSLambdaConfig(policyName, roleName, sgName)+`
resource "aws_lambda_function" "test" {
function_name = "%s"
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.example"
}
`, funcName)
}

func TestFlattenLambdaImageConfigShouldNotFailWithEmptyImageConfig(t *testing.T) {
t.Parallel()
response := lambda.ImageConfigResponse{}
Expand Down