github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_lambda_event_source_mapping_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "strconv" 7 "testing" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/service/lambda" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSLambdaEventSourceMapping_basic(t *testing.T) { 16 var conf lambda.EventSourceMappingConfiguration 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccAWSLambdaEventSourceMappingConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), 27 testAccCheckAWSLambdaEventSourceMappingAttributes(&conf), 28 ), 29 }, 30 resource.TestStep{ 31 Config: testAccAWSLambdaEventSourceMappingConfigUpdate, 32 Check: resource.ComposeTestCheckFunc( 33 testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), 34 resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", 35 "batch_size", 36 strconv.Itoa(200)), 37 resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", 38 "enabled", 39 strconv.FormatBool(false)), 40 resource.TestMatchResourceAttr( 41 "aws_lambda_event_source_mapping.lambda_event_source_mapping_test", 42 "function_arn", 43 regexp.MustCompile("example_lambda_name_update$"), 44 ), 45 ), 46 }, 47 }, 48 }) 49 } 50 51 func testAccCheckLambdaEventSourceMappingDestroy(s *terraform.State) error { 52 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 53 54 for _, rs := range s.RootModule().Resources { 55 if rs.Type != "aws_lambda_event_source_mapping" { 56 continue 57 } 58 59 _, err := conn.GetEventSourceMapping(&lambda.GetEventSourceMappingInput{ 60 UUID: aws.String(rs.Primary.ID), 61 }) 62 63 if err == nil { 64 return fmt.Errorf("Lambda event source mapping was not deleted") 65 } 66 67 } 68 69 return nil 70 71 } 72 73 func testAccCheckAwsLambdaEventSourceMappingExists(n string, mapping *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { 74 // Wait for IAM role 75 return func(s *terraform.State) error { 76 rs, ok := s.RootModule().Resources[n] 77 if !ok { 78 return fmt.Errorf("Lambda event source mapping not found: %s", n) 79 } 80 81 if rs.Primary.ID == "" { 82 return fmt.Errorf("Lambda event source mapping ID not set") 83 } 84 85 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 86 87 params := &lambda.GetEventSourceMappingInput{ 88 UUID: aws.String(rs.Primary.ID), 89 } 90 91 getSourceMappingConfiguration, err := conn.GetEventSourceMapping(params) 92 if err != nil { 93 return err 94 } 95 96 *mapping = *getSourceMappingConfiguration 97 98 return nil 99 } 100 } 101 102 func testAccCheckAWSLambdaEventSourceMappingAttributes(mapping *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { 103 return func(s *terraform.State) error { 104 uuid := *mapping.UUID 105 if uuid == "" { 106 return fmt.Errorf("Could not read Lambda event source mapping's UUID") 107 } 108 109 return nil 110 } 111 } 112 113 const testAccAWSLambdaEventSourceMappingConfig = ` 114 resource "aws_iam_role" "iam_for_lambda" { 115 name = "iam_for_lambda" 116 assume_role_policy = <<EOF 117 { 118 "Version": "2012-10-17", 119 "Statement": [ 120 { 121 "Action": "sts:AssumeRole", 122 "Principal": { 123 "Service": "lambda.amazonaws.com" 124 }, 125 "Effect": "Allow", 126 "Sid": "" 127 } 128 ] 129 } 130 EOF 131 } 132 133 resource "aws_iam_policy" "policy_for_role" { 134 name = "policy_for_role" 135 path = "/" 136 description = "IAM policy for for Lamda event mapping testing" 137 policy = <<EOF 138 { 139 "Version": "2012-10-17", 140 "Statement": [ 141 { 142 "Effect": "Allow", 143 "Action": [ 144 "kinesis:GetRecords", 145 "kinesis:GetShardIterator", 146 "kinesis:DescribeStream" 147 ], 148 "Resource": "*" 149 }, 150 { 151 "Effect": "Allow", 152 "Action": [ 153 "kinesis:ListStreams" 154 ], 155 "Resource": "*" 156 } 157 ] 158 } 159 EOF 160 } 161 162 resource "aws_iam_policy_attachment" "policy_attachment_for_role" { 163 name = "policy_attachment_for_role" 164 roles = ["${aws_iam_role.iam_for_lambda.name}"] 165 policy_arn = "${aws_iam_policy.policy_for_role.arn}" 166 } 167 168 resource "aws_kinesis_stream" "kinesis_stream_test" { 169 name = "kinesis_stream_test" 170 shard_count = 1 171 } 172 173 resource "aws_lambda_function" "lambda_function_test_create" { 174 filename = "test-fixtures/lambdatest.zip" 175 function_name = "example_lambda_name_create" 176 role = "${aws_iam_role.iam_for_lambda.arn}" 177 handler = "exports.example" 178 } 179 180 resource "aws_lambda_function" "lambda_function_test_update" { 181 filename = "test-fixtures/lambdatest.zip" 182 function_name = "example_lambda_name_update" 183 role = "${aws_iam_role.iam_for_lambda.arn}" 184 handler = "exports.example" 185 } 186 187 resource "aws_lambda_event_source_mapping" "lambda_event_source_mapping_test" { 188 batch_size = 100 189 event_source_arn = "${aws_kinesis_stream.kinesis_stream_test.arn}" 190 enabled = true 191 depends_on = ["aws_iam_policy_attachment.policy_attachment_for_role"] 192 function_name = "${aws_lambda_function.lambda_function_test_create.arn}" 193 starting_position = "TRIM_HORIZON" 194 } 195 ` 196 197 const testAccAWSLambdaEventSourceMappingConfigUpdate = ` 198 resource "aws_iam_role" "iam_for_lambda" { 199 name = "iam_for_lambda" 200 assume_role_policy = <<EOF 201 { 202 "Version": "2012-10-17", 203 "Statement": [ 204 { 205 "Action": "sts:AssumeRole", 206 "Principal": { 207 "Service": "lambda.amazonaws.com" 208 }, 209 "Effect": "Allow", 210 "Sid": "" 211 } 212 ] 213 } 214 EOF 215 } 216 217 resource "aws_iam_policy" "policy_for_role" { 218 name = "policy_for_role" 219 path = "/" 220 description = "IAM policy for for Lamda event mapping testing" 221 policy = <<EOF 222 { 223 "Version": "2012-10-17", 224 "Statement": [ 225 { 226 "Effect": "Allow", 227 "Action": [ 228 "kinesis:GetRecords", 229 "kinesis:GetShardIterator", 230 "kinesis:DescribeStream" 231 ], 232 "Resource": "*" 233 }, 234 { 235 "Effect": "Allow", 236 "Action": [ 237 "kinesis:ListStreams" 238 ], 239 "Resource": "*" 240 } 241 ] 242 } 243 EOF 244 } 245 246 resource "aws_iam_policy_attachment" "policy_attachment_for_role" { 247 name = "policy_attachment_for_role" 248 roles = ["${aws_iam_role.iam_for_lambda.name}"] 249 policy_arn = "${aws_iam_policy.policy_for_role.arn}" 250 } 251 252 resource "aws_kinesis_stream" "kinesis_stream_test" { 253 name = "kinesis_stream_test" 254 shard_count = 1 255 } 256 257 resource "aws_lambda_function" "lambda_function_test_create" { 258 filename = "test-fixtures/lambdatest.zip" 259 function_name = "example_lambda_name_create" 260 role = "${aws_iam_role.iam_for_lambda.arn}" 261 handler = "exports.example" 262 } 263 264 resource "aws_lambda_function" "lambda_function_test_update" { 265 filename = "test-fixtures/lambdatest.zip" 266 function_name = "example_lambda_name_update" 267 role = "${aws_iam_role.iam_for_lambda.arn}" 268 handler = "exports.example" 269 } 270 271 resource "aws_lambda_event_source_mapping" "lambda_event_source_mapping_test" { 272 batch_size = 200 273 event_source_arn = "${aws_kinesis_stream.kinesis_stream_test.arn}" 274 enabled = false 275 depends_on = ["aws_iam_policy_attachment.policy_attachment_for_role"] 276 function_name = "${aws_lambda_function.lambda_function_test_update.arn}" 277 starting_position = "TRIM_HORIZON" 278 } 279 `