github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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(5*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  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "ResourceNotFoundException" {
   138  			log.Printf("[DEBUG] Lambda event source mapping (%s) not found", d.Id())
   139  			d.SetId("")
   140  
   141  			return nil
   142  		}
   143  		return err
   144  	}
   145  
   146  	d.Set("batch_size", eventSourceMappingConfiguration.BatchSize)
   147  	d.Set("event_source_arn", eventSourceMappingConfiguration.EventSourceArn)
   148  	d.Set("function_arn", eventSourceMappingConfiguration.FunctionArn)
   149  	d.Set("last_modified", eventSourceMappingConfiguration.LastModified)
   150  	d.Set("last_processing_result", eventSourceMappingConfiguration.LastProcessingResult)
   151  	d.Set("state", eventSourceMappingConfiguration.State)
   152  	d.Set("state_transition_reason", eventSourceMappingConfiguration.StateTransitionReason)
   153  	d.Set("uuid", eventSourceMappingConfiguration.UUID)
   154  
   155  	return nil
   156  }
   157  
   158  // resourceAwsLambdaEventSourceMappingDelete maps to:
   159  // DeleteEventSourceMapping in the API / SDK
   160  func resourceAwsLambdaEventSourceMappingDelete(d *schema.ResourceData, meta interface{}) error {
   161  	conn := meta.(*AWSClient).lambdaconn
   162  
   163  	log.Printf("[INFO] Deleting Lambda event source mapping: %s", d.Id())
   164  
   165  	params := &lambda.DeleteEventSourceMappingInput{
   166  		UUID: aws.String(d.Id()),
   167  	}
   168  
   169  	_, err := conn.DeleteEventSourceMapping(params)
   170  	if err != nil {
   171  		return fmt.Errorf("Error deleting Lambda event source mapping: %s", err)
   172  	}
   173  
   174  	d.SetId("")
   175  
   176  	return nil
   177  }
   178  
   179  // resourceAwsLambdaEventSourceMappingUpdate maps to:
   180  // UpdateEventSourceMapping in the API / SDK
   181  func resourceAwsLambdaEventSourceMappingUpdate(d *schema.ResourceData, meta interface{}) error {
   182  	conn := meta.(*AWSClient).lambdaconn
   183  
   184  	log.Printf("[DEBUG] Updating Lambda event source mapping: %s", d.Id())
   185  
   186  	params := &lambda.UpdateEventSourceMappingInput{
   187  		UUID:         aws.String(d.Id()),
   188  		BatchSize:    aws.Int64(int64(d.Get("batch_size").(int))),
   189  		FunctionName: aws.String(d.Get("function_name").(string)),
   190  		Enabled:      aws.Bool(d.Get("enabled").(bool)),
   191  	}
   192  
   193  	err := resource.Retry(5*time.Minute, func() *resource.RetryError {
   194  		_, err := conn.UpdateEventSourceMapping(params)
   195  		if err != nil {
   196  			if awserr, ok := err.(awserr.Error); ok {
   197  				if awserr.Code() == "InvalidParameterValueException" {
   198  					return resource.RetryableError(awserr)
   199  				}
   200  			}
   201  			return resource.NonRetryableError(err)
   202  		}
   203  		return nil
   204  	})
   205  
   206  	if err != nil {
   207  		return fmt.Errorf("Error updating Lambda event source mapping: %s", err)
   208  	}
   209  
   210  	return resourceAwsLambdaEventSourceMappingRead(d, meta)
   211  }