github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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_VPC(t *testing.T) { 236 var conf lambda.GetFunctionOutput 237 238 rSt := acctest.RandString(5) 239 rName := fmt.Sprintf("tf_test_%s", rSt) 240 241 resource.Test(t, resource.TestCase{ 242 PreCheck: func() { testAccPreCheck(t) }, 243 Providers: testAccProviders, 244 CheckDestroy: testAccCheckLambdaFunctionDestroy, 245 Steps: []resource.TestStep{ 246 { 247 Config: testAccAWSLambdaConfigWithVPC(rName, rSt), 248 Check: resource.ComposeTestCheckFunc( 249 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf), 250 testAccCheckAwsLambdaFunctionName(&conf, rName), 251 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName), 252 testAccCheckAWSLambdaFunctionVersion(&conf, "$LATEST"), 253 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.#", "1"), 254 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.0.subnet_ids.#", "1"), 255 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.0.security_group_ids.#", "1"), 256 resource.TestMatchResourceAttr("aws_lambda_function.lambda_function_test", "vpc_config.0.vpc_id", regexp.MustCompile("^vpc-")), 257 ), 258 }, 259 }, 260 }) 261 } 262 263 func TestAccAWSLambdaFunction_s3(t *testing.T) { 264 var conf lambda.GetFunctionOutput 265 rSt := acctest.RandString(5) 266 rName := fmt.Sprintf("tf_test_%s", rSt) 267 268 resource.Test(t, resource.TestCase{ 269 PreCheck: func() { testAccPreCheck(t) }, 270 Providers: testAccProviders, 271 CheckDestroy: testAccCheckLambdaFunctionDestroy, 272 Steps: []resource.TestStep{ 273 { 274 Config: testAccAWSLambdaConfigS3(rName, rSt), 275 Check: resource.ComposeTestCheckFunc( 276 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3test", rName, &conf), 277 testAccCheckAwsLambdaFunctionName(&conf, rName), 278 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName), 279 testAccCheckAWSLambdaFunctionVersion(&conf, "$LATEST"), 280 ), 281 }, 282 }, 283 }) 284 } 285 286 func TestAccAWSLambdaFunction_localUpdate(t *testing.T) { 287 var conf lambda.GetFunctionOutput 288 289 path, zipFile, err := createTempFile("lambda_localUpdate") 290 if err != nil { 291 t.Fatal(err) 292 } 293 defer os.Remove(path) 294 295 rInt := acctest.RandInt() 296 rName := fmt.Sprintf("tf_acc_lambda_local_%d", rInt) 297 298 resource.Test(t, resource.TestCase{ 299 PreCheck: func() { testAccPreCheck(t) }, 300 Providers: testAccProviders, 301 CheckDestroy: testAccCheckLambdaFunctionDestroy, 302 Steps: []resource.TestStep{ 303 { 304 PreConfig: func() { 305 testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile) 306 }, 307 Config: genAWSLambdaFunctionConfig_local(path, rInt, rName), 308 Check: resource.ComposeTestCheckFunc( 309 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", rName, &conf), 310 testAccCheckAwsLambdaFunctionName(&conf, rName), 311 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName), 312 testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="), 313 ), 314 }, 315 { 316 PreConfig: func() { 317 testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile) 318 }, 319 Config: genAWSLambdaFunctionConfig_local(path, rInt, rName), 320 Check: resource.ComposeTestCheckFunc( 321 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", rName, &conf), 322 testAccCheckAwsLambdaFunctionName(&conf, rName), 323 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName), 324 testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="), 325 ), 326 }, 327 }, 328 }) 329 } 330 331 func TestAccAWSLambdaFunction_localUpdate_nameOnly(t *testing.T) { 332 var conf lambda.GetFunctionOutput 333 334 rName := fmt.Sprintf("tf_test_iam_%d", acctest.RandInt()) 335 336 path, zipFile, err := createTempFile("lambda_localUpdate") 337 if err != nil { 338 t.Fatal(err) 339 } 340 defer os.Remove(path) 341 342 updatedPath, updatedZipFile, err := createTempFile("lambda_localUpdate_name_change") 343 if err != nil { 344 t.Fatal(err) 345 } 346 defer os.Remove(updatedPath) 347 348 resource.Test(t, resource.TestCase{ 349 PreCheck: func() { testAccPreCheck(t) }, 350 Providers: testAccProviders, 351 CheckDestroy: testAccCheckLambdaFunctionDestroy, 352 Steps: []resource.TestStep{ 353 { 354 PreConfig: func() { 355 testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile) 356 }, 357 Config: genAWSLambdaFunctionConfig_local_name_only(path, rName), 358 Check: resource.ComposeTestCheckFunc( 359 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", rName, &conf), 360 testAccCheckAwsLambdaFunctionName(&conf, rName), 361 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName), 362 testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="), 363 ), 364 }, 365 { 366 PreConfig: func() { 367 testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, updatedZipFile) 368 }, 369 Config: genAWSLambdaFunctionConfig_local_name_only(updatedPath, rName), 370 Check: resource.ComposeTestCheckFunc( 371 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", rName, &conf), 372 testAccCheckAwsLambdaFunctionName(&conf, rName), 373 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName), 374 testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="), 375 ), 376 }, 377 }, 378 }) 379 } 380 381 func TestAccAWSLambdaFunction_s3Update_basic(t *testing.T) { 382 var conf lambda.GetFunctionOutput 383 384 path, zipFile, err := createTempFile("lambda_s3Update") 385 if err != nil { 386 t.Fatal(err) 387 } 388 defer os.Remove(path) 389 390 bucketName := fmt.Sprintf("tf-acc-lambda-s3-deployments-%d", randomInteger) 391 key := "lambda-func.zip" 392 393 rInt := acctest.RandInt() 394 395 rName := fmt.Sprintf("tf_acc_lambda_%d", rInt) 396 397 resource.Test(t, resource.TestCase{ 398 PreCheck: func() { testAccPreCheck(t) }, 399 Providers: testAccProviders, 400 CheckDestroy: testAccCheckLambdaFunctionDestroy, 401 Steps: []resource.TestStep{ 402 { 403 PreConfig: func() { 404 // Upload 1st version 405 testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile) 406 }, 407 Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path, rInt, rName), 408 Check: resource.ComposeTestCheckFunc( 409 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", rName, &conf), 410 testAccCheckAwsLambdaFunctionName(&conf, rName), 411 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName), 412 testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="), 413 ), 414 }, 415 { 416 ExpectNonEmptyPlan: true, 417 PreConfig: func() { 418 // Upload 2nd version 419 testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile) 420 }, 421 Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path, rInt, rName), 422 }, 423 // Extra step because of missing ComputedWhen 424 // See https://github.com/hashicorp/terraform/pull/4846 & https://github.com/hashicorp/terraform/pull/5330 425 { 426 Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path, rInt, rName), 427 Check: resource.ComposeTestCheckFunc( 428 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", rName, &conf), 429 testAccCheckAwsLambdaFunctionName(&conf, rName), 430 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName), 431 testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="), 432 ), 433 }, 434 }, 435 }) 436 } 437 438 func TestAccAWSLambdaFunction_s3Update_unversioned(t *testing.T) { 439 var conf lambda.GetFunctionOutput 440 441 rName := fmt.Sprintf("tf_iam_lambda_%d", acctest.RandInt()) 442 443 path, zipFile, err := createTempFile("lambda_s3Update") 444 if err != nil { 445 t.Fatal(err) 446 } 447 defer os.Remove(path) 448 449 bucketName := fmt.Sprintf("tf-acc-lambda-s3-deployments-%d", randomInteger) 450 key := "lambda-func.zip" 451 key2 := "lambda-func-modified.zip" 452 453 resource.Test(t, resource.TestCase{ 454 PreCheck: func() { testAccPreCheck(t) }, 455 Providers: testAccProviders, 456 CheckDestroy: testAccCheckLambdaFunctionDestroy, 457 Steps: []resource.TestStep{ 458 { 459 PreConfig: func() { 460 // Upload 1st version 461 testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile) 462 }, 463 Config: testAccAWSLambdaFunctionConfig_s3_unversioned_tpl(rName, bucketName, key, path), 464 Check: resource.ComposeTestCheckFunc( 465 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3_unversioned", &conf), 466 testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3_unversioned"), 467 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_s3_unversioned"), 468 testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="), 469 ), 470 }, 471 { 472 PreConfig: func() { 473 // Upload 2nd version 474 testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile) 475 }, 476 Config: testAccAWSLambdaFunctionConfig_s3_unversioned_tpl(rName, bucketName, key2, path), 477 Check: resource.ComposeTestCheckFunc( 478 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3_unversioned", &conf), 479 testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3_unversioned"), 480 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_s3_unversioned"), 481 testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="), 482 ), 483 }, 484 }, 485 }) 486 } 487 488 func TestAccAWSLambdaFunction_runtimeValidation_noRuntime(t *testing.T) { 489 rSt := acctest.RandString(5) 490 rName := fmt.Sprintf("tf_test_%s", rSt) 491 492 resource.Test(t, resource.TestCase{ 493 PreCheck: func() { testAccPreCheck(t) }, 494 Providers: testAccProviders, 495 CheckDestroy: testAccCheckLambdaFunctionDestroy, 496 Steps: []resource.TestStep{ 497 { 498 Config: testAccAWSLambdaConfigNoRuntime(rName, rSt), 499 ExpectError: regexp.MustCompile(`\\"runtime\\": required field is not set`), 500 }, 501 }, 502 }) 503 } 504 505 func TestAccAWSLambdaFunction_runtimeValidation_nodeJs(t *testing.T) { 506 rSt := acctest.RandString(5) 507 rName := fmt.Sprintf("tf_test_%s", rSt) 508 509 resource.Test(t, resource.TestCase{ 510 PreCheck: func() { testAccPreCheck(t) }, 511 Providers: testAccProviders, 512 CheckDestroy: testAccCheckLambdaFunctionDestroy, 513 Steps: []resource.TestStep{ 514 { 515 Config: testAccAWSLambdaConfigNodeJsRuntime(rName, rSt), 516 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)), 517 }, 518 }, 519 }) 520 } 521 522 func TestAccAWSLambdaFunction_runtimeValidation_nodeJs43(t *testing.T) { 523 var conf lambda.GetFunctionOutput 524 rSt := acctest.RandString(5) 525 rName := fmt.Sprintf("tf_test_%s", rSt) 526 527 resource.Test(t, resource.TestCase{ 528 PreCheck: func() { testAccPreCheck(t) }, 529 Providers: testAccProviders, 530 CheckDestroy: testAccCheckLambdaFunctionDestroy, 531 Steps: []resource.TestStep{ 532 { 533 Config: testAccAWSLambdaConfigNodeJs43Runtime(rName, rSt), 534 Check: resource.ComposeTestCheckFunc( 535 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf), 536 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "runtime", lambda.RuntimeNodejs43), 537 ), 538 }, 539 }, 540 }) 541 } 542 543 func TestAccAWSLambdaFunction_runtimeValidation_python27(t *testing.T) { 544 var conf lambda.GetFunctionOutput 545 rSt := acctest.RandString(5) 546 rName := fmt.Sprintf("tf_test_%s", rSt) 547 548 resource.Test(t, resource.TestCase{ 549 PreCheck: func() { testAccPreCheck(t) }, 550 Providers: testAccProviders, 551 CheckDestroy: testAccCheckLambdaFunctionDestroy, 552 Steps: []resource.TestStep{ 553 { 554 Config: testAccAWSLambdaConfigPython27Runtime(rName, rSt), 555 Check: resource.ComposeTestCheckFunc( 556 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf), 557 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "runtime", lambda.RuntimePython27), 558 ), 559 }, 560 }, 561 }) 562 } 563 564 func TestAccAWSLambdaFunction_runtimeValidation_java8(t *testing.T) { 565 var conf lambda.GetFunctionOutput 566 rSt := acctest.RandString(5) 567 rName := fmt.Sprintf("tf_test_%s", rSt) 568 569 resource.Test(t, resource.TestCase{ 570 PreCheck: func() { testAccPreCheck(t) }, 571 Providers: testAccProviders, 572 CheckDestroy: testAccCheckLambdaFunctionDestroy, 573 Steps: []resource.TestStep{ 574 { 575 Config: testAccAWSLambdaConfigJava8Runtime(rName, rSt), 576 Check: resource.ComposeTestCheckFunc( 577 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf), 578 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "runtime", lambda.RuntimeJava8), 579 ), 580 }, 581 }, 582 }) 583 } 584 585 func TestAccAWSLambdaFunction_tags(t *testing.T) { 586 var conf lambda.GetFunctionOutput 587 588 rSt := acctest.RandString(5) 589 rName := fmt.Sprintf("tf_test_%s", rSt) 590 591 resource.Test(t, resource.TestCase{ 592 PreCheck: func() { testAccPreCheck(t) }, 593 Providers: testAccProviders, 594 CheckDestroy: testAccCheckLambdaFunctionDestroy, 595 Steps: []resource.TestStep{ 596 { 597 Config: testAccAWSLambdaConfigBasic(rName, rSt), 598 Check: resource.ComposeTestCheckFunc( 599 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf), 600 testAccCheckAwsLambdaFunctionName(&conf, rName), 601 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName), 602 resource.TestCheckNoResourceAttr("aws_lambda_function.lambda_function_test", "tags"), 603 ), 604 }, 605 { 606 Config: testAccAWSLambdaConfigTags(rName, rSt), 607 Check: resource.ComposeTestCheckFunc( 608 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf), 609 testAccCheckAwsLambdaFunctionName(&conf, rName), 610 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName), 611 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.%", "2"), 612 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.Key1", "Value One"), 613 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.Description", "Very interesting"), 614 ), 615 }, 616 { 617 Config: testAccAWSLambdaConfigTagsModified(rName, rSt), 618 Check: resource.ComposeTestCheckFunc( 619 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf), 620 testAccCheckAwsLambdaFunctionName(&conf, rName), 621 testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":"+rName), 622 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.%", "3"), 623 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.Key1", "Value One Changed"), 624 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.Key2", "Value Two"), 625 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "tags.Key3", "Value Three"), 626 ), 627 }, 628 }, 629 }) 630 } 631 632 func TestAccAWSLambdaFunction_runtimeValidation_python36(t *testing.T) { 633 var conf lambda.GetFunctionOutput 634 rSt := acctest.RandString(5) 635 rName := fmt.Sprintf("tf_test_%s", rSt) 636 637 resource.Test(t, resource.TestCase{ 638 PreCheck: func() { testAccPreCheck(t) }, 639 Providers: testAccProviders, 640 CheckDestroy: testAccCheckLambdaFunctionDestroy, 641 Steps: []resource.TestStep{ 642 { 643 Config: testAccAWSLambdaConfigPython36Runtime(rName, rSt), 644 Check: resource.ComposeTestCheckFunc( 645 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", rName, &conf), 646 resource.TestCheckResourceAttr("aws_lambda_function.lambda_function_test", "runtime", lambda.RuntimePython36), 647 ), 648 }, 649 }, 650 }) 651 } 652 653 func testAccCheckLambdaFunctionDestroy(s *terraform.State) error { 654 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 655 656 for _, rs := range s.RootModule().Resources { 657 if rs.Type != "aws_lambda_function" { 658 continue 659 } 660 661 _, err := conn.GetFunction(&lambda.GetFunctionInput{ 662 FunctionName: aws.String(rs.Primary.ID), 663 }) 664 665 if err == nil { 666 return fmt.Errorf("Lambda Function still exists") 667 } 668 669 } 670 671 return nil 672 673 } 674 675 func testAccCheckAwsLambdaFunctionExists(res, funcName string, function *lambda.GetFunctionOutput) resource.TestCheckFunc { 676 // Wait for IAM role 677 return func(s *terraform.State) error { 678 rs, ok := s.RootModule().Resources[res] 679 if !ok { 680 return fmt.Errorf("Lambda function not found: %s", res) 681 } 682 683 if rs.Primary.ID == "" { 684 return fmt.Errorf("Lambda function ID not set") 685 } 686 687 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 688 689 params := &lambda.GetFunctionInput{ 690 FunctionName: aws.String(funcName), 691 } 692 693 getFunction, err := conn.GetFunction(params) 694 if err != nil { 695 return err 696 } 697 698 *function = *getFunction 699 700 return nil 701 } 702 } 703 704 func testAccCheckAwsLambdaFunctionName(function *lambda.GetFunctionOutput, expectedName string) resource.TestCheckFunc { 705 return func(s *terraform.State) error { 706 c := function.Configuration 707 if *c.FunctionName != expectedName { 708 return fmt.Errorf("Expected function name %s, got %s", expectedName, *c.FunctionName) 709 } 710 711 return nil 712 } 713 } 714 715 func testAccCheckAWSLambdaFunctionVersion(function *lambda.GetFunctionOutput, expectedVersion string) resource.TestCheckFunc { 716 return func(s *terraform.State) error { 717 c := function.Configuration 718 if *c.Version != expectedVersion { 719 return fmt.Errorf("Expected version %s, got %s", expectedVersion, *c.Version) 720 } 721 return nil 722 } 723 } 724 725 func testAccCheckAwsLambdaFunctionArnHasSuffix(function *lambda.GetFunctionOutput, arnSuffix string) resource.TestCheckFunc { 726 return func(s *terraform.State) error { 727 c := function.Configuration 728 if !strings.HasSuffix(*c.FunctionArn, arnSuffix) { 729 return fmt.Errorf("Expected function ARN %s to have suffix %s", *c.FunctionArn, arnSuffix) 730 } 731 732 return nil 733 } 734 } 735 736 func testAccCheckAwsLambdaSourceCodeHash(function *lambda.GetFunctionOutput, expectedHash string) resource.TestCheckFunc { 737 return func(s *terraform.State) error { 738 c := function.Configuration 739 if *c.CodeSha256 != expectedHash { 740 return fmt.Errorf("Expected code hash %s, got %s", expectedHash, *c.CodeSha256) 741 } 742 743 return nil 744 } 745 } 746 747 func testAccCreateZipFromFiles(files map[string]string, zipFile *os.File) error { 748 zipFile.Truncate(0) 749 zipFile.Seek(0, 0) 750 751 w := zip.NewWriter(zipFile) 752 753 for source, destination := range files { 754 f, err := w.Create(destination) 755 if err != nil { 756 return err 757 } 758 759 fileContent, err := ioutil.ReadFile(source) 760 if err != nil { 761 return err 762 } 763 764 _, err = f.Write(fileContent) 765 if err != nil { 766 return err 767 } 768 } 769 770 err := w.Close() 771 if err != nil { 772 return err 773 } 774 775 return w.Flush() 776 } 777 778 func createTempFile(prefix string) (string, *os.File, error) { 779 f, err := ioutil.TempFile(os.TempDir(), prefix) 780 if err != nil { 781 return "", nil, err 782 } 783 784 pathToFile, err := filepath.Abs(f.Name()) 785 if err != nil { 786 return "", nil, err 787 } 788 return pathToFile, f, nil 789 } 790 791 func baseAccAWSLambdaConfig(rst string) string { 792 return fmt.Sprintf(` 793 resource "aws_iam_role_policy" "iam_policy_for_lambda" { 794 name = "iam_policy_for_lambda_%s" 795 role = "${aws_iam_role.iam_for_lambda.id}" 796 policy = <<EOF 797 { 798 "Version": "2012-10-17", 799 "Statement": [ 800 { 801 "Effect": "Allow", 802 "Action": [ 803 "logs:CreateLogGroup", 804 "logs:CreateLogStream", 805 "logs:PutLogEvents" 806 ], 807 "Resource": "arn:aws:logs:*:*:*" 808 }, 809 { 810 "Effect": "Allow", 811 "Action": [ 812 "ec2:CreateNetworkInterface", 813 "ec2:DescribeNetworkInterfaces", 814 "ec2:DeleteNetworkInterface" 815 ], 816 "Resource": [ 817 "*" 818 ] 819 }, 820 { 821 "Effect": "Allow", 822 "Action": [ 823 "SNS:Publish" 824 ], 825 "Resource": [ 826 "*" 827 ] 828 } 829 ] 830 } 831 EOF 832 } 833 834 resource "aws_iam_role" "iam_for_lambda" { 835 name = "iam_for_lambda_%s" 836 assume_role_policy = <<EOF 837 { 838 "Version": "2012-10-17", 839 "Statement": [ 840 { 841 "Action": "sts:AssumeRole", 842 "Principal": { 843 "Service": "lambda.amazonaws.com" 844 }, 845 "Effect": "Allow", 846 "Sid": "" 847 } 848 ] 849 } 850 EOF 851 } 852 853 resource "aws_vpc" "vpc_for_lambda" { 854 cidr_block = "10.0.0.0/16" 855 } 856 857 resource "aws_subnet" "subnet_for_lambda" { 858 vpc_id = "${aws_vpc.vpc_for_lambda.id}" 859 cidr_block = "10.0.1.0/24" 860 861 tags { 862 Name = "lambda" 863 } 864 } 865 866 resource "aws_security_group" "sg_for_lambda" { 867 name = "sg_for_lambda_%s" 868 description = "Allow all inbound traffic for lambda test" 869 vpc_id = "${aws_vpc.vpc_for_lambda.id}" 870 871 ingress { 872 from_port = 0 873 to_port = 0 874 protocol = "-1" 875 cidr_blocks = ["0.0.0.0/0"] 876 } 877 878 egress { 879 from_port = 0 880 to_port = 0 881 protocol = "-1" 882 cidr_blocks = ["0.0.0.0/0"] 883 } 884 }`, rst, rst, rst) 885 } 886 887 func testAccAWSLambdaConfigBasic(rName, rSt string) string { 888 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 889 resource "aws_lambda_function" "lambda_function_test" { 890 filename = "test-fixtures/lambdatest.zip" 891 function_name = "%s" 892 role = "${aws_iam_role.iam_for_lambda.arn}" 893 handler = "exports.example" 894 runtime = "nodejs4.3" 895 } 896 `, rName) 897 } 898 899 func testAccAWSLambdaConfigBasicUpdateRuntime(rName, rSt string) string { 900 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 901 resource "aws_lambda_function" "lambda_function_test" { 902 filename = "test-fixtures/lambdatest.zip" 903 function_name = "%s" 904 role = "${aws_iam_role.iam_for_lambda.arn}" 905 handler = "exports.example" 906 runtime = "nodejs4.3-edge" 907 } 908 `, rName) 909 } 910 911 func testAccAWSLambdaConfigWithoutFilenameAndS3Attributes(rName, rSt string) string { 912 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 913 resource "aws_lambda_function" "lambda_function_test" { 914 function_name = "%s" 915 role = "${aws_iam_role.iam_for_lambda.arn}" 916 handler = "exports.example" 917 runtime = "nodejs4.3" 918 } 919 `, rName) 920 } 921 922 func testAccAWSLambdaConfigEnvVariables(rName, rSt string) string { 923 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 924 resource "aws_lambda_function" "lambda_function_test" { 925 filename = "test-fixtures/lambdatest.zip" 926 function_name = "%s" 927 role = "${aws_iam_role.iam_for_lambda.arn}" 928 handler = "exports.example" 929 runtime = "nodejs4.3" 930 environment { 931 variables = { 932 foo = "bar" 933 } 934 } 935 } 936 `, rName) 937 } 938 939 func testAccAWSLambdaConfigEnvVariablesModified(rName, rSt string) string { 940 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 941 resource "aws_lambda_function" "lambda_function_test" { 942 filename = "test-fixtures/lambdatest.zip" 943 function_name = "%s" 944 role = "${aws_iam_role.iam_for_lambda.arn}" 945 handler = "exports.example" 946 runtime = "nodejs4.3" 947 environment { 948 variables = { 949 foo = "baz" 950 foo1 = "bar1" 951 } 952 } 953 } 954 `, rName) 955 } 956 957 func testAccAWSLambdaConfigEnvVariablesModifiedWithoutEnvironment(rName, rSt string) string { 958 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 959 resource "aws_lambda_function" "lambda_function_test" { 960 filename = "test-fixtures/lambdatest.zip" 961 function_name = "%s" 962 role = "${aws_iam_role.iam_for_lambda.arn}" 963 handler = "exports.example" 964 runtime = "nodejs4.3" 965 } 966 `, rName) 967 } 968 969 func testAccAWSLambdaConfigEncryptedEnvVariables(rName, rSt string) string { 970 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 971 resource "aws_kms_key" "foo" { 972 description = "Terraform acc test %s" 973 policy = <<POLICY 974 { 975 "Version": "2012-10-17", 976 "Id": "kms-tf-1", 977 "Statement": [ 978 { 979 "Sid": "Enable IAM User Permissions", 980 "Effect": "Allow", 981 "Principal": { 982 "AWS": "*" 983 }, 984 "Action": "kms:*", 985 "Resource": "*" 986 } 987 ] 988 } 989 POLICY 990 } 991 992 resource "aws_lambda_function" "lambda_function_test" { 993 filename = "test-fixtures/lambdatest.zip" 994 function_name = "%s" 995 role = "${aws_iam_role.iam_for_lambda.arn}" 996 handler = "exports.example" 997 kms_key_arn = "${aws_kms_key.foo.arn}" 998 runtime = "nodejs4.3" 999 environment { 1000 variables = { 1001 foo = "bar" 1002 } 1003 } 1004 } 1005 `, rName, rName) 1006 } 1007 1008 func testAccAWSLambdaConfigEncryptedEnvVariablesModified(rName, rSt string) string { 1009 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1010 resource "aws_lambda_function" "lambda_function_test" { 1011 filename = "test-fixtures/lambdatest.zip" 1012 function_name = "%s" 1013 role = "${aws_iam_role.iam_for_lambda.arn}" 1014 handler = "exports.example" 1015 runtime = "nodejs4.3" 1016 environment { 1017 variables = { 1018 foo = "bar" 1019 } 1020 } 1021 } 1022 `, rName) 1023 } 1024 1025 func testAccAWSLambdaConfigVersioned(rName, rSt string) string { 1026 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1027 resource "aws_lambda_function" "lambda_function_test" { 1028 filename = "test-fixtures/lambdatest.zip" 1029 function_name = "%s" 1030 publish = true 1031 role = "${aws_iam_role.iam_for_lambda.arn}" 1032 handler = "exports.example" 1033 runtime = "nodejs4.3" 1034 } 1035 `, rName) 1036 } 1037 1038 func testAccAWSLambdaConfigWithDeadLetterConfig(rName, rSt string) string { 1039 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1040 resource "aws_lambda_function" "lambda_function_test" { 1041 filename = "test-fixtures/lambdatest.zip" 1042 function_name = "%s" 1043 role = "${aws_iam_role.iam_for_lambda.arn}" 1044 handler = "exports.example" 1045 runtime = "nodejs4.3" 1046 1047 dead_letter_config { 1048 target_arn = "${aws_sns_topic.lambda_function_test.arn}" 1049 } 1050 } 1051 1052 resource "aws_sns_topic" "lambda_function_test" { 1053 name = "%s" 1054 } 1055 1056 `, rName, rName) 1057 } 1058 1059 func testAccAWSLambdaConfigWithVPC(rName, rSt string) string { 1060 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1061 resource "aws_lambda_function" "lambda_function_test" { 1062 filename = "test-fixtures/lambdatest.zip" 1063 function_name = "%s" 1064 role = "${aws_iam_role.iam_for_lambda.arn}" 1065 handler = "exports.example" 1066 runtime = "nodejs4.3" 1067 1068 vpc_config = { 1069 subnet_ids = ["${aws_subnet.subnet_for_lambda.id}"] 1070 security_group_ids = ["${aws_security_group.sg_for_lambda.id}"] 1071 } 1072 }`, rName) 1073 } 1074 1075 func testAccAWSLambdaConfigS3(rName, rSt string) string { 1076 return fmt.Sprintf(` 1077 resource "aws_s3_bucket" "lambda_bucket" { 1078 bucket = "tf-test-bucket-%d" 1079 } 1080 1081 resource "aws_s3_bucket_object" "lambda_code" { 1082 bucket = "${aws_s3_bucket.lambda_bucket.id}" 1083 key = "lambdatest.zip" 1084 source = "test-fixtures/lambdatest.zip" 1085 } 1086 1087 resource "aws_iam_role" "iam_for_lambda" { 1088 name = "iam_for_lambda_%s" 1089 assume_role_policy = <<EOF 1090 { 1091 "Version": "2012-10-17", 1092 "Statement": [ 1093 { 1094 "Action": "sts:AssumeRole", 1095 "Principal": { 1096 "Service": "lambda.amazonaws.com" 1097 }, 1098 "Effect": "Allow", 1099 "Sid": "" 1100 } 1101 ] 1102 } 1103 EOF 1104 } 1105 1106 resource "aws_lambda_function" "lambda_function_s3test" { 1107 s3_bucket = "${aws_s3_bucket.lambda_bucket.id}" 1108 s3_key = "${aws_s3_bucket_object.lambda_code.id}" 1109 function_name = "%s" 1110 role = "${aws_iam_role.iam_for_lambda.arn}" 1111 handler = "exports.example" 1112 runtime = "nodejs4.3" 1113 } 1114 `, acctest.RandInt(), rSt, rName) 1115 } 1116 1117 func testAccAWSLambdaConfigNoRuntime(rName, rSt string) string { 1118 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1119 resource "aws_lambda_function" "lambda_function_test" { 1120 filename = "test-fixtures/lambdatest.zip" 1121 function_name = "%s" 1122 role = "${aws_iam_role.iam_for_lambda.arn}" 1123 handler = "exports.example" 1124 runtime = "nodejs4.3" 1125 } 1126 `, rName) 1127 } 1128 1129 func testAccAWSLambdaConfigNodeJsRuntime(rName, rSt string) string { 1130 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1131 resource "aws_lambda_function" "lambda_function_test" { 1132 filename = "test-fixtures/lambdatest.zip" 1133 function_name = "%s" 1134 role = "${aws_iam_role.iam_for_lambda.arn}" 1135 handler = "exports.example" 1136 runtime = "nodejs4.3" 1137 } 1138 `, rName) 1139 } 1140 1141 func testAccAWSLambdaConfigNodeJs43Runtime(rName, rSt string) string { 1142 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1143 resource "aws_lambda_function" "lambda_function_test" { 1144 filename = "test-fixtures/lambdatest.zip" 1145 function_name = "%s" 1146 role = "${aws_iam_role.iam_for_lambda.arn}" 1147 handler = "exports.example" 1148 runtime = "nodejs4.3" 1149 } 1150 `, rName) 1151 } 1152 1153 func testAccAWSLambdaConfigPython27Runtime(rName, rSt string) string { 1154 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1155 resource "aws_lambda_function" "lambda_function_test" { 1156 filename = "test-fixtures/lambdatest.zip" 1157 function_name = "%s" 1158 role = "${aws_iam_role.iam_for_lambda.arn}" 1159 handler = "exports.example" 1160 runtime = "python2.7" 1161 } 1162 `, rName) 1163 } 1164 1165 func testAccAWSLambdaConfigJava8Runtime(rName, rSt string) string { 1166 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1167 resource "aws_lambda_function" "lambda_function_test" { 1168 filename = "test-fixtures/lambdatest.zip" 1169 function_name = "%s" 1170 role = "${aws_iam_role.iam_for_lambda.arn}" 1171 handler = "exports.example" 1172 runtime = "java8" 1173 } 1174 `, rName) 1175 } 1176 1177 func testAccAWSLambdaConfigTags(rName, rSt string) string { 1178 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1179 resource "aws_lambda_function" "lambda_function_test" { 1180 filename = "test-fixtures/lambdatest.zip" 1181 function_name = "%s" 1182 role = "${aws_iam_role.iam_for_lambda.arn}" 1183 handler = "exports.example" 1184 runtime = "nodejs4.3" 1185 tags { 1186 Key1 = "Value One" 1187 Description = "Very interesting" 1188 } 1189 } 1190 `, rName) 1191 } 1192 1193 func testAccAWSLambdaConfigTagsModified(rName, rSt string) string { 1194 return fmt.Sprintf(baseAccAWSLambdaConfig(rSt)+` 1195 resource "aws_lambda_function" "lambda_function_test" { 1196 filename = "test-fixtures/lambdatest.zip" 1197 function_name = "%s" 1198 role = "${aws_iam_role.iam_for_lambda.arn}" 1199 handler = "exports.example" 1200 runtime = "nodejs4.3" 1201 tags { 1202 Key1 = "Value One Changed" 1203 Key2 = "Value Two" 1204 Key3 = "Value Three" 1205 } 1206 } 1207 `, rName) 1208 } 1209 1210 func testAccAWSLambdaConfigPython36Runtime(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 = "python3.6" 1218 } 1219 `, rName) 1220 } 1221 1222 const testAccAWSLambdaFunctionConfig_local_tpl = ` 1223 resource "aws_iam_role" "iam_for_lambda" { 1224 name = "iam_for_lambda_%d" 1225 assume_role_policy = <<EOF 1226 { 1227 "Version": "2012-10-17", 1228 "Statement": [ 1229 { 1230 "Action": "sts:AssumeRole", 1231 "Principal": { 1232 "Service": "lambda.amazonaws.com" 1233 }, 1234 "Effect": "Allow", 1235 "Sid": "" 1236 } 1237 ] 1238 } 1239 EOF 1240 } 1241 resource "aws_lambda_function" "lambda_function_local" { 1242 filename = "%s" 1243 source_code_hash = "${base64sha256(file("%s"))}" 1244 function_name = "%s" 1245 role = "${aws_iam_role.iam_for_lambda.arn}" 1246 handler = "exports.example" 1247 runtime = "nodejs4.3" 1248 } 1249 ` 1250 1251 func genAWSLambdaFunctionConfig_local(filePath string, rInt int, rName string) string { 1252 return fmt.Sprintf(testAccAWSLambdaFunctionConfig_local_tpl, rInt, 1253 filePath, filePath, rName) 1254 } 1255 1256 func genAWSLambdaFunctionConfig_local_name_only(filePath, rName string) string { 1257 return testAccAWSLambdaFunctionConfig_local_name_only_tpl(filePath, rName) 1258 } 1259 1260 func testAccAWSLambdaFunctionConfig_local_name_only_tpl(filePath, rName string) string { 1261 return fmt.Sprintf(` 1262 resource "aws_iam_role" "iam_for_lambda" { 1263 name = "%s" 1264 assume_role_policy = <<EOF 1265 { 1266 "Version": "2012-10-17", 1267 "Statement": [ 1268 { 1269 "Action": "sts:AssumeRole", 1270 "Principal": { 1271 "Service": "lambda.amazonaws.com" 1272 }, 1273 "Effect": "Allow", 1274 "Sid": "" 1275 } 1276 ] 1277 } 1278 EOF 1279 } 1280 resource "aws_lambda_function" "lambda_function_local" { 1281 filename = "%s" 1282 function_name = "%s" 1283 role = "${aws_iam_role.iam_for_lambda.arn}" 1284 handler = "exports.example" 1285 runtime = "nodejs4.3" 1286 }`, rName, filePath, rName) 1287 } 1288 1289 const testAccAWSLambdaFunctionConfig_s3_tpl = ` 1290 resource "aws_s3_bucket" "artifacts" { 1291 bucket = "%s" 1292 acl = "private" 1293 force_destroy = true 1294 versioning { 1295 enabled = true 1296 } 1297 } 1298 resource "aws_s3_bucket_object" "o" { 1299 bucket = "${aws_s3_bucket.artifacts.bucket}" 1300 key = "%s" 1301 source = "%s" 1302 etag = "${md5(file("%s"))}" 1303 } 1304 resource "aws_iam_role" "iam_for_lambda" { 1305 name = "iam_for_lambda_%d" 1306 assume_role_policy = <<EOF 1307 { 1308 "Version": "2012-10-17", 1309 "Statement": [ 1310 { 1311 "Action": "sts:AssumeRole", 1312 "Principal": { 1313 "Service": "lambda.amazonaws.com" 1314 }, 1315 "Effect": "Allow", 1316 "Sid": "" 1317 } 1318 ] 1319 } 1320 EOF 1321 } 1322 resource "aws_lambda_function" "lambda_function_s3" { 1323 s3_bucket = "${aws_s3_bucket_object.o.bucket}" 1324 s3_key = "${aws_s3_bucket_object.o.key}" 1325 s3_object_version = "${aws_s3_bucket_object.o.version_id}" 1326 function_name = "%s" 1327 role = "${aws_iam_role.iam_for_lambda.arn}" 1328 handler = "exports.example" 1329 runtime = "nodejs4.3" 1330 } 1331 ` 1332 1333 func genAWSLambdaFunctionConfig_s3(bucket, key, path string, rInt int, rName string) string { 1334 return fmt.Sprintf(testAccAWSLambdaFunctionConfig_s3_tpl, 1335 bucket, key, path, path, rInt, rName) 1336 } 1337 1338 func testAccAWSLambdaFunctionConfig_s3_unversioned_tpl(rName, bucketName, key, path string) string { 1339 return fmt.Sprintf(` 1340 resource "aws_s3_bucket" "artifacts" { 1341 bucket = "%s" 1342 acl = "private" 1343 force_destroy = true 1344 } 1345 resource "aws_s3_bucket_object" "o" { 1346 bucket = "${aws_s3_bucket.artifacts.bucket}" 1347 key = "%s" 1348 source = "%s" 1349 etag = "${md5(file("%s"))}" 1350 } 1351 resource "aws_iam_role" "iam_for_lambda" { 1352 name = "%s" 1353 assume_role_policy = <<EOF 1354 { 1355 "Version": "2012-10-17", 1356 "Statement": [ 1357 { 1358 "Action": "sts:AssumeRole", 1359 "Principal": { 1360 "Service": "lambda.amazonaws.com" 1361 }, 1362 "Effect": "Allow", 1363 "Sid": "" 1364 } 1365 ] 1366 } 1367 EOF 1368 } 1369 resource "aws_lambda_function" "lambda_function_s3" { 1370 s3_bucket = "${aws_s3_bucket_object.o.bucket}" 1371 s3_key = "${aws_s3_bucket_object.o.key}" 1372 function_name = "tf_acc_lambda_name_s3_unversioned" 1373 role = "${aws_iam_role.iam_for_lambda.arn}" 1374 handler = "exports.example" 1375 runtime = "nodejs4.3" 1376 }`, bucketName, key, path, path, rName) 1377 }