github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/lambda" 13 "github.com/hashicorp/terraform/helper/acctest" 14 "github.com/hashicorp/terraform/helper/resource" 15 "github.com/hashicorp/terraform/terraform" 16 ) 17 18 func TestAccAWSLambdaEventSourceMapping_basic(t *testing.T) { 19 var conf lambda.EventSourceMappingConfiguration 20 rInt := acctest.RandInt() 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccAWSLambdaEventSourceMappingConfig(rInt), 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), 31 testAccCheckAWSLambdaEventSourceMappingAttributes(&conf), 32 ), 33 }, 34 resource.TestStep{ 35 Config: testAccAWSLambdaEventSourceMappingConfigUpdate(rInt), 36 Check: resource.ComposeTestCheckFunc( 37 testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), 38 resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", 39 "batch_size", 40 strconv.Itoa(200)), 41 resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", 42 "enabled", 43 strconv.FormatBool(false)), 44 resource.TestMatchResourceAttr( 45 "aws_lambda_event_source_mapping.lambda_event_source_mapping_test", 46 "function_arn", 47 regexp.MustCompile("example_lambda_name_update$"), 48 ), 49 ), 50 }, 51 }, 52 }) 53 } 54 55 func TestAccAWSLambdaEventSourceMapping_disappears(t *testing.T) { 56 var conf lambda.EventSourceMappingConfiguration 57 58 rInt := acctest.RandInt() 59 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { testAccPreCheck(t) }, 62 Providers: testAccProviders, 63 CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy, 64 Steps: []resource.TestStep{ 65 resource.TestStep{ 66 Config: testAccAWSLambdaEventSourceMappingConfig(rInt), 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), 69 testAccCheckAWSLambdaEventSourceMappingDisappears(&conf), 70 ), 71 ExpectNonEmptyPlan: true, 72 }, 73 }, 74 }) 75 } 76 77 func testAccCheckAWSLambdaEventSourceMappingDisappears(conf *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { 78 return func(s *terraform.State) error { 79 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 80 81 params := &lambda.DeleteEventSourceMappingInput{ 82 UUID: conf.UUID, 83 } 84 85 _, err := conn.DeleteEventSourceMapping(params) 86 if err != nil { 87 if err != nil { 88 return err 89 } 90 } 91 92 return resource.Retry(10*time.Minute, func() *resource.RetryError { 93 params := &lambda.GetEventSourceMappingInput{ 94 UUID: conf.UUID, 95 } 96 _, err := conn.GetEventSourceMapping(params) 97 if err != nil { 98 cgw, ok := err.(awserr.Error) 99 if ok && cgw.Code() == "ResourceNotFoundException" { 100 return nil 101 } 102 return resource.NonRetryableError( 103 fmt.Errorf("Error retrieving Lambda Event Source Mapping: %s", err)) 104 } 105 return resource.RetryableError(fmt.Errorf( 106 "Waiting for Lambda Event Source Mapping: %v", conf.UUID)) 107 }) 108 } 109 } 110 111 func testAccCheckLambdaEventSourceMappingDestroy(s *terraform.State) error { 112 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 113 114 for _, rs := range s.RootModule().Resources { 115 if rs.Type != "aws_lambda_event_source_mapping" { 116 continue 117 } 118 119 _, err := conn.GetEventSourceMapping(&lambda.GetEventSourceMappingInput{ 120 UUID: aws.String(rs.Primary.ID), 121 }) 122 123 if err == nil { 124 return fmt.Errorf("Lambda event source mapping was not deleted") 125 } 126 127 } 128 129 return nil 130 131 } 132 133 func testAccCheckAwsLambdaEventSourceMappingExists(n string, mapping *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { 134 // Wait for IAM role 135 return func(s *terraform.State) error { 136 rs, ok := s.RootModule().Resources[n] 137 if !ok { 138 return fmt.Errorf("Lambda event source mapping not found: %s", n) 139 } 140 141 if rs.Primary.ID == "" { 142 return fmt.Errorf("Lambda event source mapping ID not set") 143 } 144 145 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 146 147 params := &lambda.GetEventSourceMappingInput{ 148 UUID: aws.String(rs.Primary.ID), 149 } 150 151 getSourceMappingConfiguration, err := conn.GetEventSourceMapping(params) 152 if err != nil { 153 return err 154 } 155 156 *mapping = *getSourceMappingConfiguration 157 158 return nil 159 } 160 } 161 162 func testAccCheckAWSLambdaEventSourceMappingAttributes(mapping *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { 163 return func(s *terraform.State) error { 164 uuid := *mapping.UUID 165 if uuid == "" { 166 return fmt.Errorf("Could not read Lambda event source mapping's UUID") 167 } 168 169 return nil 170 } 171 } 172 173 func testAccAWSLambdaEventSourceMappingConfig(rInt int) string { 174 return fmt.Sprintf(` 175 resource "aws_iam_role" "iam_for_lambda" { 176 name = "iam_for_lambda_%d" 177 assume_role_policy = <<EOF 178 { 179 "Version": "2012-10-17", 180 "Statement": [ 181 { 182 "Action": "sts:AssumeRole", 183 "Principal": { 184 "Service": "lambda.amazonaws.com" 185 }, 186 "Effect": "Allow", 187 "Sid": "" 188 } 189 ] 190 } 191 EOF 192 } 193 194 resource "aws_iam_policy" "policy_for_role" { 195 name = "policy_for_role_%d" 196 path = "/" 197 description = "IAM policy for for Lamda event mapping testing" 198 policy = <<EOF 199 { 200 "Version": "2012-10-17", 201 "Statement": [ 202 { 203 "Effect": "Allow", 204 "Action": [ 205 "kinesis:GetRecords", 206 "kinesis:GetShardIterator", 207 "kinesis:DescribeStream" 208 ], 209 "Resource": "*" 210 }, 211 { 212 "Effect": "Allow", 213 "Action": [ 214 "kinesis:ListStreams" 215 ], 216 "Resource": "*" 217 } 218 ] 219 } 220 EOF 221 } 222 223 resource "aws_iam_policy_attachment" "policy_attachment_for_role" { 224 name = "policy_attachment_for_role_%d" 225 roles = ["${aws_iam_role.iam_for_lambda.name}"] 226 policy_arn = "${aws_iam_policy.policy_for_role.arn}" 227 } 228 229 resource "aws_kinesis_stream" "kinesis_stream_test" { 230 name = "kinesis_stream_test_%d" 231 shard_count = 1 232 } 233 234 resource "aws_lambda_function" "lambda_function_test_create" { 235 filename = "test-fixtures/lambdatest.zip" 236 function_name = "%d_example_lambda_name_create" 237 role = "${aws_iam_role.iam_for_lambda.arn}" 238 handler = "exports.example" 239 runtime = "nodejs4.3" 240 } 241 242 resource "aws_lambda_function" "lambda_function_test_update" { 243 filename = "test-fixtures/lambdatest.zip" 244 function_name = "%d_example_lambda_name_update" 245 role = "${aws_iam_role.iam_for_lambda.arn}" 246 handler = "exports.example" 247 runtime = "nodejs4.3" 248 } 249 250 resource "aws_lambda_event_source_mapping" "lambda_event_source_mapping_test" { 251 batch_size = 100 252 event_source_arn = "${aws_kinesis_stream.kinesis_stream_test.arn}" 253 enabled = true 254 depends_on = ["aws_iam_policy_attachment.policy_attachment_for_role"] 255 function_name = "${aws_lambda_function.lambda_function_test_create.arn}" 256 starting_position = "TRIM_HORIZON" 257 }`, rInt, rInt, rInt, rInt, rInt, rInt) 258 } 259 260 func testAccAWSLambdaEventSourceMappingConfigUpdate(rInt int) string { 261 return fmt.Sprintf(` 262 resource "aws_iam_role" "iam_for_lambda" { 263 name = "iam_for_lambda_%d" 264 assume_role_policy = <<EOF 265 { 266 "Version": "2012-10-17", 267 "Statement": [ 268 { 269 "Action": "sts:AssumeRole", 270 "Principal": { 271 "Service": "lambda.amazonaws.com" 272 }, 273 "Effect": "Allow", 274 "Sid": "" 275 } 276 ] 277 } 278 EOF 279 } 280 281 resource "aws_iam_policy" "policy_for_role" { 282 name = "policy_for_role_%d" 283 path = "/" 284 description = "IAM policy for for Lamda event mapping testing" 285 policy = <<EOF 286 { 287 "Version": "2012-10-17", 288 "Statement": [ 289 { 290 "Effect": "Allow", 291 "Action": [ 292 "kinesis:GetRecords", 293 "kinesis:GetShardIterator", 294 "kinesis:DescribeStream" 295 ], 296 "Resource": "*" 297 }, 298 { 299 "Effect": "Allow", 300 "Action": [ 301 "kinesis:ListStreams" 302 ], 303 "Resource": "*" 304 } 305 ] 306 } 307 EOF 308 } 309 310 resource "aws_iam_policy_attachment" "policy_attachment_for_role" { 311 name = "policy_attachment_for_role_%d" 312 roles = ["${aws_iam_role.iam_for_lambda.name}"] 313 policy_arn = "${aws_iam_policy.policy_for_role.arn}" 314 } 315 316 resource "aws_kinesis_stream" "kinesis_stream_test" { 317 name = "kinesis_stream_test_%d" 318 shard_count = 1 319 } 320 321 resource "aws_lambda_function" "lambda_function_test_create" { 322 filename = "test-fixtures/lambdatest.zip" 323 function_name = "%d_example_lambda_name_create" 324 role = "${aws_iam_role.iam_for_lambda.arn}" 325 handler = "exports.example" 326 runtime = "nodejs4.3" 327 } 328 329 resource "aws_lambda_function" "lambda_function_test_update" { 330 filename = "test-fixtures/lambdatest.zip" 331 function_name = "%d_example_lambda_name_update" 332 role = "${aws_iam_role.iam_for_lambda.arn}" 333 handler = "exports.example" 334 runtime = "nodejs4.3" 335 } 336 337 resource "aws_lambda_event_source_mapping" "lambda_event_source_mapping_test" { 338 batch_size = 200 339 event_source_arn = "${aws_kinesis_stream.kinesis_stream_test.arn}" 340 enabled = false 341 depends_on = ["aws_iam_policy_attachment.policy_attachment_for_role"] 342 function_name = "${aws_lambda_function.lambda_function_test_update.arn}" 343 starting_position = "TRIM_HORIZON" 344 }`, rInt, rInt, rInt, rInt, rInt, rInt) 345 }