github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/aws/resource_aws_lambda_event_source_mapping.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 "github.com/aws/aws-sdk-go/service/lambda" 11 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/helper/schema" 14 ) 15 16 func resourceAwsLambdaEventSourceMapping() *schema.Resource { 17 return &schema.Resource{ 18 Create: resourceAwsLambdaEventSourceMappingCreate, 19 Read: resourceAwsLambdaEventSourceMappingRead, 20 Update: resourceAwsLambdaEventSourceMappingUpdate, 21 Delete: resourceAwsLambdaEventSourceMappingDelete, 22 Importer: &schema.ResourceImporter{ 23 State: schema.ImportStatePassthrough, 24 }, 25 26 Schema: map[string]*schema.Schema{ 27 "event_source_arn": { 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 "function_name": { 33 Type: schema.TypeString, 34 Required: true, 35 }, 36 "starting_position": { 37 Type: schema.TypeString, 38 Required: true, 39 ForceNew: true, 40 }, 41 "batch_size": { 42 Type: schema.TypeInt, 43 Optional: true, 44 Default: 100, 45 }, 46 "enabled": { 47 Type: schema.TypeBool, 48 Optional: true, 49 Default: true, 50 }, 51 "function_arn": { 52 Type: schema.TypeString, 53 Computed: true, 54 }, 55 "last_modified": { 56 Type: schema.TypeString, 57 Computed: true, 58 }, 59 "last_processing_result": { 60 Type: schema.TypeString, 61 Computed: true, 62 }, 63 "state": { 64 Type: schema.TypeString, 65 Computed: true, 66 }, 67 "state_transition_reason": { 68 Type: schema.TypeString, 69 Computed: true, 70 }, 71 "uuid": { 72 Type: schema.TypeString, 73 Computed: true, 74 }, 75 }, 76 } 77 } 78 79 // resourceAwsLambdaEventSourceMappingCreate maps to: 80 // CreateEventSourceMapping in the API / SDK 81 func resourceAwsLambdaEventSourceMappingCreate(d *schema.ResourceData, meta interface{}) error { 82 conn := meta.(*AWSClient).lambdaconn 83 84 functionName := d.Get("function_name").(string) 85 eventSourceArn := d.Get("event_source_arn").(string) 86 87 log.Printf("[DEBUG] Creating Lambda event source mapping: source %s to function %s", eventSourceArn, functionName) 88 89 params := &lambda.CreateEventSourceMappingInput{ 90 EventSourceArn: aws.String(eventSourceArn), 91 FunctionName: aws.String(functionName), 92 StartingPosition: aws.String(d.Get("starting_position").(string)), 93 BatchSize: aws.Int64(int64(d.Get("batch_size").(int))), 94 Enabled: aws.Bool(d.Get("enabled").(bool)), 95 } 96 97 // IAM profiles and roles can take some time to propagate in AWS: 98 // http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#launch-instance-with-role-console 99 // Error creating Lambda function: InvalidParameterValueException: The 100 // function defined for the task cannot be assumed by Lambda. 101 // 102 // The role may exist, but the permissions may not have propagated, so we 103 // retry 104 err := resource.Retry(5*time.Minute, func() *resource.RetryError { 105 eventSourceMappingConfiguration, err := conn.CreateEventSourceMapping(params) 106 if err != nil { 107 if awserr, ok := err.(awserr.Error); ok { 108 if awserr.Code() == "InvalidParameterValueException" { 109 return resource.RetryableError(awserr) 110 } 111 } 112 return resource.NonRetryableError(err) 113 } 114 // No error 115 d.Set("uuid", eventSourceMappingConfiguration.UUID) 116 d.SetId(*eventSourceMappingConfiguration.UUID) 117 return nil 118 }) 119 120 if err != nil { 121 return fmt.Errorf("Error creating Lambda event source mapping: %s", err) 122 } 123 124 return resourceAwsLambdaEventSourceMappingRead(d, meta) 125 } 126 127 // resourceAwsLambdaEventSourceMappingRead maps to: 128 // GetEventSourceMapping in the API / SDK 129 func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interface{}) error { 130 conn := meta.(*AWSClient).lambdaconn 131 132 log.Printf("[DEBUG] Fetching Lambda event source mapping: %s", d.Id()) 133 134 params := &lambda.GetEventSourceMappingInput{ 135 UUID: aws.String(d.Id()), 136 } 137 138 eventSourceMappingConfiguration, err := conn.GetEventSourceMapping(params) 139 if err != nil { 140 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "ResourceNotFoundException" { 141 log.Printf("[DEBUG] Lambda event source mapping (%s) not found", d.Id()) 142 d.SetId("") 143 144 return nil 145 } 146 return err 147 } 148 149 d.Set("batch_size", eventSourceMappingConfiguration.BatchSize) 150 d.Set("event_source_arn", eventSourceMappingConfiguration.EventSourceArn) 151 d.Set("function_arn", eventSourceMappingConfiguration.FunctionArn) 152 d.Set("last_modified", eventSourceMappingConfiguration.LastModified) 153 d.Set("last_processing_result", eventSourceMappingConfiguration.LastProcessingResult) 154 d.Set("state", eventSourceMappingConfiguration.State) 155 d.Set("state_transition_reason", eventSourceMappingConfiguration.StateTransitionReason) 156 d.Set("uuid", eventSourceMappingConfiguration.UUID) 157 d.Set("function_name", eventSourceMappingConfiguration.FunctionArn) 158 159 return nil 160 } 161 162 // resourceAwsLambdaEventSourceMappingDelete maps to: 163 // DeleteEventSourceMapping in the API / SDK 164 func resourceAwsLambdaEventSourceMappingDelete(d *schema.ResourceData, meta interface{}) error { 165 conn := meta.(*AWSClient).lambdaconn 166 167 log.Printf("[INFO] Deleting Lambda event source mapping: %s", d.Id()) 168 169 params := &lambda.DeleteEventSourceMappingInput{ 170 UUID: aws.String(d.Id()), 171 } 172 173 _, err := conn.DeleteEventSourceMapping(params) 174 if err != nil { 175 return fmt.Errorf("Error deleting Lambda event source mapping: %s", err) 176 } 177 178 d.SetId("") 179 180 return nil 181 } 182 183 // resourceAwsLambdaEventSourceMappingUpdate maps to: 184 // UpdateEventSourceMapping in the API / SDK 185 func resourceAwsLambdaEventSourceMappingUpdate(d *schema.ResourceData, meta interface{}) error { 186 conn := meta.(*AWSClient).lambdaconn 187 188 log.Printf("[DEBUG] Updating Lambda event source mapping: %s", d.Id()) 189 190 params := &lambda.UpdateEventSourceMappingInput{ 191 UUID: aws.String(d.Id()), 192 BatchSize: aws.Int64(int64(d.Get("batch_size").(int))), 193 FunctionName: aws.String(d.Get("function_name").(string)), 194 Enabled: aws.Bool(d.Get("enabled").(bool)), 195 } 196 197 err := resource.Retry(5*time.Minute, func() *resource.RetryError { 198 _, err := conn.UpdateEventSourceMapping(params) 199 if err != nil { 200 if awserr, ok := err.(awserr.Error); ok { 201 if awserr.Code() == "InvalidParameterValueException" { 202 return resource.RetryableError(awserr) 203 } 204 } 205 return resource.NonRetryableError(err) 206 } 207 return nil 208 }) 209 210 if err != nil { 211 return fmt.Errorf("Error updating Lambda event source mapping: %s", err) 212 } 213 214 return resourceAwsLambdaEventSourceMappingRead(d, meta) 215 }