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