diff --git a/builtin/providers/aws/resource_aws_lambda_function.go b/builtin/providers/aws/resource_aws_lambda_function.go index 5f8a81684..f556fa226 100644 --- a/builtin/providers/aws/resource_aws_lambda_function.go +++ b/builtin/providers/aws/resource_aws_lambda_function.go @@ -174,24 +174,30 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e log.Printf("[DEBUG] Creating Lambda Function %s with role %s", functionName, iamRole) + filename, hasFilename := d.GetOk("filename") + s3Bucket, bucketOk := d.GetOk("s3_bucket") + s3Key, keyOk := d.GetOk("s3_key") + s3ObjectVersion, versionOk := d.GetOk("s3_object_version") + + if !hasFilename && !bucketOk && !keyOk && !versionOk { + return errors.New("filename or s3_* attributes must be set") + } + var functionCode *lambda.FunctionCode - if v, ok := d.GetOk("filename"); ok { + if hasFilename { // Grab an exclusive lock so that we're only reading one function into // memory at a time. // See https://github.com/hashicorp/terraform/issues/9364 awsMutexKV.Lock(awsMutexLambdaKey) defer awsMutexKV.Unlock(awsMutexLambdaKey) - file, err := loadFileContent(v.(string)) + file, err := loadFileContent(filename.(string)) if err != nil { - return fmt.Errorf("Unable to load %q: %s", v.(string), err) + return fmt.Errorf("Unable to load %q: %s", filename.(string), err) } functionCode = &lambda.FunctionCode{ ZipFile: file, } } else { - s3Bucket, bucketOk := d.GetOk("s3_bucket") - s3Key, keyOk := d.GetOk("s3_key") - s3ObjectVersion, versionOk := d.GetOk("s3_object_version") if !bucketOk || !keyOk { return errors.New("s3_bucket and s3_key must all be set while using S3 code source") } diff --git a/builtin/providers/aws/resource_aws_lambda_function_test.go b/builtin/providers/aws/resource_aws_lambda_function_test.go index 06c7a64db..c50a7a60b 100644 --- a/builtin/providers/aws/resource_aws_lambda_function_test.go +++ b/builtin/providers/aws/resource_aws_lambda_function_test.go @@ -39,6 +39,22 @@ func TestAccAWSLambdaFunction_basic(t *testing.T) { }) } +func TestAccAWSLambdaFunction_expectFilenameAndS3Attributes(t *testing.T) { + rName := fmt.Sprintf("tf_test_%s", acctest.RandString(5)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLambdaFunctionDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLambdaConfigWithoutFilenameAndS3Attributes(rName), + ExpectError: regexp.MustCompile(`filename or s3_\* attributes must be set`), + }, + }, + }) +} + func TestAccAWSLambdaFunction_envVariables(t *testing.T) { var conf lambda.GetFunctionOutput @@ -625,6 +641,16 @@ resource "aws_lambda_function" "lambda_function_test" { `, rName) } +func testAccAWSLambdaConfigWithoutFilenameAndS3Attributes(rName string) string { + return fmt.Sprintf(baseAccAWSLambdaConfig+` +resource "aws_lambda_function" "lambda_function_test" { + function_name = "%s" + role = "${aws_iam_role.iam_for_lambda.arn}" + handler = "exports.example" +} +`, rName) +} + func testAccAWSLambdaConfigEnvVariables(rName string) string { return fmt.Sprintf(baseAccAWSLambdaConfig+` resource "aws_lambda_function" "lambda_function_test" {