github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_lambda_event_source_mapping.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/lambda"
     9  
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsLambdaEventSourceMapping() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsLambdaEventSourceMappingCreate,
    16  		Read:   resourceAwsLambdaEventSourceMappingRead,
    17  		Update: resourceAwsLambdaEventSourceMappingUpdate,
    18  		Delete: resourceAwsLambdaEventSourceMappingDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"event_source_arn": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  			"function_name": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  			},
    30  			"starting_position": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  			"batch_size": &schema.Schema{
    36  				Type:     schema.TypeInt,
    37  				Optional: true,
    38  				Default:  100,
    39  			},
    40  			"enabled": &schema.Schema{
    41  				Type:     schema.TypeBool,
    42  				Optional: true,
    43  				Default:  true,
    44  			},
    45  			"function_arn": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Computed: true,
    48  			},
    49  			"last_modified": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Computed: true,
    52  			},
    53  			"last_processing_result": &schema.Schema{
    54  				Type:     schema.TypeString,
    55  				Computed: true,
    56  			},
    57  			"state": &schema.Schema{
    58  				Type:     schema.TypeString,
    59  				Computed: true,
    60  			},
    61  			"state_transition_reason": &schema.Schema{
    62  				Type:     schema.TypeString,
    63  				Computed: true,
    64  			},
    65  			"uuid": &schema.Schema{
    66  				Type:     schema.TypeString,
    67  				Computed: true,
    68  			},
    69  		},
    70  	}
    71  }
    72  
    73  // resourceAwsLambdaEventSourceMappingCreate maps to:
    74  // CreateEventSourceMapping in the API / SDK
    75  func resourceAwsLambdaEventSourceMappingCreate(d *schema.ResourceData, meta interface{}) error {
    76  	conn := meta.(*AWSClient).lambdaconn
    77  
    78  	functionName := d.Get("function_name").(string)
    79  	eventSourceArn := d.Get("event_source_arn").(string)
    80  
    81  	log.Printf("[DEBUG] Creating Lambda event source mapping: source %s to function %s", eventSourceArn, functionName)
    82  
    83  	params := &lambda.CreateEventSourceMappingInput{
    84  		EventSourceArn:   aws.String(eventSourceArn),
    85  		FunctionName:     aws.String(functionName),
    86  		StartingPosition: aws.String(d.Get("starting_position").(string)),
    87  		BatchSize:        aws.Int64(int64(d.Get("batch_size").(int))),
    88  		Enabled:          aws.Bool(d.Get("enabled").(bool)),
    89  	}
    90  
    91  	eventSourceMappingConfiguration, err := conn.CreateEventSourceMapping(params)
    92  	if err != nil {
    93  		return fmt.Errorf("Error creating Lambda event source mapping: %s", err)
    94  	}
    95  
    96  	d.Set("uuid", eventSourceMappingConfiguration.UUID)
    97  	d.SetId(*eventSourceMappingConfiguration.UUID)
    98  
    99  	return resourceAwsLambdaEventSourceMappingRead(d, meta)
   100  }
   101  
   102  // resourceAwsLambdaEventSourceMappingRead maps to:
   103  // GetEventSourceMapping in the API / SDK
   104  func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interface{}) error {
   105  	conn := meta.(*AWSClient).lambdaconn
   106  
   107  	log.Printf("[DEBUG] Fetching Lambda event source mapping: %s", d.Id())
   108  
   109  	params := &lambda.GetEventSourceMappingInput{
   110  		UUID: aws.String(d.Id()),
   111  	}
   112  
   113  	eventSourceMappingConfiguration, err := conn.GetEventSourceMapping(params)
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	d.Set("batch_size", eventSourceMappingConfiguration.BatchSize)
   119  	d.Set("event_source_arn", eventSourceMappingConfiguration.EventSourceArn)
   120  	d.Set("function_arn", eventSourceMappingConfiguration.FunctionArn)
   121  	d.Set("last_modified", eventSourceMappingConfiguration.LastModified)
   122  	d.Set("last_processing_result", eventSourceMappingConfiguration.LastProcessingResult)
   123  	d.Set("state", eventSourceMappingConfiguration.State)
   124  	d.Set("state_transition_reason", eventSourceMappingConfiguration.StateTransitionReason)
   125  	d.Set("uuid", eventSourceMappingConfiguration.UUID)
   126  
   127  	return nil
   128  }
   129  
   130  // resourceAwsLambdaEventSourceMappingDelete maps to:
   131  // DeleteEventSourceMapping in the API / SDK
   132  func resourceAwsLambdaEventSourceMappingDelete(d *schema.ResourceData, meta interface{}) error {
   133  	conn := meta.(*AWSClient).lambdaconn
   134  
   135  	log.Printf("[INFO] Deleting Lambda event source mapping: %s", d.Id())
   136  
   137  	params := &lambda.DeleteEventSourceMappingInput{
   138  		UUID: aws.String(d.Id()),
   139  	}
   140  
   141  	_, err := conn.DeleteEventSourceMapping(params)
   142  	if err != nil {
   143  		return fmt.Errorf("Error deleting Lambda event source mapping: %s", err)
   144  	}
   145  
   146  	d.SetId("")
   147  
   148  	return nil
   149  }
   150  
   151  // resourceAwsLambdaEventSourceMappingUpdate maps to:
   152  // UpdateEventSourceMapping in the API / SDK
   153  func resourceAwsLambdaEventSourceMappingUpdate(d *schema.ResourceData, meta interface{}) error {
   154  	conn := meta.(*AWSClient).lambdaconn
   155  
   156  	log.Printf("[DEBUG] Updating Lambda event source mapping: %s", d.Id())
   157  
   158  	params := &lambda.UpdateEventSourceMappingInput{
   159  		UUID:         aws.String(d.Id()),
   160  		BatchSize:    aws.Int64(int64(d.Get("batch_size").(int))),
   161  		FunctionName: aws.String(d.Get("function_name").(string)),
   162  		Enabled:      aws.Bool(d.Get("enabled").(bool)),
   163  	}
   164  
   165  	_, err := conn.UpdateEventSourceMapping(params)
   166  	if err != nil {
   167  		return fmt.Errorf("Error updating Lambda event source mapping: %s", err)
   168  	}
   169  
   170  	return resourceAwsLambdaEventSourceMappingRead(d, meta)
   171  }