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