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