title: Why AWS Lambda function invoked multiple times for a single event?
body: <p>I am trying to create AWS Lambda function that does following process.</p>
  
  <ul>
  <li>Receive S3 "Put" event </li>
  <li>Get fileA from S3</li>
  <li>Get fileB from S3 that invoked lambda</li>
  <li>Launch just one EC2 instance</li>
  <li>Create tags for the new EC2 instance</li>
  </ul>
  
  <p><strong>Problem: Multiple(5) instances are launched unexpectedly.</strong></p>
  
  <p>An instance is successfully created, but 4 other instances are also launched. 5 instances in total are launched.</p>
  
  <p><strong>Logs</strong></p>
  
  <p>In the Log Streams for this function, I found 4 Streams for this invocation. Each Stream doesn't show any errors or exceptions, but it seems that the function is executed repeatedly.</p>
  
  <p><strong>Trial</strong></p>
  
  <p>I guessed that the function has been timed out and then re-run.</p>
  
  <p>Then, I changed <code>Timeout</code> from 5s to 60s and put a file on S3.
  It somehow effected. Only 2 Log Streams appeared, first one shows that the function has been executed just once, second shows the function has been executed twice. Number of launched instances is 3.</p>
  
  <p>However, I have no idea why multiple(3) instances are launched.</p>
  
  <p>Any comments are welcome! 
  Thank you in advance :-)</p>
  
  <p><strong>My Lambda function</strong></p>
  
  <p>My Lambda function is following. (It's simplified to hide credential informations but it doesn't lose its basic structure)</p>
  
  <pre><code>var AWS = require('aws-sdk');
  
  function composeParams(data, config){
    var block_device_name = "/dev/xvdb";
    var security_groups = [
      "MyGroupName"
    ];
    var key_name = 'mykey';
    var security_group_ids = [
      "sg-xxxxxxx"
    ];
    var subnet_id = "subnet-xxxxxxx";
  
    // Configurations for a new EC2 instance
    var params = {
      ImageId: 'ami-22d27b22',      /* required */
      MaxCount: 1,                  /* required */
      MinCount: 1,                  /* required */
      KeyName: key_name,
      SecurityGroupIds: security_group_ids,
      InstanceType: data.instance_type,
      BlockDeviceMappings: [
        {
          DeviceName: block_device_name,
          Ebs: {
            DeleteOnTermination: true,
            Encrypted: true,
            VolumeSize: data.volume_size,
            VolumeType: 'gp2'
          }
        }
      ],
      Monitoring: {
        Enabled: false              /* required */
      },
      SubnetId: subnet_id,
      UserData: new Buffer(config).toString('base64'),
      DisableApiTermination: false,
      InstanceInitiatedShutdownBehavior: 'stop',
      DryRun: data.dry_run,
      EbsOptimized: false
    };
  
    return params;
  }
  
  exports.handler = function(event, context) {
    // Get the object from the event
    var s3 = new AWS.S3({ apiVersion: '2006-03-01' });
    var bucket = event.Records[0].s3.bucket.name;
    var key = event.Records[0].s3.object.key;
  
    // Get fileA
    var paramsA = {
      Bucket: bucket,
      Key: key
    };
    s3.getObject(paramsA, function(err, data) {
      if (err) {
        console.log(err);
      } else {
        var dataA = JSON.parse(String(data.Body));
  
        // Get fileB
        var paramsB = {
          Bucket: bucket,
          Key: 'config/config.yml'
        };
        s3.getObject(paramsB, function(err, data) {
          if (err) {
            console.log(err, err.stack);
          } else {
            var config = data.Body;
            /* Some process */
  
            // Launch EC2 Instance
            var ec2 = new AWS.EC2({ region: REGION, apiVersion: '2015-04-15' });
            var params = composeParams(dataA, config);
            ec2.runInstances(params, function(err, data) {
              if (err) {
                console.log(err, err.stack);
              } else {
                console.log(data);
  
                // Create tags for instance
                for (var i=0; i&lt;data.Instances.length; i++){
                  var instance = data.Instances[i];
                  var params = {
                    Resources: [                /* required */
                      instance.InstanceId
                    ],
                    Tags: [                     /* required */
                      {
                        Key: 'Name',
                        Value: instance_id
                      },
                      {
                        Key: 'userID',
                        Value: dataA.user_id
                      }
                    ],
                    DryRun: dataA.dry_run
                  };
                  ec2.createTags(params, function(err, data) {
                    if (err) {
                      console.log(err, err.stack);
                    } else {
                      console.log("Tags created.");
                      console.log(data);
                    }
                  });
                }
              }
            });
          }
        });
      }
    });
  };
  </code></pre>