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