diff --git a/builtin/providers/aws/resource_aws_lambda_function.go b/builtin/providers/aws/resource_aws_lambda_function.go index f54bc1a2e5..4df10e9487 100644 --- a/builtin/providers/aws/resource_aws_lambda_function.go +++ b/builtin/providers/aws/resource_aws_lambda_function.go @@ -297,10 +297,11 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e } codeUpdate := false - if v, ok := d.GetOk("filename"); ok && d.HasChange("source_code_hash") { - file, err := loadFileContent(v.(string)) + if d.HasChange("filename") || d.HasChange("source_code_hash") { + name := d.Get("filename").(string) + file, err := loadFileContent(name) if err != nil { - return fmt.Errorf("Unable to load %q: %s", v.(string), err) + return fmt.Errorf("Unable to load %q: %s", name, err) } codeReq.ZipFile = file codeUpdate = true @@ -312,8 +313,8 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e codeUpdate = true } - log.Printf("[DEBUG] Send Update Lambda Function Code request: %#v", codeReq) if codeUpdate { + log.Printf("[DEBUG] Send Update Lambda Function Code request: %#v", codeReq) _, err := conn.UpdateFunctionCode(codeReq) if err != nil { return fmt.Errorf("Error modifying Lambda Function Code %s: %s", d.Id(), err) @@ -352,8 +353,8 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e configUpdate = true } - log.Printf("[DEBUG] Send Update Lambda Function Configuration request: %#v", configReq) if configUpdate { + log.Printf("[DEBUG] Send Update Lambda Function Configuration request: %#v", configReq) _, err := conn.UpdateFunctionConfiguration(configReq) if err != nil { return fmt.Errorf("Error modifying Lambda Function Configuration %s: %s", d.Id(), err) diff --git a/builtin/providers/aws/resource_aws_lambda_function_test.go b/builtin/providers/aws/resource_aws_lambda_function_test.go index 0195b2c2a5..f83004f0ea 100644 --- a/builtin/providers/aws/resource_aws_lambda_function_test.go +++ b/builtin/providers/aws/resource_aws_lambda_function_test.go @@ -129,6 +129,54 @@ func TestAccAWSLambdaFunction_localUpdate(t *testing.T) { }) } +func TestAccAWSLambdaFunction_localUpdate_nameOnly(t *testing.T) { + var conf lambda.GetFunctionOutput + + path, zipFile, err := createTempFile("lambda_localUpdate") + if err != nil { + t.Fatal(err) + } + defer os.Remove(path) + + updatedPath, updatedZipFile, err := createTempFile("lambda_localUpdate_name_change") + if err != nil { + t.Fatal(err) + } + defer os.Remove(updatedPath) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLambdaFunctionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + PreConfig: func() { + testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile) + }, + Config: genAWSLambdaFunctionConfig_local_name_only(path), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf), + testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"), + testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_local"), + testAccCheckAwsLambdaSourceCodeHash(&conf, "un6qF9S9hKvXbWwJ6m2EYaVCWjcr0PCZWiTV3h4zB0I="), + ), + }, + resource.TestStep{ + PreConfig: func() { + testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, updatedZipFile) + }, + Config: genAWSLambdaFunctionConfig_local_name_only(updatedPath), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf), + testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"), + testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_local"), + testAccCheckAwsLambdaSourceCodeHash(&conf, "Y5Jf4Si63UDy1wKNfPs+U56ZL0NxsieKPt9EwRl4GQM="), + ), + }, + }, + }) +} + func TestAccAWSLambdaFunction_s3Update(t *testing.T) { var conf lambda.GetFunctionOutput @@ -505,6 +553,38 @@ func genAWSLambdaFunctionConfig_local(filePath string) string { filePath, filePath) } +func genAWSLambdaFunctionConfig_local_name_only(filePath string) string { + return fmt.Sprintf(testAccAWSLambdaFunctionConfig_local_name_only_tpl, + filePath) +} + +const testAccAWSLambdaFunctionConfig_local_name_only_tpl = ` +resource "aws_iam_role" "iam_for_lambda" { + name = "iam_for_lambda" + assume_role_policy = <