Lonergan6275
Lonergan6275

Reputation: 2038

Create AWS IOT thingGroup in Lambda

I am Trying to create an IOT thing group in a Lambda function. When I try run this function it just times out and no other errors appear in the logs. Increasing the execution duration does not help.

var AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
    const region = "eu-west-1";
    const iotParams = {"apiVersion": "2019-05-28", "region": region};
    var iot = new AWS.Iot(iotParams);
    var params = {
        thingGroupName: 'test',
        tags: [
            {
                Key: 'name',
                Value: 'test'
            },
            /* more items */
        ],
        thingGroupProperties: {
            attributePayload: {
                attributes: {
                    'name': 'test',
                },
                merge: false
            },
            thingGroupDescription: 'test'
        }
    };
    iot.createThingGroup(params, function(err, data) {
        if (err) {
            callback(err);
        }
        else {
            callback(null, data);
        }
    });
};

Upvotes: 0

Views: 123

Answers (1)

Marcin
Marcin

Reputation: 238259

Common reason for timeouts when using lambda in a VPC is the fact that lambda in VPC does not have intenet access nor public IP. From docs:

Connect your function to private subnets to access private resources. If your function needs internet access, use NAT. Connecting a function to a public subnet does not give it internet access or a public IP address.

Also the lambda requires special permissions in its execution policy:

ec2:CreateNetworkInterface

ec2:DescribeNetworkInterfaces

ec2:DeleteNetworkInterface

To access AWS services from lambda in VPC, NAT gateway or instance are required. Alternatively, VPC endpoints can be used for supported services (IoT is not one of them).

Upvotes: 1

Related Questions