github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_lambda_function_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/lambda" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAWSLambdaFunction_basic(t *testing.T) { 14 var conf lambda.GetFunctionOutput 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckLambdaFunctionDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAWSLambdaConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", &conf), 25 testAccCheckAWSLambdaAttributes(&conf), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func testAccCheckLambdaFunctionDestroy(s *terraform.State) error { 33 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 34 35 for _, rs := range s.RootModule().Resources { 36 if rs.Type != "aws_lambda_function" { 37 continue 38 } 39 40 _, err := conn.GetFunction(&lambda.GetFunctionInput{ 41 FunctionName: aws.String(rs.Primary.ID), 42 }) 43 44 if err == nil { 45 return fmt.Errorf("Lambda Function still exists") 46 } 47 48 } 49 50 return nil 51 52 } 53 54 func testAccCheckAwsLambdaFunctionExists(n string, function *lambda.GetFunctionOutput) resource.TestCheckFunc { 55 // Wait for IAM role 56 return func(s *terraform.State) error { 57 rs, ok := s.RootModule().Resources[n] 58 if !ok { 59 return fmt.Errorf("Lambda function not found: %s", n) 60 } 61 62 if rs.Primary.ID == "" { 63 return fmt.Errorf("Lambda function ID not set") 64 } 65 66 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 67 68 params := &lambda.GetFunctionInput{ 69 FunctionName: aws.String("example_lambda_name"), 70 } 71 72 getFunction, err := conn.GetFunction(params) 73 if err != nil { 74 return err 75 } 76 77 *function = *getFunction 78 79 return nil 80 } 81 } 82 83 func testAccCheckAWSLambdaAttributes(function *lambda.GetFunctionOutput) resource.TestCheckFunc { 84 return func(s *terraform.State) error { 85 c := function.Configuration 86 const expectedName = "example_lambda_name" 87 if *c.FunctionName != expectedName { 88 return fmt.Errorf("Expected function name %s, got %s", expectedName, *c.FunctionName) 89 } 90 91 if *c.FunctionArn == "" { 92 return fmt.Errorf("Could not read Lambda Function's ARN") 93 } 94 95 return nil 96 } 97 } 98 99 const testAccAWSLambdaConfig = ` 100 resource "aws_iam_role" "iam_for_lambda" { 101 name = "iam_for_lambda" 102 assume_role_policy = <<EOF 103 { 104 "Version": "2012-10-17", 105 "Statement": [ 106 { 107 "Action": "sts:AssumeRole", 108 "Principal": { 109 "Service": "lambda.amazonaws.com" 110 }, 111 "Effect": "Allow", 112 "Sid": "" 113 } 114 ] 115 } 116 EOF 117 } 118 119 resource "aws_lambda_function" "lambda_function_test" { 120 filename = "test-fixtures/lambdatest.zip" 121 function_name = "example_lambda_name" 122 role = "${aws_iam_role.iam_for_lambda.arn}" 123 handler = "exports.example" 124 } 125 `