github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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  	rSt := acctest.RandString(5)
    24  	rName := fmt.Sprintf("tf_test_%s", rSt)
    25  
    26  	resource.Test(t, resource.TestCase{
    27  		PreCheck:     func() { testAccPreCheck(t) },
    28  		Providers:    testAccProviders,
    29  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
    30  		Steps: []resource.TestStep{
    31  			{
    32  				Config: testAccAWSLambdaConfigBasic(rName, rSt),
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
    35  					testAccCheckAwsLambdaFunctionName(&conf, rName),
    36  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func TestAccAWSLambdaFunction_updateRuntime(t *testing.T) {
    44  	var conf lambda.GetFunctionOutput
    45  
    46  	rSt := acctest.RandString(5)
    47  	rName := fmt.Sprintf("tf_test_%s", rSt)
    48  
    49  	resource.Test(t, resource.TestCase{
    50  		PreCheck:     func() { testAccPreCheck(t) },
    51  		Providers:    testAccProviders,
    52  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
    53  		Steps: []resource.TestStep{
    54  			{
    55  				Config: testAccAWSLambdaConfigBasic(rName, rSt),
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
    58  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "runtime", "nodejs4.3"),
    59  				),
    60  			},
    61  			{
    62  				Config: testAccAWSLambdaConfigBasicUpdateRuntime(rName, rSt),
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
    65  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "runtime", "nodejs4.3-edge"),
    66  				),
    67  			},
    68  		},
    69  	})
    70  }
    71  
    72  func TestAccAWSLambdaFunction_expectFilenameAndS3Attributes(t *testing.T) {
    73  	rSt := acctest.RandString(5)
    74  	rName := fmt.Sprintf("tf_test_%s", rSt)
    75  
    76  	resource.Test(t, resource.TestCase{
    77  		PreCheck:     func() { testAccPreCheck(t) },
    78  		Providers:    testAccProviders,
    79  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
    80  		Steps: []resource.TestStep{
    81  			{
    82  				Config:      testAccAWSLambdaConfigWithoutFilenameAndS3Attributes(rName, rSt),
    83  				ExpectError: regexp.MustCompile(`filename or s3_\* attributes must be set`),
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func TestAccAWSLambdaFunction_envVariables(t *testing.T) {
    90  	var conf lambda.GetFunctionOutput
    91  
    92  	rSt := acctest.RandString(5)
    93  	rName := fmt.Sprintf("tf_test_%s", rSt)
    94  
    95  	resource.Test(t, resource.TestCase{
    96  		PreCheck:     func() { testAccPreCheck(t) },
    97  		Providers:    testAccProviders,
    98  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
    99  		Steps: []resource.TestStep{
   100  			{
   101  				Config: testAccAWSLambdaConfigBasic(rName, rSt),
   102  				Check: resource.ComposeTestCheckFunc(
   103  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   104  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   105  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   106  					resource.TestCheckNoResourceAttr("aws_lambda_function.lambda_function_test", "environment"),
   107  				),
   108  			},
   109  			{
   110  				Config: testAccAWSLambdaConfigEnvVariables(rName, rSt),
   111  				Check: resource.ComposeTestCheckFunc(
   112  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   113  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   114  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   115  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "environment.0.variables.foo", "bar"),
   116  				),
   117  			},
   118  			{
   119  				Config: testAccAWSLambdaConfigEnvVariablesModified(rName, rSt),
   120  				Check: resource.ComposeTestCheckFunc(
   121  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   122  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   123  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   124  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "environment.0.variables.foo", "baz"),
   125  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "environment.0.variables.foo1", "bar1"),
   126  				),
   127  			},
   128  			{
   129  				Config: testAccAWSLambdaConfigEnvVariablesModifiedWithoutEnvironment(rName, rSt),
   130  				Check: resource.ComposeTestCheckFunc(
   131  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   132  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   133  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   134  					resource.TestCheckNoResourceAttr("aws_lambda_function.lambda_function_test", "environment"),
   135  				),
   136  			},
   137  		},
   138  	})
   139  }
   140  
   141  func TestAccAWSLambdaFunction_encryptedEnvVariables(t *testing.T) {
   142  	var conf lambda.GetFunctionOutput
   143  
   144  	rSt := acctest.RandString(5)
   145  	rName := fmt.Sprintf("tf_test_%s", rSt)
   146  	keyRegex := regexp.MustCompile("^arn:aws:kms:")
   147  
   148  	resource.Test(t, resource.TestCase{
   149  		PreCheck:     func() { testAccPreCheck(t) },
   150  		Providers:    testAccProviders,
   151  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   152  		Steps: []resource.TestStep{
   153  			{
   154  				Config: testAccAWSLambdaConfigEncryptedEnvVariables(rName, rSt),
   155  				Check: resource.ComposeTestCheckFunc(
   156  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   157  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   158  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   159  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "environment.0.variables.foo", "bar"),
   160  					resource.TestMatchResourceAttr("aws_lambda_function.lambda_function_test", "kms_key_arn", keyRegex),
   161  				),
   162  			},
   163  			{
   164  				Config: testAccAWSLambdaConfigEncryptedEnvVariablesModified(rName, rSt),
   165  				Check: resource.ComposeTestCheckFunc(
   166  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   167  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   168  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   169  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "environment.0.variables.foo", "bar"),
   170  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "kms_key_arn", ""),
   171  				),
   172  			},
   173  		},
   174  	})
   175  }
   176  
   177  func TestAccAWSLambdaFunction_versioned(t *testing.T) {
   178  	var conf lambda.GetFunctionOutput
   179  
   180  	rSt := acctest.RandString(5)
   181  	rName := fmt.Sprintf("tf_test_%s", rSt)
   182  
   183  	resource.Test(t, resource.TestCase{
   184  		PreCheck:     func() { testAccPreCheck(t) },
   185  		Providers:    testAccProviders,
   186  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   187  		Steps: []resource.TestStep{
   188  			{
   189  				Config: testAccAWSLambdaConfigVersioned(rName, rSt),
   190  				Check: resource.ComposeTestCheckFunc(
   191  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   192  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   193  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   194  					resource.TestMatchResourceAttr("aws_lambda_function.lambda_function_test", "version",
   195  						regexp.MustCompile("^[0-9]+$")),
   196  					resource.TestMatchResourceAttr("aws_lambda_function.lambda_function_test", "qualified_arn",
   197  						regexp.MustCompile(":"+rName+":[0-9]+$")),
   198  				),
   199  			},
   200  		},
   201  	})
   202  }
   203  
   204  func TestAccAWSLambdaFunction_DeadLetterConfig(t *testing.T) {
   205  	var conf lambda.GetFunctionOutput
   206  
   207  	rSt := acctest.RandString(5)
   208  	rName := fmt.Sprintf("tf_test_%s", rSt)
   209  
   210  	resource.Test(t, resource.TestCase{
   211  		PreCheck:     func() { testAccPreCheck(t) },
   212  		Providers:    testAccProviders,
   213  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   214  		Steps: []resource.TestStep{
   215  			{
   216  				Config: testAccAWSLambdaConfigWithDeadLetterConfig(rName, rSt),
   217  				Check: resource.ComposeTestCheckFunc(
   218  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   219  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   220  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   221  					func(s *terraform.State) error {
   222  						if !strings.HasSuffix(*conf.Configuration.DeadLetterConfig.TargetArn, ":"+rName) {
   223  							return fmt.Errorf(
   224  								"Expected DeadLetterConfig.TargetArn %s to have suffix %s", *conf.Configuration.DeadLetterConfig.TargetArn, ":"+rName,
   225  							)
   226  						}
   227  						return nil
   228  					},
   229  				),
   230  			},
   231  		},
   232  	})
   233  }
   234  
   235  func TestAccAWSLambdaFunction_nilDeadLetterConfig(t *testing.T) {
   236  	rSt := acctest.RandString(5)
   237  	rName := fmt.Sprintf("tf_test_%s", rSt)
   238  
   239  	resource.Test(t, resource.TestCase{
   240  		PreCheck:     func() { testAccPreCheck(t) },
   241  		Providers:    testAccProviders,
   242  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   243  		Steps: []resource.TestStep{
   244  			{
   245  				Config: testAccAWSLambdaConfigWithNilDeadLetterConfig(rName, rSt),
   246  				ExpectError: regexp.MustCompile(
   247  					fmt.Sprintf("Nil dead_letter_config supplied for function: %s", rName)),
   248  			},
   249  		},
   250  	})
   251  }
   252  
   253  func TestAccAWSLambdaFunction_tracingConfig(t *testing.T) {
   254  	var conf lambda.GetFunctionOutput
   255  
   256  	rSt := acctest.RandString(5)
   257  	rName := fmt.Sprintf("tf_test_%s", rSt)
   258  
   259  	resource.Test(t, resource.TestCase{
   260  		PreCheck:     func() { testAccPreCheck(t) },
   261  		Providers:    testAccProviders,
   262  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   263  		Steps: []resource.TestStep{
   264  			{
   265  				Config: testAccAWSLambdaConfigWithTracingConfig(rName, rSt),
   266  				Check: resource.ComposeTestCheckFunc(
   267  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   268  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   269  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   270  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tracing_config.0.mode", "Active"),
   271  				),
   272  			},
   273  			{
   274  				Config: testAccAWSLambdaConfigWithTracingConfigUpdated(rName, rSt),
   275  				Check: resource.ComposeTestCheckFunc(
   276  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   277  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   278  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   279  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tracing_config.0.mode", "PassThrough"),
   280  				),
   281  			},
   282  		},
   283  	})
   284  }
   285  
   286  func TestAccAWSLambdaFunction_VPC(t *testing.T) {
   287  	var conf lambda.GetFunctionOutput
   288  
   289  	rSt := acctest.RandString(5)
   290  	rName := fmt.Sprintf("tf_test_%s", rSt)
   291  
   292  	resource.Test(t, resource.TestCase{
   293  		PreCheck:     func() { testAccPreCheck(t) },
   294  		Providers:    testAccProviders,
   295  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   296  		Steps: []resource.TestStep{
   297  			{
   298  				Config: testAccAWSLambdaConfigWithVPC(rName, rSt),
   299  				Check: resource.ComposeTestCheckFunc(
   300  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   301  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   302  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   303  					testAccCheckAWSLambdaFunctionVersion(&conf, "$LATEST"),
   304  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.#", "1"),
   305  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.0.subnet_ids.#", "1"),
   306  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.0.security_group_ids.#", "1"),
   307  					resource.TestMatchResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.0.vpc_id", regexp.MustCompile("^vpc-")),
   308  				),
   309  			},
   310  		},
   311  	})
   312  }
   313  
   314  // See https://github.com/hashicorp/terraform/issues/5767
   315  // and https://github.com/hashicorp/terraform/issues/10272
   316  func TestAccAWSLambdaFunction_VPC_withInvocation(t *testing.T) {
   317  	var conf lambda.GetFunctionOutput
   318  
   319  	rSt := acctest.RandString(5)
   320  	rName := fmt.Sprintf("tf_test_%s", rSt)
   321  
   322  	resource.Test(t, resource.TestCase{
   323  		PreCheck:     func() { testAccPreCheck(t) },
   324  		Providers:    testAccProviders,
   325  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   326  		Steps: []resource.TestStep{
   327  			{
   328  				Config: testAccAWSLambdaConfigWithVPC(rName, rSt),
   329  				Check: resource.ComposeTestCheckFunc(
   330  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   331  					testAccAwsInvokeLambdaFunction(&conf),
   332  				),
   333  			},
   334  		},
   335  	})
   336  }
   337  
   338  func TestAccAWSLambdaFunction_s3(t *testing.T) {
   339  	var conf lambda.GetFunctionOutput
   340  	rSt := acctest.RandString(5)
   341  	rName := fmt.Sprintf("tf_test_%s", rSt)
   342  
   343  	resource.Test(t, resource.TestCase{
   344  		PreCheck:     func() { testAccPreCheck(t) },
   345  		Providers:    testAccProviders,
   346  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   347  		Steps: []resource.TestStep{
   348  			{
   349  				Config: testAccAWSLambdaConfigS3(rName, rSt),
   350  				Check: resource.ComposeTestCheckFunc(
   351  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3test", rName, &conf),
   352  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   353  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   354  					testAccCheckAWSLambdaFunctionVersion(&conf, "$LATEST"),
   355  				),
   356  			},
   357  		},
   358  	})
   359  }
   360  
   361  func TestAccAWSLambdaFunction_localUpdate(t *testing.T) {
   362  	var conf lambda.GetFunctionOutput
   363  
   364  	path, zipFile, err := createTempFile("lambda_localUpdate")
   365  	if err != nil {
   366  		t.Fatal(err)
   367  	}
   368  	defer os.Remove(path)
   369  
   370  	rInt := acctest.RandInt()
   371  	rName := fmt.Sprintf("tf_acc_lambda_local_%d", rInt)
   372  
   373  	resource.Test(t, resource.TestCase{
   374  		PreCheck:     func() { testAccPreCheck(t) },
   375  		Providers:    testAccProviders,
   376  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   377  		Steps: []resource.TestStep{
   378  			{
   379  				PreConfig: func() {
   380  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
   381  				},
   382  				Config: genAWSLambdaFunctionConfig_local(path, rInt, rName),
   383  				Check: resource.ComposeTestCheckFunc(
   384  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", rName, &conf),
   385  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   386  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName),
   387  					testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="),
   388  				),
   389  			},
   390  			{
   391  				PreConfig: func() {
   392  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile)
   393  				},
   394  				Config: genAWSLambdaFunctionConfig_local(path, rInt, rName),
   395  				Check: resource.ComposeTestCheckFunc(
   396  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", rName, &conf),
   397  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   398  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName),
   399  					testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="),
   400  				),
   401  			},
   402  		},
   403  	})
   404  }
   405  
   406  func TestAccAWSLambdaFunction_localUpdate_nameOnly(t *testing.T) {
   407  	var conf lambda.GetFunctionOutput
   408  
   409  	rName := fmt.Sprintf("tf_test_iam_%d", acctest.RandInt())
   410  
   411  	path, zipFile, err := createTempFile("lambda_localUpdate")
   412  	if err != nil {
   413  		t.Fatal(err)
   414  	}
   415  	defer os.Remove(path)
   416  
   417  	updatedPath, updatedZipFile, err := createTempFile("lambda_localUpdate_name_change")
   418  	if err != nil {
   419  		t.Fatal(err)
   420  	}
   421  	defer os.Remove(updatedPath)
   422  
   423  	resource.Test(t, resource.TestCase{
   424  		PreCheck:     func() { testAccPreCheck(t) },
   425  		Providers:    testAccProviders,
   426  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   427  		Steps: []resource.TestStep{
   428  			{
   429  				PreConfig: func() {
   430  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
   431  				},
   432  				Config: genAWSLambdaFunctionConfig_local_name_only(path, rName),
   433  				Check: resource.ComposeTestCheckFunc(
   434  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", rName, &conf),
   435  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   436  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName),
   437  					testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="),
   438  				),
   439  			},
   440  			{
   441  				PreConfig: func() {
   442  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, updatedZipFile)
   443  				},
   444  				Config: genAWSLambdaFunctionConfig_local_name_only(updatedPath, rName),
   445  				Check: resource.ComposeTestCheckFunc(
   446  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", rName, &conf),
   447  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   448  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName),
   449  					testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="),
   450  				),
   451  			},
   452  		},
   453  	})
   454  }
   455  
   456  func TestAccAWSLambdaFunction_s3Update_basic(t *testing.T) {
   457  	var conf lambda.GetFunctionOutput
   458  
   459  	path, zipFile, err := createTempFile("lambda_s3Update")
   460  	if err != nil {
   461  		t.Fatal(err)
   462  	}
   463  	defer os.Remove(path)
   464  
   465  	bucketName := fmt.Sprintf("tf-acc-lambda-s3-deployments-%d", randomInteger)
   466  	key := "lambda-func.zip"
   467  
   468  	rInt := acctest.RandInt()
   469  
   470  	rName := fmt.Sprintf("tf_acc_lambda_%d", rInt)
   471  
   472  	resource.Test(t, resource.TestCase{
   473  		PreCheck:     func() { testAccPreCheck(t) },
   474  		Providers:    testAccProviders,
   475  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   476  		Steps: []resource.TestStep{
   477  			{
   478  				PreConfig: func() {
   479  					// Upload 1st version
   480  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
   481  				},
   482  				Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path, rInt, rName),
   483  				Check: resource.ComposeTestCheckFunc(
   484  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", rName, &conf),
   485  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   486  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName),
   487  					testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="),
   488  				),
   489  			},
   490  			{
   491  				ExpectNonEmptyPlan: true,
   492  				PreConfig: func() {
   493  					// Upload 2nd version
   494  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile)
   495  				},
   496  				Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path, rInt, rName),
   497  			},
   498  			// Extra step because of missing ComputedWhen
   499  			// See https://github.com/hashicorp/terraform/pull/4846 & https://github.com/hashicorp/terraform/pull/5330
   500  			{
   501  				Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path, rInt, rName),
   502  				Check: resource.ComposeTestCheckFunc(
   503  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", rName, &conf),
   504  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   505  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName),
   506  					testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="),
   507  				),
   508  			},
   509  		},
   510  	})
   511  }
   512  
   513  func TestAccAWSLambdaFunction_s3Update_unversioned(t *testing.T) {
   514  	var conf lambda.GetFunctionOutput
   515  
   516  	rName := fmt.Sprintf("tf_iam_lambda_%d", acctest.RandInt())
   517  
   518  	path, zipFile, err := createTempFile("lambda_s3Update")
   519  	if err != nil {
   520  		t.Fatal(err)
   521  	}
   522  	defer os.Remove(path)
   523  
   524  	bucketName := fmt.Sprintf("tf-acc-lambda-s3-deployments-%d", randomInteger)
   525  	key := "lambda-func.zip"
   526  	key2 := "lambda-func-modified.zip"
   527  
   528  	resource.Test(t, resource.TestCase{
   529  		PreCheck:     func() { testAccPreCheck(t) },
   530  		Providers:    testAccProviders,
   531  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   532  		Steps: []resource.TestStep{
   533  			{
   534  				PreConfig: func() {
   535  					// Upload 1st version
   536  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
   537  				},
   538  				Config: testAccAWSLambdaFunctionConfig_s3_unversioned_tpl(rName, bucketName, key, path),
   539  				Check: resource.ComposeTestCheckFunc(
   540  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3_unversioned", &conf),
   541  					testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3_unversioned"),
   542  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_s3_unversioned"),
   543  					testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="),
   544  				),
   545  			},
   546  			{
   547  				PreConfig: func() {
   548  					// Upload 2nd version
   549  					testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile)
   550  				},
   551  				Config: testAccAWSLambdaFunctionConfig_s3_unversioned_tpl(rName, bucketName, key2, path),
   552  				Check: resource.ComposeTestCheckFunc(
   553  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3_unversioned", &conf),
   554  					testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3_unversioned"),
   555  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_s3_unversioned"),
   556  					testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="),
   557  				),
   558  			},
   559  		},
   560  	})
   561  }
   562  
   563  func TestAccAWSLambdaFunction_runtimeValidation_noRuntime(t *testing.T) {
   564  	rSt := acctest.RandString(5)
   565  	rName := fmt.Sprintf("tf_test_%s", rSt)
   566  
   567  	resource.Test(t, resource.TestCase{
   568  		PreCheck:     func() { testAccPreCheck(t) },
   569  		Providers:    testAccProviders,
   570  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   571  		Steps: []resource.TestStep{
   572  			{
   573  				Config:      testAccAWSLambdaConfigNoRuntime(rName, rSt),
   574  				ExpectError: regexp.MustCompile(`\\"runtime\\": required field is not set`),
   575  			},
   576  		},
   577  	})
   578  }
   579  
   580  func TestAccAWSLambdaFunction_runtimeValidation_nodeJs(t *testing.T) {
   581  	rSt := acctest.RandString(5)
   582  	rName := fmt.Sprintf("tf_test_%s", rSt)
   583  
   584  	resource.Test(t, resource.TestCase{
   585  		PreCheck:     func() { testAccPreCheck(t) },
   586  		Providers:    testAccProviders,
   587  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   588  		Steps: []resource.TestStep{
   589  			{
   590  				Config:      testAccAWSLambdaConfigNodeJsRuntime(rName, rSt),
   591  				ExpectError: regexp.MustCompile(fmt.Sprintf("%s has reached end of life since October 2016 and has been deprecated in favor of %s", lambda.RuntimeNodejs, lambda.RuntimeNodejs43)),
   592  			},
   593  		},
   594  	})
   595  }
   596  
   597  func TestAccAWSLambdaFunction_runtimeValidation_nodeJs43(t *testing.T) {
   598  	var conf lambda.GetFunctionOutput
   599  	rSt := acctest.RandString(5)
   600  	rName := fmt.Sprintf("tf_test_%s", rSt)
   601  
   602  	resource.Test(t, resource.TestCase{
   603  		PreCheck:     func() { testAccPreCheck(t) },
   604  		Providers:    testAccProviders,
   605  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   606  		Steps: []resource.TestStep{
   607  			{
   608  				Config: testAccAWSLambdaConfigNodeJs43Runtime(rName, rSt),
   609  				Check: resource.ComposeTestCheckFunc(
   610  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   611  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "runtime", lambda.RuntimeNodejs43),
   612  				),
   613  			},
   614  		},
   615  	})
   616  }
   617  
   618  func TestAccAWSLambdaFunction_runtimeValidation_python27(t *testing.T) {
   619  	var conf lambda.GetFunctionOutput
   620  	rSt := acctest.RandString(5)
   621  	rName := fmt.Sprintf("tf_test_%s", rSt)
   622  
   623  	resource.Test(t, resource.TestCase{
   624  		PreCheck:     func() { testAccPreCheck(t) },
   625  		Providers:    testAccProviders,
   626  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   627  		Steps: []resource.TestStep{
   628  			{
   629  				Config: testAccAWSLambdaConfigPython27Runtime(rName, rSt),
   630  				Check: resource.ComposeTestCheckFunc(
   631  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   632  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "runtime", lambda.RuntimePython27),
   633  				),
   634  			},
   635  		},
   636  	})
   637  }
   638  
   639  func TestAccAWSLambdaFunction_runtimeValidation_java8(t *testing.T) {
   640  	var conf lambda.GetFunctionOutput
   641  	rSt := acctest.RandString(5)
   642  	rName := fmt.Sprintf("tf_test_%s", rSt)
   643  
   644  	resource.Test(t, resource.TestCase{
   645  		PreCheck:     func() { testAccPreCheck(t) },
   646  		Providers:    testAccProviders,
   647  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   648  		Steps: []resource.TestStep{
   649  			{
   650  				Config: testAccAWSLambdaConfigJava8Runtime(rName, rSt),
   651  				Check: resource.ComposeTestCheckFunc(
   652  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   653  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "runtime", lambda.RuntimeJava8),
   654  				),
   655  			},
   656  		},
   657  	})
   658  }
   659  
   660  func TestAccAWSLambdaFunction_tags(t *testing.T) {
   661  	var conf lambda.GetFunctionOutput
   662  
   663  	rSt := acctest.RandString(5)
   664  	rName := fmt.Sprintf("tf_test_%s", rSt)
   665  
   666  	resource.Test(t, resource.TestCase{
   667  		PreCheck:     func() { testAccPreCheck(t) },
   668  		Providers:    testAccProviders,
   669  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   670  		Steps: []resource.TestStep{
   671  			{
   672  				Config: testAccAWSLambdaConfigBasic(rName, rSt),
   673  				Check: resource.ComposeTestCheckFunc(
   674  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   675  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   676  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   677  					resource.TestCheckNoResourceAttr("aws_lambda_function.lambda_function_test", "tags"),
   678  				),
   679  			},
   680  			{
   681  				Config: testAccAWSLambdaConfigTags(rName, rSt),
   682  				Check: resource.ComposeTestCheckFunc(
   683  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   684  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   685  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   686  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.%", "2"),
   687  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.Key1", "Value One"),
   688  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.Description", "Very interesting"),
   689  				),
   690  			},
   691  			{
   692  				Config: testAccAWSLambdaConfigTagsModified(rName, rSt),
   693  				Check: resource.ComposeTestCheckFunc(
   694  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   695  					testAccCheckAwsLambdaFunctionName(&conf, rName),
   696  					testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName),
   697  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.%", "3"),
   698  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.Key1", "Value One Changed"),
   699  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.Key2", "Value Two"),
   700  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.Key3", "Value Three"),
   701  				),
   702  			},
   703  		},
   704  	})
   705  }
   706  
   707  func TestAccAWSLambdaFunction_runtimeValidation_python36(t *testing.T) {
   708  	var conf lambda.GetFunctionOutput
   709  	rSt := acctest.RandString(5)
   710  	rName := fmt.Sprintf("tf_test_%s", rSt)
   711  
   712  	resource.Test(t, resource.TestCase{
   713  		PreCheck:     func() { testAccPreCheck(t) },
   714  		Providers:    testAccProviders,
   715  		CheckDestroy: testAccCheckLambdaFunctionDestroy,
   716  		Steps: []resource.TestStep{
   717  			{
   718  				Config: testAccAWSLambdaConfigPython36Runtime(rName, rSt),
   719  				Check: resource.ComposeTestCheckFunc(
   720  					testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf),
   721  					resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "runtime", lambda.RuntimePython36),
   722  				),
   723  			},
   724  		},
   725  	})
   726  }
   727  
   728  func testAccCheckLambdaFunctionDestroy(s *terraform.State) error {
   729  	conn := testAccProvider.Meta().(*AWSClient).lambdaconn
   730  
   731  	for _, rs := range s.RootModule().Resources {
   732  		if rs.Type != "aws_lambda_function" {
   733  			continue
   734  		}
   735  
   736  		_, err := conn.GetFunction(&lambda.GetFunctionInput{
   737  			FunctionName: aws.String(rs.Primary.ID),
   738  		})
   739  
   740  		if err == nil {
   741  			return fmt.Errorf("Lambda Function still exists")
   742  		}
   743  
   744  	}
   745  
   746  	return nil
   747  
   748  }
   749  
   750  func testAccCheckAwsLambdaFunctionExists(res, funcName string, function *lambda.GetFunctionOutput) resource.TestCheckFunc {
   751  	// Wait for IAM role
   752  	return func(s *terraform.State) error {
   753  		rs, ok := s.RootModule().Resources[res]
   754  		if !ok {
   755  			return fmt.Errorf("Lambda function not found: %s", res)
   756  		}
   757  
   758  		if rs.Primary.ID == "" {
   759  			return fmt.Errorf("Lambda function ID not set")
   760  		}
   761  
   762  		conn := testAccProvider.Meta().(*AWSClient).lambdaconn
   763  
   764  		params := &lambda.GetFunctionInput{
   765  			FunctionName: aws.String(funcName),
   766  		}
   767  
   768  		getFunction, err := conn.GetFunction(params)
   769  		if err != nil {
   770  			return err
   771  		}
   772  
   773  		*function = *getFunction
   774  
   775  		return nil
   776  	}
   777  }
   778  
   779  func testAccAwsInvokeLambdaFunction(function *lambda.GetFunctionOutput) resource.TestCheckFunc {
   780  	return func(s *terraform.State) error {
   781  		f := function.Configuration
   782  		conn := testAccProvider.Meta().(*AWSClient).lambdaconn
   783  
   784  		// If the function is VPC-enabled this will create ENI automatically
   785  		_, err := conn.Invoke(&lambda.InvokeInput{
   786  			FunctionName: f.FunctionName,
   787  		})
   788  
   789  		return err
   790  	}
   791  }
   792  
   793  func testAccCheckAwsLambdaFunctionName(function *lambda.GetFunctionOutput, expectedName string) resource.TestCheckFunc {
   794  	return func(s *terraform.State) error {
   795  		c := function.Configuration
   796  		if *c.FunctionName != expectedName {
   797  			return fmt.Errorf("Expected function name %s, got %s", expectedName, *c.FunctionName)
   798  		}
   799  
   800  		return nil
   801  	}
   802  }
   803  
   804  func testAccCheckAWSLambdaFunctionVersion(function *lambda.GetFunctionOutput, expectedVersion string) resource.TestCheckFunc {
   805  	return func(s *terraform.State) error {
   806  		c := function.Configuration
   807  		if *c.Version != expectedVersion {
   808  			return fmt.Errorf("Expected version %s, got %s", expectedVersion, *c.Version)
   809  		}
   810  		return nil
   811  	}
   812  }
   813  
   814  func testAccCheckAwsLambdaFunctionArnHasSuffix(function *lambda.GetFunctionOutput, arnSuffix string) resource.TestCheckFunc {
   815  	return func(s *terraform.State) error {
   816  		c := function.Configuration
   817  		if !strings.HasSuffix(*c.FunctionArn, arnSuffix) {
   818  			return fmt.Errorf("Expected function ARN %s to have suffix %s", *c.FunctionArn, arnSuffix)
   819  		}
   820  
   821  		return nil
   822  	}
   823  }
   824  
   825  func testAccCheckAwsLambdaSourceCodeHash(function *lambda.GetFunctionOutput, expectedHash string) resource.TestCheckFunc {
   826  	return func(s *terraform.State) error {
   827  		c := function.Configuration
   828  		if *c.CodeSha256 != expectedHash {
   829  			return fmt.Errorf("Expected code hash %s, got %s", expectedHash, *c.CodeSha256)
   830  		}
   831  
   832  		return nil
   833  	}
   834  }
   835  
   836  func testAccCreateZipFromFiles(files map[string]string, zipFile *os.File) error {
   837  	zipFile.Truncate(0)
   838  	zipFile.Seek(0, 0)
   839  
   840  	w := zip.NewWriter(zipFile)
   841  
   842  	for source, destination := range files {
   843  		f, err := w.Create(destination)
   844  		if err != nil {
   845  			return err
   846  		}
   847  
   848  		fileContent, err := ioutil.ReadFile(source)
   849  		if err != nil {
   850  			return err
   851  		}
   852  
   853  		_, err = f.Write(fileContent)
   854  		if err != nil {
   855  			return err
   856  		}
   857  	}
   858  
   859  	err := w.Close()
   860  	if err != nil {
   861  		return err
   862  	}
   863  
   864  	return w.Flush()
   865  }
   866  
   867  func createTempFile(prefix string) (string, *os.File, error) {
   868  	f, err := ioutil.TempFile(os.TempDir(), prefix)
   869  	if err != nil {
   870  		return "", nil, err
   871  	}
   872  
   873  	pathToFile, err := filepath.Abs(f.Name())
   874  	if err != nil {
   875  		return "", nil, err
   876  	}
   877  	return pathToFile, f, nil
   878  }
   879  
   880  func baseAccAWSLambdaConfig(rst string) string {
   881  	return fmt.Sprintf(`
   882  resource "aws_iam_role_policy" "iam_policy_for_lambda" {
   883      name = "iam_policy_for_lambda_%s"
   884      role = "${aws_iam_role.iam_for_lambda.id}"
   885      policy = <<EOF
   886  {
   887    "Version": "2012-10-17",
   888    "Statement": [
   889          {
   890              "Effect": "Allow",
   891              "Action": [
   892                  "logs:CreateLogGroup",
   893                  "logs:CreateLogStream",
   894                  "logs:PutLogEvents"
   895              ],
   896              "Resource": "arn:aws:logs:*:*:*"
   897          },
   898      {
   899        "Effect": "Allow",
   900        "Action": [
   901          "ec2:CreateNetworkInterface",
   902  				"ec2:DescribeNetworkInterfaces",
   903  				"ec2:DeleteNetworkInterface"
   904        ],
   905        "Resource": [
   906          "*"
   907        ]
   908      },
   909      {
   910        "Effect": "Allow",
   911        "Action": [
   912          "SNS:Publish"
   913        ],
   914        "Resource": [
   915          "*"
   916        ]
   917      },
   918      {
   919        "Effect": "Allow",
   920        "Action": [
   921          "xray:PutTraceSegments"
   922        ],
   923        "Resource": [
   924          "*"
   925        ]
   926      }
   927    ]
   928  }
   929  EOF
   930  }
   931  
   932  resource "aws_iam_role" "iam_for_lambda" {
   933      name = "iam_for_lambda_%s"
   934      assume_role_policy = <<EOF
   935  {
   936    "Version": "2012-10-17",
   937    "Statement": [
   938      {
   939        "Action": "sts:AssumeRole",
   940        "Principal": {
   941          "Service": "lambda.amazonaws.com"
   942        },
   943        "Effect": "Allow",
   944        "Sid": ""
   945      }
   946    ]
   947  }
   948  EOF
   949  }
   950  
   951  resource "aws_vpc" "vpc_for_lambda" {
   952      cidr_block = "10.0.0.0/16"
   953  		tags {
   954  			Name = "baseAccAWSLambdaConfig"
   955  		}
   956  }
   957  
   958  resource "aws_subnet" "subnet_for_lambda" {
   959      vpc_id = "${aws_vpc.vpc_for_lambda.id}"
   960      cidr_block = "10.0.1.0/24"
   961  
   962      tags {
   963          Name = "lambda"
   964      }
   965  }
   966  
   967  resource "aws_security_group" "sg_for_lambda" {
   968    name = "sg_for_lambda_%s"
   969    description = "Allow all inbound traffic for lambda test"
   970    vpc_id = "${aws_vpc.vpc_for_lambda.id}"
   971  
   972    ingress {
   973        from_port = 0
   974        to_port = 0
   975        protocol = "-1"
   976        cidr_blocks = ["0.0.0.0/0"]
   977    }
   978  
   979    egress {
   980        from_port = 0
   981        to_port = 0
   982        protocol = "-1"
   983        cidr_blocks = ["0.0.0.0/0"]
   984    }
   985  }`, rst, rst, rst)
   986  }
   987  
   988  func testAccAWSLambdaConfigBasic(rName, rSt string) string {
   989  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
   990  resource "aws_lambda_function" "lambda_function_test" {
   991      filename = "test-fixtures/lambdatest.zip"
   992      function_name = "%s"
   993      role = "${aws_iam_role.iam_for_lambda.arn}"
   994      handler = "exports.example"
   995      runtime = "nodejs4.3"
   996  }
   997  `, rName)
   998  }
   999  
  1000  func testAccAWSLambdaConfigBasicUpdateRuntime(rName, rSt string) string {
  1001  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1002  resource "aws_lambda_function" "lambda_function_test" {
  1003      filename = "test-fixtures/lambdatest.zip"
  1004      function_name = "%s"
  1005      role = "${aws_iam_role.iam_for_lambda.arn}"
  1006      handler = "exports.example"
  1007      runtime = "nodejs4.3-edge"
  1008  }
  1009  `, rName)
  1010  }
  1011  
  1012  func testAccAWSLambdaConfigWithoutFilenameAndS3Attributes(rName, rSt string) string {
  1013  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1014  resource "aws_lambda_function" "lambda_function_test" {
  1015      function_name = "%s"
  1016      role = "${aws_iam_role.iam_for_lambda.arn}"
  1017      handler = "exports.example"
  1018  		runtime = "nodejs4.3"
  1019  }
  1020  `, rName)
  1021  }
  1022  
  1023  func testAccAWSLambdaConfigEnvVariables(rName, rSt string) string {
  1024  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1025  resource "aws_lambda_function" "lambda_function_test" {
  1026      filename = "test-fixtures/lambdatest.zip"
  1027      function_name = "%s"
  1028      role = "${aws_iam_role.iam_for_lambda.arn}"
  1029      handler = "exports.example"
  1030      runtime = "nodejs4.3"
  1031      environment {
  1032          variables = {
  1033              foo = "bar"
  1034          }
  1035      }
  1036  }
  1037  `, rName)
  1038  }
  1039  
  1040  func testAccAWSLambdaConfigEnvVariablesModified(rName, rSt string) string {
  1041  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1042  resource "aws_lambda_function" "lambda_function_test" {
  1043      filename = "test-fixtures/lambdatest.zip"
  1044      function_name = "%s"
  1045      role = "${aws_iam_role.iam_for_lambda.arn}"
  1046      handler = "exports.example"
  1047      runtime = "nodejs4.3"
  1048      environment {
  1049          variables = {
  1050              foo = "baz"
  1051              foo1 = "bar1"
  1052          }
  1053      }
  1054  }
  1055  `, rName)
  1056  }
  1057  
  1058  func testAccAWSLambdaConfigEnvVariablesModifiedWithoutEnvironment(rName, rSt string) string {
  1059  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1060  resource "aws_lambda_function" "lambda_function_test" {
  1061      filename = "test-fixtures/lambdatest.zip"
  1062      function_name = "%s"
  1063      role = "${aws_iam_role.iam_for_lambda.arn}"
  1064      handler = "exports.example"
  1065      runtime = "nodejs4.3"
  1066  }
  1067  `, rName)
  1068  }
  1069  
  1070  func testAccAWSLambdaConfigEncryptedEnvVariables(rName, rSt string) string {
  1071  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1072  resource "aws_kms_key" "foo" {
  1073      description = "Terraform acc test %s"
  1074      policy = <<POLICY
  1075  {
  1076    "Version": "2012-10-17",
  1077    "Id": "kms-tf-1",
  1078    "Statement": [
  1079      {
  1080        "Sid": "Enable IAM User Permissions",
  1081        "Effect": "Allow",
  1082        "Principal": {
  1083          "AWS": "*"
  1084        },
  1085        "Action": "kms:*",
  1086        "Resource": "*"
  1087      }
  1088    ]
  1089  }
  1090  POLICY
  1091  }
  1092  
  1093  resource "aws_lambda_function" "lambda_function_test" {
  1094      filename = "test-fixtures/lambdatest.zip"
  1095      function_name = "%s"
  1096      role = "${aws_iam_role.iam_for_lambda.arn}"
  1097      handler = "exports.example"
  1098      kms_key_arn = "${aws_kms_key.foo.arn}"
  1099      runtime = "nodejs4.3"
  1100      environment {
  1101          variables = {
  1102              foo = "bar"
  1103          }
  1104      }
  1105  }
  1106  `, rName, rName)
  1107  }
  1108  
  1109  func testAccAWSLambdaConfigEncryptedEnvVariablesModified(rName, rSt string) string {
  1110  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1111  resource "aws_lambda_function" "lambda_function_test" {
  1112      filename = "test-fixtures/lambdatest.zip"
  1113      function_name = "%s"
  1114      role = "${aws_iam_role.iam_for_lambda.arn}"
  1115      handler = "exports.example"
  1116      runtime = "nodejs4.3"
  1117      environment {
  1118          variables = {
  1119              foo = "bar"
  1120          }
  1121      }
  1122  }
  1123  `, rName)
  1124  }
  1125  
  1126  func testAccAWSLambdaConfigVersioned(rName, rSt string) string {
  1127  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1128  resource "aws_lambda_function" "lambda_function_test" {
  1129      filename = "test-fixtures/lambdatest.zip"
  1130      function_name = "%s"
  1131      publish = true
  1132      role = "${aws_iam_role.iam_for_lambda.arn}"
  1133      handler = "exports.example"
  1134      runtime = "nodejs4.3"
  1135  }
  1136  `, rName)
  1137  }
  1138  
  1139  func testAccAWSLambdaConfigWithTracingConfig(rName, rSt string) string {
  1140  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1141  resource "aws_lambda_function" "lambda_function_test" {
  1142      filename = "test-fixtures/lambdatest.zip"
  1143      function_name = "%s"
  1144      role = "${aws_iam_role.iam_for_lambda.arn}"
  1145      handler = "exports.example"
  1146      runtime = "nodejs4.3"
  1147  
  1148      tracing_config {
  1149          mode = "Active"
  1150      }
  1151  }
  1152  
  1153  `, rName)
  1154  }
  1155  
  1156  func testAccAWSLambdaConfigWithTracingConfigUpdated(rName, rSt string) string {
  1157  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1158  resource "aws_lambda_function" "lambda_function_test" {
  1159      filename = "test-fixtures/lambdatest.zip"
  1160      function_name = "%s"
  1161      role = "${aws_iam_role.iam_for_lambda.arn}"
  1162      handler = "exports.example"
  1163      runtime = "nodejs4.3"
  1164  
  1165      tracing_config {
  1166          mode = "PassThrough"
  1167      }
  1168  }
  1169  
  1170  `, rName)
  1171  }
  1172  
  1173  func testAccAWSLambdaConfigWithDeadLetterConfig(rName, rSt string) string {
  1174  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1175  resource "aws_lambda_function" "lambda_function_test" {
  1176      filename = "test-fixtures/lambdatest.zip"
  1177      function_name = "%s"
  1178      role = "${aws_iam_role.iam_for_lambda.arn}"
  1179      handler = "exports.example"
  1180      runtime = "nodejs4.3"
  1181  
  1182      dead_letter_config {
  1183          target_arn = "${aws_sns_topic.lambda_function_test.arn}"
  1184      }
  1185  }
  1186  
  1187  resource "aws_sns_topic" "lambda_function_test" {
  1188  	name = "%s"
  1189  }
  1190  
  1191  `, rName, rName)
  1192  }
  1193  
  1194  func testAccAWSLambdaConfigWithNilDeadLetterConfig(rName, rSt string) string {
  1195  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1196  resource "aws_lambda_function" "lambda_function_test" {
  1197      filename = "test-fixtures/lambdatest.zip"
  1198      function_name = "%s"
  1199      role = "${aws_iam_role.iam_for_lambda.arn}"
  1200      handler = "exports.example"
  1201      runtime = "nodejs4.3"
  1202  
  1203      dead_letter_config {
  1204          target_arn = ""
  1205      }
  1206  }
  1207  `, rName)
  1208  }
  1209  
  1210  func testAccAWSLambdaConfigWithVPC(rName, rSt string) string {
  1211  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1212  resource "aws_lambda_function" "lambda_function_test" {
  1213      filename = "test-fixtures/lambdatest.zip"
  1214      function_name = "%s"
  1215      role = "${aws_iam_role.iam_for_lambda.arn}"
  1216      handler = "exports.example"
  1217      runtime = "nodejs4.3"
  1218  
  1219      vpc_config = {
  1220          subnet_ids = ["${aws_subnet.subnet_for_lambda.id}"]
  1221          security_group_ids = ["${aws_security_group.sg_for_lambda.id}"]
  1222      }
  1223  }`, rName)
  1224  }
  1225  
  1226  func testAccAWSLambdaConfigS3(rName, rSt string) string {
  1227  	return fmt.Sprintf(`
  1228  resource "aws_s3_bucket" "lambda_bucket" {
  1229    bucket = "tf-test-bucket-%d"
  1230  }
  1231  
  1232  resource "aws_s3_bucket_object" "lambda_code" {
  1233    bucket = "${aws_s3_bucket.lambda_bucket.id}"
  1234    key = "lambdatest.zip"
  1235    source = "test-fixtures/lambdatest.zip"
  1236  }
  1237  
  1238  resource "aws_iam_role" "iam_for_lambda" {
  1239      name = "iam_for_lambda_%s"
  1240      assume_role_policy = <<EOF
  1241  {
  1242    "Version": "2012-10-17",
  1243    "Statement": [
  1244      {
  1245        "Action": "sts:AssumeRole",
  1246        "Principal": {
  1247          "Service": "lambda.amazonaws.com"
  1248        },
  1249        "Effect": "Allow",
  1250        "Sid": ""
  1251      }
  1252    ]
  1253  }
  1254  EOF
  1255  }
  1256  
  1257  resource "aws_lambda_function" "lambda_function_s3test" {
  1258      s3_bucket = "${aws_s3_bucket.lambda_bucket.id}"
  1259      s3_key = "${aws_s3_bucket_object.lambda_code.id}"
  1260      function_name = "%s"
  1261      role = "${aws_iam_role.iam_for_lambda.arn}"
  1262      handler = "exports.example"
  1263      runtime = "nodejs4.3"
  1264  }
  1265  `, acctest.RandInt(), rSt, rName)
  1266  }
  1267  
  1268  func testAccAWSLambdaConfigNoRuntime(rName, rSt string) string {
  1269  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1270  resource "aws_lambda_function" "lambda_function_test" {
  1271      filename = "test-fixtures/lambdatest.zip"
  1272      function_name = "%s"
  1273      role = "${aws_iam_role.iam_for_lambda.arn}"
  1274      handler = "exports.example"
  1275      runtime = "nodejs4.3"
  1276  }
  1277  `, rName)
  1278  }
  1279  
  1280  func testAccAWSLambdaConfigNodeJsRuntime(rName, rSt string) string {
  1281  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1282  resource "aws_lambda_function" "lambda_function_test" {
  1283      filename = "test-fixtures/lambdatest.zip"
  1284      function_name = "%s"
  1285      role = "${aws_iam_role.iam_for_lambda.arn}"
  1286      handler = "exports.example"
  1287      runtime = "nodejs4.3"
  1288  }
  1289  `, rName)
  1290  }
  1291  
  1292  func testAccAWSLambdaConfigNodeJs43Runtime(rName, rSt string) string {
  1293  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1294  resource "aws_lambda_function" "lambda_function_test" {
  1295      filename = "test-fixtures/lambdatest.zip"
  1296      function_name = "%s"
  1297      role = "${aws_iam_role.iam_for_lambda.arn}"
  1298      handler = "exports.example"
  1299      runtime = "nodejs4.3"
  1300  }
  1301  `, rName)
  1302  }
  1303  
  1304  func testAccAWSLambdaConfigPython27Runtime(rName, rSt string) string {
  1305  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1306  resource "aws_lambda_function" "lambda_function_test" {
  1307      filename = "test-fixtures/lambdatest.zip"
  1308      function_name = "%s"
  1309      role = "${aws_iam_role.iam_for_lambda.arn}"
  1310      handler = "exports.example"
  1311      runtime = "python2.7"
  1312  }
  1313  `, rName)
  1314  }
  1315  
  1316  func testAccAWSLambdaConfigJava8Runtime(rName, rSt string) string {
  1317  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1318  resource "aws_lambda_function" "lambda_function_test" {
  1319      filename = "test-fixtures/lambdatest.zip"
  1320      function_name = "%s"
  1321      role = "${aws_iam_role.iam_for_lambda.arn}"
  1322      handler = "exports.example"
  1323      runtime = "java8"
  1324  }
  1325  `, rName)
  1326  }
  1327  
  1328  func testAccAWSLambdaConfigTags(rName, rSt string) string {
  1329  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1330  resource "aws_lambda_function" "lambda_function_test" {
  1331      filename = "test-fixtures/lambdatest.zip"
  1332      function_name = "%s"
  1333      role = "${aws_iam_role.iam_for_lambda.arn}"
  1334      handler = "exports.example"
  1335      runtime = "nodejs4.3"
  1336      tags {
  1337  		Key1 = "Value One"
  1338  		Description = "Very interesting"
  1339      }
  1340  }
  1341  `, rName)
  1342  }
  1343  
  1344  func testAccAWSLambdaConfigTagsModified(rName, rSt string) string {
  1345  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1346  resource "aws_lambda_function" "lambda_function_test" {
  1347      filename = "test-fixtures/lambdatest.zip"
  1348      function_name = "%s"
  1349      role = "${aws_iam_role.iam_for_lambda.arn}"
  1350      handler = "exports.example"
  1351      runtime = "nodejs4.3"
  1352      tags {
  1353  		Key1 = "Value One Changed"
  1354  		Key2 = "Value Two"
  1355  		Key3 = "Value Three"
  1356      }
  1357  }
  1358  `, rName)
  1359  }
  1360  
  1361  func testAccAWSLambdaConfigPython36Runtime(rName, rSt string) string {
  1362  	return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+`
  1363  resource "aws_lambda_function" "lambda_function_test" {
  1364      filename = "test-fixtures/lambdatest.zip"
  1365      function_name = "%s"
  1366      role = "${aws_iam_role.iam_for_lambda.arn}"
  1367      handler = "exports.example"
  1368      runtime = "python3.6"
  1369  }
  1370  `, rName)
  1371  }
  1372  
  1373  const testAccAWSLambdaFunctionConfig_local_tpl = `
  1374  resource "aws_iam_role" "iam_for_lambda" {
  1375      name = "iam_for_lambda_%d"
  1376      assume_role_policy = <<EOF
  1377  {
  1378    "Version": "2012-10-17",
  1379    "Statement": [
  1380      {
  1381        "Action": "sts:AssumeRole",
  1382        "Principal": {
  1383          "Service": "lambda.amazonaws.com"
  1384        },
  1385        "Effect": "Allow",
  1386        "Sid": ""
  1387      }
  1388    ]
  1389  }
  1390  EOF
  1391  }
  1392  resource "aws_lambda_function" "lambda_function_local" {
  1393      filename = "%s"
  1394      source_code_hash = "${base64sha256(file("%s"))}"
  1395      function_name = "%s"
  1396      role = "${aws_iam_role.iam_for_lambda.arn}"
  1397      handler = "exports.example"
  1398      runtime = "nodejs4.3"
  1399  }
  1400  `
  1401  
  1402  func genAWSLambdaFunctionConfig_local(filePath string, rInt int, rName string) string {
  1403  	return fmt.Sprintf(testAccAWSLambdaFunctionConfig_local_tpl, rInt,
  1404  		filePath, filePath, rName)
  1405  }
  1406  
  1407  func genAWSLambdaFunctionConfig_local_name_only(filePath, rName string) string {
  1408  	return testAccAWSLambdaFunctionConfig_local_name_only_tpl(filePath, rName)
  1409  }
  1410  
  1411  func testAccAWSLambdaFunctionConfig_local_name_only_tpl(filePath, rName string) string {
  1412  	return fmt.Sprintf(`
  1413  resource "aws_iam_role" "iam_for_lambda" {
  1414      name = "%s"
  1415      assume_role_policy = <<EOF
  1416  {
  1417    "Version": "2012-10-17",
  1418    "Statement": [
  1419      {
  1420        "Action": "sts:AssumeRole",
  1421        "Principal": {
  1422          "Service": "lambda.amazonaws.com"
  1423        },
  1424        "Effect": "Allow",
  1425        "Sid": ""
  1426      }
  1427    ]
  1428  }
  1429  EOF
  1430  }
  1431  resource "aws_lambda_function" "lambda_function_local" {
  1432      filename = "%s"
  1433      function_name = "%s"
  1434      role = "${aws_iam_role.iam_for_lambda.arn}"
  1435      handler = "exports.example"
  1436      runtime = "nodejs4.3"
  1437  }`, rName, filePath, rName)
  1438  }
  1439  
  1440  const testAccAWSLambdaFunctionConfig_s3_tpl = `
  1441  resource "aws_s3_bucket" "artifacts" {
  1442      bucket = "%s"
  1443      acl = "private"
  1444      force_destroy = true
  1445      versioning {
  1446          enabled = true
  1447      }
  1448  }
  1449  resource "aws_s3_bucket_object" "o" {
  1450      bucket = "${aws_s3_bucket.artifacts.bucket}"
  1451      key = "%s"
  1452      source = "%s"
  1453      etag = "${md5(file("%s"))}"
  1454  }
  1455  resource "aws_iam_role" "iam_for_lambda" {
  1456      name = "iam_for_lambda_%d"
  1457      assume_role_policy = <<EOF
  1458  {
  1459    "Version": "2012-10-17",
  1460    "Statement": [
  1461      {
  1462        "Action": "sts:AssumeRole",
  1463        "Principal": {
  1464          "Service": "lambda.amazonaws.com"
  1465        },
  1466        "Effect": "Allow",
  1467        "Sid": ""
  1468      }
  1469    ]
  1470  }
  1471  EOF
  1472  }
  1473  resource "aws_lambda_function" "lambda_function_s3" {
  1474      s3_bucket = "${aws_s3_bucket_object.o.bucket}"
  1475      s3_key = "${aws_s3_bucket_object.o.key}"
  1476      s3_object_version = "${aws_s3_bucket_object.o.version_id}"
  1477      function_name = "%s"
  1478      role = "${aws_iam_role.iam_for_lambda.arn}"
  1479      handler = "exports.example"
  1480      runtime = "nodejs4.3"
  1481  }
  1482  `
  1483  
  1484  func genAWSLambdaFunctionConfig_s3(bucket, key, path string, rInt int, rName string) string {
  1485  	return fmt.Sprintf(testAccAWSLambdaFunctionConfig_s3_tpl,
  1486  		bucket, key, path, path, rInt, rName)
  1487  }
  1488  
  1489  func testAccAWSLambdaFunctionConfig_s3_unversioned_tpl(rName, bucketName, key, path string) string {
  1490  	return fmt.Sprintf(`
  1491  resource "aws_s3_bucket" "artifacts" {
  1492      bucket = "%s"
  1493      acl = "private"
  1494      force_destroy = true
  1495  }
  1496  resource "aws_s3_bucket_object" "o" {
  1497      bucket = "${aws_s3_bucket.artifacts.bucket}"
  1498      key = "%s"
  1499      source = "%s"
  1500      etag = "${md5(file("%s"))}"
  1501  }
  1502  resource "aws_iam_role" "iam_for_lambda" {
  1503  		name = "%s"
  1504      assume_role_policy = <<EOF
  1505  {
  1506    "Version": "2012-10-17",
  1507    "Statement": [
  1508      {
  1509        "Action": "sts:AssumeRole",
  1510        "Principal": {
  1511          "Service": "lambda.amazonaws.com"
  1512        },
  1513        "Effect": "Allow",
  1514        "Sid": ""
  1515      }
  1516    ]
  1517  }
  1518  EOF
  1519  }
  1520  resource "aws_lambda_function" "lambda_function_s3" {
  1521      s3_bucket = "${aws_s3_bucket_object.o.bucket}"
  1522      s3_key = "${aws_s3_bucket_object.o.key}"
  1523      function_name = "tf_acc_lambda_name_s3_unversioned"
  1524      role = "${aws_iam_role.iam_for_lambda.arn}"
  1525      handler = "exports.example"
  1526      runtime = "nodejs4.3"
  1527  }`, bucketName, key, path, path, rName)
  1528  }