github.com/daveadams/terraform@v0.6.4-0.20160830094355-13ce74975936/builtin/providers/aws/resource_aws_lambda_function_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"archive/zip"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"regexp"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/aws/aws-sdk-go/aws"
    14  	"github.com/aws/aws-sdk-go/service/lambda"
    15  	"github.com/hashicorp/terraform/helper/acctest"
    16  	"github.com/hashicorp/terraform/helper/resource"
    17  	"github.com/hashicorp/terraform/terraform"
    18  )
    19  
    20  func TestAccAWSLambdaFunction_basic(t *testing.T) {
    21  	var conf lambda.GetFunctionOutput
    22  
    23  	rName := fmt.Sprintf("tf_test_%s", acctest.RandString(5))
    24  
    25  	resource.Test(t, resource.TestCase{
    26  		PreCheck:     func() { testAccPreCheck(t) },
    27  		Providers:    testAccProviders,
    28  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
    29  		Steps: []resource.TestStep{
    30  			resource.TestStep{
    31  				Config: testAccAWSLambdaConfigBasic(rName),
    32  				Check: resource.ComposeTestCheckFunc(
    33  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
    34  					testAccCheckAwsLambdaFunctionName(&conf, rName),
    35  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccAWSLambdaFunction_VPC(t *testing.T) {
    43  	var conf lambda.GetFunctionOutput
    44  	rName := fmt.Sprintf("tf_test_%s", acctest.RandString(5))
    45  
    46  	resource.Test(t, resource.TestCase{
    47  		PreCheck:     func() { testAccPreCheck(t) },
    48  		Providers:    testAccProviders,
    49  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
    50  		Steps: []resource.TestStep{
    51  			resource.TestStep{
    52  				Config: testAccAWSLambdaConfigWithVPC(rName),
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
    55  					testAccCheckAwsLambdaFunctionName(&conf, rName),
    56  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
    57  					testAccCheckAWSLambdaFunctionVersion(&conf, "$LATEST"),
    58  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.#", "1"),
    59  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.0.subnet_ids.#", "1"),
    60  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.0.security_group_ids.#", "1"),
    61  					resource.TestMatchResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.0.vpc_id", regexp.MustCompile("^vpc-")),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func TestAccAWSLambdaFunction_s3(t *testing.T) {
    69  	var conf lambda.GetFunctionOutput
    70  	rName := fmt.Sprintf("tf_test_%s", acctest.RandString(5))
    71  
    72  	resource.Test(t, resource.TestCase{
    73  		PreCheck:     func() { testAccPreCheck(t) },
    74  		Providers:    testAccProviders,
    75  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
    76  		Steps: []resource.TestStep{
    77  			resource.TestStep{
    78  				Config: testAccAWSLambdaConfigS3(rName),
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3test", rName, &conf),
    81  					testAccCheckAwsLambdaFunctionName(&conf, rName),
    82  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
    83  					testAccCheckAWSLambdaFunctionVersion(&conf, "$LATEST"),
    84  				),
    85  			},
    86  		},
    87  	})
    88  }
    89  
    90  func TestAccAWSLambdaFunction_localUpdate(t *testing.T) {
    91  	var conf lambda.GetFunctionOutput
    92  
    93  	path, zipFile, err := createTempFile("lambda_localUpdate")
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	defer os.Remove(path)
    98  
    99  	resource.Test(t, resource.TestCase{
   100  		PreCheck:     func() { testAccPreCheck(t) },
   101  		Providers:    testAccProviders,
   102  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   103  		Steps: []resource.TestStep{
   104  			resource.TestStep{
   105  				PreConfig: func() {
   106  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
   107  				},
   108  				Config: genAWSLambdaFunctionConfig_local(path),
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf),
   111  					testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"),
   112  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_local"),
   113  					testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="),
   114  				),
   115  			},
   116  			resource.TestStep{
   117  				PreConfig: func() {
   118  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile)
   119  				},
   120  				Config: genAWSLambdaFunctionConfig_local(path),
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf),
   123  					testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"),
   124  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_local"),
   125  					testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="),
   126  				),
   127  			},
   128  		},
   129  	})
   130  }
   131  
   132  func TestAccAWSLambdaFunction_localUpdate_nameOnly(t *testing.T) {
   133  	var conf lambda.GetFunctionOutput
   134  
   135  	path, zipFile, err := createTempFile("lambda_localUpdate")
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	defer os.Remove(path)
   140  
   141  	updatedPath, updatedZipFile, err := createTempFile("lambda_localUpdate_name_change")
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  	defer os.Remove(updatedPath)
   146  
   147  	resource.Test(t, resource.TestCase{
   148  		PreCheck:     func() { testAccPreCheck(t) },
   149  		Providers:    testAccProviders,
   150  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   151  		Steps: []resource.TestStep{
   152  			resource.TestStep{
   153  				PreConfig: func() {
   154  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
   155  				},
   156  				Config: genAWSLambdaFunctionConfig_local_name_only(path),
   157  				Check: resource.ComposeTestCheckFunc(
   158  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf),
   159  					testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"),
   160  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_local"),
   161  					testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="),
   162  				),
   163  			},
   164  			resource.TestStep{
   165  				PreConfig: func() {
   166  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, updatedZipFile)
   167  				},
   168  				Config: genAWSLambdaFunctionConfig_local_name_only(updatedPath),
   169  				Check: resource.ComposeTestCheckFunc(
   170  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf),
   171  					testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"),
   172  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_local"),
   173  					testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="),
   174  				),
   175  			},
   176  		},
   177  	})
   178  }
   179  
   180  func TestAccAWSLambdaFunction_s3Update(t *testing.T) {
   181  	var conf lambda.GetFunctionOutput
   182  
   183  	path, zipFile, err := createTempFile("lambda_s3Update")
   184  	if err != nil {
   185  		t.Fatal(err)
   186  	}
   187  	defer os.Remove(path)
   188  
   189  	bucketName := fmt.Sprintf("tf-acc-lambda-s3-deployments-%d", randomInteger)
   190  	key := "lambda-func.zip"
   191  
   192  	resource.Test(t, resource.TestCase{
   193  		PreCheck:     func() { testAccPreCheck(t) },
   194  		Providers:    testAccProviders,
   195  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   196  		Steps: []resource.TestStep{
   197  			resource.TestStep{
   198  				PreConfig: func() {
   199  					// Upload 1st version
   200  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
   201  				},
   202  				Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path),
   203  				Check: resource.ComposeTestCheckFunc(
   204  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3", &conf),
   205  					testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3"),
   206  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_s3"),
   207  					testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="),
   208  				),
   209  			},
   210  			resource.TestStep{
   211  				ExpectNonEmptyPlan: true,
   212  				PreConfig: func() {
   213  					// Upload 2nd version
   214  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile)
   215  				},
   216  				Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path),
   217  			},
   218  			// Extra step because of missing ComputedWhen
   219  			// See https://github.com/hashicorp/terraform/pull/4846 & https://github.com/hashicorp/terraform/pull/5330
   220  			resource.TestStep{
   221  				Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path),
   222  				Check: resource.ComposeTestCheckFunc(
   223  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3", &conf),
   224  					testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3"),
   225  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_s3"),
   226  					testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="),
   227  				),
   228  			},
   229  		},
   230  	})
   231  }
   232  
   233  func TestAccAWSLambdaFunction_s3Update_unversioned(t *testing.T) {
   234  	var conf lambda.GetFunctionOutput
   235  
   236  	path, zipFile, err := createTempFile("lambda_s3Update")
   237  	if err != nil {
   238  		t.Fatal(err)
   239  	}
   240  	defer os.Remove(path)
   241  
   242  	bucketName := fmt.Sprintf("tf-acc-lambda-s3-deployments-%d", randomInteger)
   243  	key := "lambda-func.zip"
   244  	key2 := "lambda-func-modified.zip"
   245  
   246  	resource.Test(t, resource.TestCase{
   247  		PreCheck:     func() { testAccPreCheck(t) },
   248  		Providers:    testAccProviders,
   249  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   250  		Steps: []resource.TestStep{
   251  			resource.TestStep{
   252  				PreConfig: func() {
   253  					// Upload 1st version
   254  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
   255  				},
   256  				Config: genAWSLambdaFunctionConfig_s3_unversioned(bucketName, key, path),
   257  				Check: resource.ComposeTestCheckFunc(
   258  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3_unversioned", &conf),
   259  					testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3_unversioned"),
   260  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_s3_unversioned"),
   261  					testAccCheckAwsLambdaSourceCodeHash(&conf, "un6qF9S9hKvXbWwJ6m2EYaVCWjcr0PCZWiTV3h4zB0I="),
   262  				),
   263  			},
   264  			resource.TestStep{
   265  				ExpectNonEmptyPlan: true,
   266  				PreConfig: func() {
   267  					// Upload 2nd version
   268  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile)
   269  				},
   270  				Config: genAWSLambdaFunctionConfig_s3_unversioned(bucketName, key2, path),
   271  			},
   272  			// Extra step because of missing ComputedWhen
   273  			// See https://github.com/hashicorp/terraform/pull/4846 & https://github.com/hashicorp/terraform/pull/5330
   274  			resource.TestStep{
   275  				Config: genAWSLambdaFunctionConfig_s3_unversioned(bucketName, key2, path),
   276  				Check: resource.ComposeTestCheckFunc(
   277  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3_unversioned", &conf),
   278  					testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3_unversioned"),
   279  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_s3_unversioned"),
   280  					testAccCheckAwsLambdaSourceCodeHash(&conf, "Y5Jf4Si63UDy1wKNfPs+U56ZL0NxsieKPt9EwRl4GQM="),
   281  				),
   282  			},
   283  		},
   284  	})
   285  }
   286  
   287  func testAccCheckLambdaFunctionDestroy(s *terraform.State) error {
   288  	conn := testAccProvider.Meta().(*AWSClient).lambdaconn
   289  
   290  	for _, rs := range s.RootModule().Resources {
   291  		if rs.Type != "aws_lambda_function" {
   292  			continue
   293  		}
   294  
   295  		_, err := conn.GetFunction(&lambda.GetFunctionInput{
   296  			FunctionName: aws.String(rs.Primary.ID),
   297  		})
   298  
   299  		if err == nil {
   300  			return fmt.Errorf("Lambda Function still exists")
   301  		}
   302  
   303  	}
   304  
   305  	return nil
   306  
   307  }
   308  
   309  func testAccCheckAwsLambdaFunctionExists(res, funcName string, function *lambda.GetFunctionOutput) resource.TestCheckFunc {
   310  	// Wait for IAM role
   311  	return func(s *terraform.State) error {
   312  		rs, ok := s.RootModule().Resources[res]
   313  		if !ok {
   314  			return fmt.Errorf("Lambda function not found: %s", res)
   315  		}
   316  
   317  		if rs.Primary.ID == "" {
   318  			return fmt.Errorf("Lambda function ID not set")
   319  		}
   320  
   321  		conn := testAccProvider.Meta().(*AWSClient).lambdaconn
   322  
   323  		params := &lambda.GetFunctionInput{
   324  			FunctionName: aws.String(funcName),
   325  		}
   326  
   327  		getFunction, err := conn.GetFunction(params)
   328  		if err != nil {
   329  			return err
   330  		}
   331  
   332  		*function = *getFunction
   333  
   334  		return nil
   335  	}
   336  }
   337  
   338  func testAccCheckAwsLambdaFunctionName(function *lambda.GetFunctionOutput, expectedName string) resource.TestCheckFunc {
   339  	return func(s *terraform.State) error {
   340  		c := function.Configuration
   341  		if *c.FunctionName != expectedName {
   342  			return fmt.Errorf("Expected function name %s, got %s", expectedName, *c.FunctionName)
   343  		}
   344  
   345  		return nil
   346  	}
   347  }
   348  
   349  func testAccCheckAWSLambdaFunctionVersion(function *lambda.GetFunctionOutput, expectedVersion string) resource.TestCheckFunc {
   350  	return func(s *terraform.State) error {
   351  		c := function.Configuration
   352  		if *c.Version != expectedVersion {
   353  			return fmt.Errorf("Expected version %s, got %s", expectedVersion, *c.Version)
   354  		}
   355  		return nil
   356  	}
   357  }
   358  
   359  func testAccCheckAwsLambdaFunctionArnHasSuffix(function *lambda.GetFunctionOutput, arnSuffix string) resource.TestCheckFunc {
   360  	return func(s *terraform.State) error {
   361  		c := function.Configuration
   362  		if !strings.HasSuffix(*c.FunctionArn, arnSuffix) {
   363  			return fmt.Errorf("Expected function ARN %s to have suffix %s", *c.FunctionArn, arnSuffix)
   364  		}
   365  
   366  		return nil
   367  	}
   368  }
   369  
   370  func testAccCheckAwsLambdaSourceCodeHash(function *lambda.GetFunctionOutput, expectedHash string) resource.TestCheckFunc {
   371  	return func(s *terraform.State) error {
   372  		c := function.Configuration
   373  		if *c.CodeSha256 != expectedHash {
   374  			return fmt.Errorf("Expected code hash %s, got %s", expectedHash, *c.CodeSha256)
   375  		}
   376  
   377  		return nil
   378  	}
   379  }
   380  
   381  func testAccCreateZipFromFiles(files map[string]string, zipFile *os.File) error {
   382  	zipFile.Truncate(0)
   383  	zipFile.Seek(0, 0)
   384  
   385  	w := zip.NewWriter(zipFile)
   386  
   387  	for source, destination := range files {
   388  		f, err := w.Create(destination)
   389  		if err != nil {
   390  			return err
   391  		}
   392  
   393  		fileContent, err := ioutil.ReadFile(source)
   394  		if err != nil {
   395  			return err
   396  		}
   397  
   398  		_, err = f.Write(fileContent)
   399  		if err != nil {
   400  			return err
   401  		}
   402  	}
   403  
   404  	err := w.Close()
   405  	if err != nil {
   406  		return err
   407  	}
   408  
   409  	return w.Flush()
   410  }
   411  
   412  func createTempFile(prefix string) (string, *os.File, error) {
   413  	f, err := ioutil.TempFile(os.TempDir(), prefix)
   414  	if err != nil {
   415  		return "", nil, err
   416  	}
   417  
   418  	pathToFile, err := filepath.Abs(f.Name())
   419  	if err != nil {
   420  		return "", nil, err
   421  	}
   422  	return pathToFile, f, nil
   423  }
   424  
   425  const baseAccAWSLambdaConfig = `
   426  resource "aws_iam_role_policy" "iam_policy_for_lambda" {
   427      name = "iam_policy_for_lambda"
   428      role = "${aws_iam_role.iam_for_lambda.id}"
   429      policy = <<EOF
   430  {
   431    "Version": "2012-10-17",
   432    "Statement": [
   433          {
   434              "Effect": "Allow",
   435              "Action": [
   436                  "logs:CreateLogGroup",
   437                  "logs:CreateLogStream",
   438                  "logs:PutLogEvents"
   439              ],
   440              "Resource": "arn:aws:logs:*:*:*"
   441          },
   442      {
   443        "Effect": "Allow",
   444        "Action": [
   445          "ec2:CreateNetworkInterface"
   446        ],
   447        "Resource": [
   448          "*"
   449        ]
   450      }
   451    ]
   452  }
   453  EOF
   454  }
   455  
   456  resource "aws_iam_role" "iam_for_lambda" {
   457      name = "iam_for_lambda"
   458      assume_role_policy = <<EOF
   459  {
   460    "Version": "2012-10-17",
   461    "Statement": [
   462      {
   463        "Action": "sts:AssumeRole",
   464        "Principal": {
   465          "Service": "lambda.amazonaws.com"
   466        },
   467        "Effect": "Allow",
   468        "Sid": ""
   469      }
   470    ]
   471  }
   472  EOF
   473  }
   474  
   475  resource "aws_vpc" "vpc_for_lambda" {
   476      cidr_block = "10.0.0.0/16"
   477  }
   478  
   479  resource "aws_subnet" "subnet_for_lambda" {
   480      vpc_id = "${aws_vpc.vpc_for_lambda.id}"
   481      cidr_block = "10.0.1.0/24"
   482  
   483      tags {
   484          Name = "lambda"
   485      }
   486  }
   487  
   488  resource "aws_security_group" "sg_for_lambda" {
   489    name = "sg_for_lambda"
   490    description = "Allow all inbound traffic for lambda test"
   491    vpc_id = "${aws_vpc.vpc_for_lambda.id}"
   492  
   493    ingress {
   494        from_port = 0
   495        to_port = 0
   496        protocol = "-1"
   497        cidr_blocks = ["0.0.0.0/0"]
   498    }
   499  
   500    egress {
   501        from_port = 0
   502        to_port = 0
   503        protocol = "-1"
   504        cidr_blocks = ["0.0.0.0/0"]
   505    }
   506  }
   507  
   508  `
   509  
   510  func testAccAWSLambdaConfigBasic(rName string) string {
   511  	return fmt.Sprintf(baseAccAWSLambdaConfig+`
   512  resource "aws_lambda_function" "lambda_function_test" {
   513      filename = "test-fixtures/lambdatest.zip"
   514      function_name = "%s"
   515      role = "${aws_iam_role.iam_for_lambda.arn}"
   516      handler = "exports.example"
   517  }
   518  `, rName)
   519  }
   520  
   521  func testAccAWSLambdaConfigWithVPC(rName string) string {
   522  	return fmt.Sprintf(baseAccAWSLambdaConfig+`
   523  resource "aws_lambda_function" "lambda_function_test" {
   524      filename = "test-fixtures/lambdatest.zip"
   525      function_name = "%s"
   526      role = "${aws_iam_role.iam_for_lambda.arn}"
   527      handler = "exports.example"
   528  
   529      vpc_config = {
   530          subnet_ids = ["${aws_subnet.subnet_for_lambda.id}"]
   531          security_group_ids = ["${aws_security_group.sg_for_lambda.id}"]
   532      }
   533  }`, rName)
   534  }
   535  
   536  func testAccAWSLambdaConfigS3(rName string) string {
   537  	return fmt.Sprintf(`
   538  resource "aws_s3_bucket" "lambda_bucket" {
   539    bucket = "tf-test-bucket-%d"
   540  }
   541  
   542  resource "aws_s3_bucket_object" "lambda_code" {
   543    bucket = "${aws_s3_bucket.lambda_bucket.id}"
   544    key = "lambdatest.zip"
   545    source = "test-fixtures/lambdatest.zip"
   546  }
   547  
   548  resource "aws_iam_role" "iam_for_lambda" {
   549      name = "iam_for_lambda"
   550      assume_role_policy = <<EOF
   551  {
   552    "Version": "2012-10-17",
   553    "Statement": [
   554      {
   555        "Action": "sts:AssumeRole",
   556        "Principal": {
   557          "Service": "lambda.amazonaws.com"
   558        },
   559        "Effect": "Allow",
   560        "Sid": ""
   561      }
   562    ]
   563  }
   564  EOF
   565  }
   566  
   567  resource "aws_lambda_function" "lambda_function_s3test" {
   568      s3_bucket = "${aws_s3_bucket.lambda_bucket.id}"
   569      s3_key = "${aws_s3_bucket_object.lambda_code.id}"
   570      function_name = "%s"
   571      role = "${aws_iam_role.iam_for_lambda.arn}"
   572      handler = "exports.example"
   573  }
   574  `, acctest.RandInt(), rName)
   575  }
   576  
   577  const testAccAWSLambdaFunctionConfig_local_tpl = `
   578  resource "aws_iam_role" "iam_for_lambda" {
   579      name = "iam_for_lambda"
   580      assume_role_policy = <<EOF
   581  {
   582    "Version": "2012-10-17",
   583    "Statement": [
   584      {
   585        "Action": "sts:AssumeRole",
   586        "Principal": {
   587          "Service": "lambda.amazonaws.com"
   588        },
   589        "Effect": "Allow",
   590        "Sid": ""
   591      }
   592    ]
   593  }
   594  EOF
   595  }
   596  resource "aws_lambda_function" "lambda_function_local" {
   597      filename = "%s"
   598      source_code_hash = "${base64sha256(file("%s"))}"
   599      function_name = "tf_acc_lambda_name_local"
   600      role = "${aws_iam_role.iam_for_lambda.arn}"
   601      handler = "exports.example"
   602  }
   603  `
   604  
   605  func genAWSLambdaFunctionConfig_local(filePath string) string {
   606  	return fmt.Sprintf(testAccAWSLambdaFunctionConfig_local_tpl,
   607  		filePath, filePath)
   608  }
   609  
   610  func genAWSLambdaFunctionConfig_local_name_only(filePath string) string {
   611  	return fmt.Sprintf(testAccAWSLambdaFunctionConfig_local_name_only_tpl,
   612  		filePath)
   613  }
   614  
   615  const testAccAWSLambdaFunctionConfig_local_name_only_tpl = `
   616  resource "aws_iam_role" "iam_for_lambda" {
   617      name = "iam_for_lambda"
   618      assume_role_policy = <<EOF
   619  {
   620    "Version": "2012-10-17",
   621    "Statement": [
   622      {
   623        "Action": "sts:AssumeRole",
   624        "Principal": {
   625          "Service": "lambda.amazonaws.com"
   626        },
   627        "Effect": "Allow",
   628        "Sid": ""
   629      }
   630    ]
   631  }
   632  EOF
   633  }
   634  resource "aws_lambda_function" "lambda_function_local" {
   635      filename = "%s"
   636      function_name = "tf_acc_lambda_name_local"
   637      role = "${aws_iam_role.iam_for_lambda.arn}"
   638      handler = "exports.example"
   639  }
   640  `
   641  
   642  const testAccAWSLambdaFunctionConfig_s3_tpl = `
   643  resource "aws_s3_bucket" "artifacts" {
   644  	bucket = "%s"
   645  	acl = "private"
   646  	force_destroy = true
   647  	versioning {
   648  		enabled = true
   649  	}
   650  }
   651  resource "aws_s3_bucket_object" "o" {
   652  	bucket = "${aws_s3_bucket.artifacts.bucket}"
   653  	key = "%s"
   654  	source = "%s"
   655  	etag = "${md5(file("%s"))}"
   656  }
   657  resource "aws_iam_role" "iam_for_lambda" {
   658      name = "iam_for_lambda"
   659      assume_role_policy = <<EOF
   660  {
   661    "Version": "2012-10-17",
   662    "Statement": [
   663      {
   664        "Action": "sts:AssumeRole",
   665        "Principal": {
   666          "Service": "lambda.amazonaws.com"
   667        },
   668        "Effect": "Allow",
   669        "Sid": ""
   670      }
   671    ]
   672  }
   673  EOF
   674  }
   675  resource "aws_lambda_function" "lambda_function_s3" {
   676  	s3_bucket = "${aws_s3_bucket_object.o.bucket}"
   677  	s3_key = "${aws_s3_bucket_object.o.key}"
   678  	s3_object_version = "${aws_s3_bucket_object.o.version_id}"
   679      function_name = "tf_acc_lambda_name_s3"
   680      role = "${aws_iam_role.iam_for_lambda.arn}"
   681      handler = "exports.example"
   682  }
   683  `
   684  
   685  func genAWSLambdaFunctionConfig_s3(bucket, key, path string) string {
   686  	return fmt.Sprintf(testAccAWSLambdaFunctionConfig_s3_tpl,
   687  		bucket, key, path, path)
   688  }
   689  
   690  const testAccAWSLambdaFunctionConfig_s3_unversioned_tpl = `
   691  resource "aws_s3_bucket" "artifacts" {
   692  	bucket = "%s"
   693  	acl = "private"
   694  	force_destroy = true
   695  }
   696  resource "aws_s3_bucket_object" "o" {
   697  	bucket = "${aws_s3_bucket.artifacts.bucket}"
   698  	key = "%s"
   699  	source = "%s"
   700  	etag = "${md5(file("%s"))}"
   701  }
   702  resource "aws_iam_role" "iam_for_lambda" {
   703      name = "iam_for_lambda"
   704      assume_role_policy = <<EOF
   705  {
   706    "Version": "2012-10-17",
   707    "Statement": [
   708      {
   709        "Action": "sts:AssumeRole",
   710        "Principal": {
   711          "Service": "lambda.amazonaws.com"
   712        },
   713        "Effect": "Allow",
   714        "Sid": ""
   715      }
   716    ]
   717  }
   718  EOF
   719  }
   720  resource "aws_lambda_function" "lambda_function_s3" {
   721  	s3_bucket = "${aws_s3_bucket_object.o.bucket}"
   722  	s3_key = "${aws_s3_bucket_object.o.key}"
   723      function_name = "tf_acc_lambda_name_s3_unversioned"
   724      role = "${aws_iam_role.iam_for_lambda.arn}"
   725      handler = "exports.example"
   726  }
   727  `
   728  
   729  func genAWSLambdaFunctionConfig_s3_unversioned(bucket, key, path string) string {
   730  	return fmt.Sprintf(testAccAWSLambdaFunctionConfig_s3_unversioned_tpl,
   731  		bucket, key, path, path)
   732  }