github.com/tam7t/terraform@v0.7.0-rc2.0.20160705125922-be2469a05c5e/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() *resource.RetryError { 102 eventSourceMappingConfiguration, err := conn.CreateEventSourceMapping(params) 103 if err != nil { 104 if awserr, ok := err.(awserr.Error); ok { 105 if awserr.Code() == "InvalidParameterValueException" { 106 return resource.RetryableError(awserr) 107 } 108 } 109 return resource.NonRetryableError(err) 110 } 111 // No error 112 d.Set("uuid", eventSourceMappingConfiguration.UUID) 113 d.SetId(*eventSourceMappingConfiguration.UUID) 114 return nil 115 }) 116 117 if err != nil { 118 return fmt.Errorf("Error creating Lambda event source mapping: %s", err) 119 } 120 121 return resourceAwsLambdaEventSourceMappingRead(d, meta) 122 } 123 124 // resourceAwsLambdaEventSourceMappingRead maps to: 125 // GetEventSourceMapping in the API / SDK 126 func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interface{}) error { 127 conn := meta.(*AWSClient).lambdaconn 128 129 log.Printf("[DEBUG] Fetching Lambda event source mapping: %s", d.Id()) 130 131 params := &lambda.GetEventSourceMappingInput{ 132 UUID: aws.String(d.Id()), 133 } 134 135 eventSourceMappingConfiguration, err := conn.GetEventSourceMapping(params) 136 if err != nil { 137 return err 138 } 139 140 d.Set("batch_size", eventSourceMappingConfiguration.BatchSize) 141 d.Set("event_source_arn", eventSourceMappingConfiguration.EventSourceArn) 142 d.Set("function_arn", eventSourceMappingConfiguration.FunctionArn) 143 d.Set("last_modified", eventSourceMappingConfiguration.LastModified) 144 d.Set("last_processing_result", eventSourceMappingConfiguration.LastProcessingResult) 145 d.Set("state", eventSourceMappingConfiguration.State) 146 d.Set("state_transition_reason", eventSourceMappingConfiguration.StateTransitionReason) 147 d.Set("uuid", eventSourceMappingConfiguration.UUID) 148 149 return nil 150 } 151 152 // resourceAwsLambdaEventSourceMappingDelete maps to: 153 // DeleteEventSourceMapping in the API / SDK 154 func resourceAwsLambdaEventSourceMappingDelete(d *schema.ResourceData, meta interface{}) error { 155 conn := meta.(*AWSClient).lambdaconn 156 157 log.Printf("[INFO] Deleting Lambda event source mapping: %s", d.Id()) 158 159 params := &lambda.DeleteEventSourceMappingInput{ 160 UUID: aws.String(d.Id()), 161 } 162 163 _, err := conn.DeleteEventSourceMapping(params) 164 if err != nil { 165 return fmt.Errorf("Error deleting Lambda event source mapping: %s", err) 166 } 167 168 d.SetId("") 169 170 return nil 171 } 172 173 // resourceAwsLambdaEventSourceMappingUpdate maps to: 174 // UpdateEventSourceMapping in the API / SDK 175 func resourceAwsLambdaEventSourceMappingUpdate(d *schema.ResourceData, meta interface{}) error { 176 conn := meta.(*AWSClient).lambdaconn 177 178 log.Printf("[DEBUG] Updating Lambda event source mapping: %s", d.Id()) 179 180 params := &lambda.UpdateEventSourceMappingInput{ 181 UUID: aws.String(d.Id()), 182 BatchSize: aws.Int64(int64(d.Get("batch_size").(int))), 183 FunctionName: aws.String(d.Get("function_name").(string)), 184 Enabled: aws.Bool(d.Get("enabled").(bool)), 185 } 186 187 err := resource.Retry(1*time.Minute, func() *resource.RetryError { 188 _, err := conn.UpdateEventSourceMapping(params) 189 if err != nil { 190 if awserr, ok := err.(awserr.Error); ok { 191 if awserr.Code() == "InvalidParameterValueException" { 192 return resource.RetryableError(awserr) 193 } 194 } 195 return resource.NonRetryableError(err) 196 } 197 return nil 198 }) 199 200 if err != nil { 201 return fmt.Errorf("Error updating Lambda event source mapping: %s", err) 202 } 203 204 return resourceAwsLambdaEventSourceMappingRead(d, meta) 205 }