diff --git a/builtin/providers/aws/resource_aws_lambda_function.go b/builtin/providers/aws/resource_aws_lambda_function.go index 1f18622e71e8..1ebaf421dd27 100644 --- a/builtin/providers/aws/resource_aws_lambda_function.go +++ b/builtin/providers/aws/resource_aws_lambda_function.go @@ -193,6 +193,7 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e err := resource.Retry(1*time.Minute, func() error { _, err := conn.CreateFunction(params) if err != nil { + log.Printf("[ERROR] Received %q, retrying CreateFunction", err) if awserr, ok := err.(awserr.Error); ok { if awserr.Code() == "InvalidParameterValueException" { log.Printf("[DEBUG] InvalidParameterValueException creating Lambda Function: %s", awserr) diff --git a/builtin/providers/aws/resource_aws_lambda_function_test.go b/builtin/providers/aws/resource_aws_lambda_function_test.go index ff6f92bd4f33..616b176f5e67 100644 --- a/builtin/providers/aws/resource_aws_lambda_function_test.go +++ b/builtin/providers/aws/resource_aws_lambda_function_test.go @@ -1,7 +1,11 @@ package aws import ( + "archive/zip" "fmt" + "io/ioutil" + "os" + "path/filepath" "testing" "github.com/aws/aws-sdk-go/aws" @@ -19,9 +23,9 @@ func TestAccAWSLambdaFunction_basic(t *testing.T) { CheckDestroy: testAccCheckLambdaFunctionDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccAWSLambdaConfig, + Config: testAccAWSLambdaFunctionConfig, Check: resource.ComposeTestCheckFunc( - testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", &conf), + testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", "example_lambda_name", &conf), testAccCheckAWSLambdaAttributes(&conf), ), }, @@ -29,6 +33,45 @@ func TestAccAWSLambdaFunction_basic(t *testing.T) { }) } +func TestAccAWSLambdaFunction_localUpdate(t *testing.T) { + var conf lambda.GetFunctionOutput + + path, zipFile, err := createTempFile("lambda_localUpdate") + if err != nil { + t.Fatal(err) + } + + 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(path), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf), + testAccCheckAwsLambdaFunctionName(&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"}, zipFile) + }, + Config: genAWSLambdaFunctionConfig_local(path), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf), + testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"), + testAccCheckAwsLambdaSourceCodeHash(&conf, "Y5Jf4Si63UDy1wKNfPs+U56ZL0NxsieKPt9EwRl4GQM="), + ), + }, + }, + }) +} + func testAccCheckLambdaFunctionDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).lambdaconn @@ -51,7 +94,7 @@ func testAccCheckLambdaFunctionDestroy(s *terraform.State) error { } -func testAccCheckAwsLambdaFunctionExists(n string, function *lambda.GetFunctionOutput) resource.TestCheckFunc { +func testAccCheckAwsLambdaFunctionExists(n, funcName string, function *lambda.GetFunctionOutput) resource.TestCheckFunc { // Wait for IAM role return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -66,7 +109,7 @@ func testAccCheckAwsLambdaFunctionExists(n string, function *lambda.GetFunctionO conn := testAccProvider.Meta().(*AWSClient).lambdaconn params := &lambda.GetFunctionInput{ - FunctionName: aws.String("example_lambda_name"), + FunctionName: aws.String(funcName), } getFunction, err := conn.GetFunction(params) @@ -80,6 +123,40 @@ func testAccCheckAwsLambdaFunctionExists(n string, function *lambda.GetFunctionO } } +func testAccCheckAwsLambdaFunctionName(function *lambda.GetFunctionOutput, expectedName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + c := function.Configuration + if *c.FunctionName != expectedName { + return fmt.Errorf("Expected function name %s, got %s", expectedName, *c.FunctionName) + } + + return nil + } +} + +func testAccCheckAwsLambdaFunctionArn(function *lambda.GetFunctionOutput, expectedArn string) resource.TestCheckFunc { + return func(s *terraform.State) error { + c := function.Configuration + if *c.FunctionArn != expectedArn { + return fmt.Errorf("Expected function ARN %s, got %s", expectedArn, *c.FunctionArn) + } + + return nil + } +} + +func testAccCheckAwsLambdaSourceCodeHash(function *lambda.GetFunctionOutput, expectedHash string) resource.TestCheckFunc { + return func(s *terraform.State) error { + c := function.Configuration + if *c.CodeSha256 != expectedHash { + return fmt.Errorf("Expected code hash %s, got %s", expectedHash, *c.CodeSha256) + } + + return nil + } +} + +// TODO: REMOVE func testAccCheckAWSLambdaAttributes(function *lambda.GetFunctionOutput) resource.TestCheckFunc { return func(s *terraform.State) error { c := function.Configuration @@ -96,7 +173,51 @@ func testAccCheckAWSLambdaAttributes(function *lambda.GetFunctionOutput) resourc } } -const testAccAWSLambdaConfig = ` +func testAccCreateZipFromFiles(files map[string]string, zipFile *os.File) error { + zipFile.Truncate(0) + zipFile.Seek(0, 0) + + w := zip.NewWriter(zipFile) + + for source, destination := range files { + f, err := w.Create(destination) + if err != nil { + return err + } + + fileContent, err := ioutil.ReadFile(source) + if err != nil { + return err + } + + _, err = f.Write(fileContent) + if err != nil { + return err + } + } + + err := w.Close() + if err != nil { + return err + } + + return w.Flush() +} + +func createTempFile(prefix string) (string, *os.File, error) { + f, err := ioutil.TempFile(os.TempDir(), prefix) + if err != nil { + return "", nil, err + } + + pathToFile, err := filepath.Abs(f.Name()) + if err != nil { + return "", nil, err + } + return pathToFile, f, nil +} + +const testAccAWSLambdaFunctionConfig = ` resource "aws_iam_role_policy" "iam_policy_for_lambda" { name = "iam_policy_for_lambda" role = "${aws_iam_role.iam_for_lambda.id}" @@ -191,3 +312,37 @@ resource "aws_lambda_function" "lambda_function_test" { } } ` + +const testAccAWSLambdaFunctionConfig_local_tpl = ` +resource "aws_iam_role" "iam_for_lambda" { + name = "iam_for_lambda" + assume_role_policy = <