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