Why is the AWS Lambda function called multiple times for a single event?

I am trying to create an AWS Lambda function that performs the following process.

  • Get the S3 Event Set
  • Get file A from S3
  • Get file B from S3 that is called by lambda
  • Run only one instance of EC2
  • Create Tags for New EC2 Instance

Problem: Multiple instances (5) start unexpectedly.

The instance was created successfully, but 4 more instances are also launched. A total of 5 copies were launched.

Magazines

In the log threads for this function, I found 4 threads for this call. Each thread does not show any errors or exceptions, but it seems that the function is executed several times.

Trial

I guessed that the function was disabled and then restarted.

Then I changed Timeout from 5 to 60 seconds and put the file on S3. It somehow happened. Only 2 log threads appeared, the first shows that the function was executed only once, the second shows that the function was executed twice. The number of running instances is 3.

However, I do not know why several (3) instances are running.

Any comments are welcome! Thanks in advance: -)

My lambda function

My lambda function is as follows. (It simplifies hiding credential information, but does not lose its basic structure)

 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<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); } }); } } }); } }); } }); }; 
+8
amazon-s3 amazon-web-services amazon-ec2 aws-lambda aws-sdk
source share
2 answers

solvable.

Adding context.succeed(message); to the last part of the nested callback prevents re-execution of the function.

  ec2.createTags(params, function(err, data) { if (err) { console.log(err, err.stack); context.fail('Failed'); } else { console.log("Tags created."); console.log(data); context.succeed('Completed'); } }); 
+12
source share

I had the same problem with a newer version (Node.JS v4.3). Call

 context.callbackWaitsForEmptyEventLoop = false; 

before calling

 callback(...) 
+1
source share

All Articles