AWS Greengrass | How to create greengrass group via API (Node.js)
We all know the coming era is an era of IoT so if you are programmer and keen to learn about IoT then AWS IoT is one of the cool platform to start with.
But if you are newbie here you will face quite a problems while setting up your system even if you go through the official AWS documentation.
I found myself stuck and helpless for days when I tried to do things programmatically which I was able to do through AWS console easily. I don’t want the same for you so, don’t worry, I will help you out!
__ Get ready to get dirty with code!!!
I am working in Node.js, where I want to create AWS greengrass group using aws-sdk. When you create greengrass group using AWS console you just click on create group and it does all the necessary work for you on the go from creating core device for that group to creating cert and keys of that core device, policy and attaching that cert with created core device but on the backend all these are created separately and linked to each other at the end, that’s what we are going to do. Stay with me … you gonna do it.
Given below is step wise operations we will be performing:
- Create a thing (core device for GG group to be created)
- Create cert and key for core thing created in step 1
- Attach a policy with created cert and key above. I am assuming u have already created a policy document on AWS console, we will use just use its name
- Create greengrass core definition by using core device Id, core device Arn and cert and key
- Finally create greengrass group using the core definition created in previous step.
Given below is the code, explore by yourself and enjoy!
var AWS = require('aws-sdk');AWS.config.update({
"endpoint": "https://greengrass.your-region.amazonaws.com",
"accessKeyId": "yourAccessKeyId", //IAM User's
"secretAccessKey": "secretAccessKey" //IAM User's
});var greengrass = new AWS.Greengrass();AWS.config.update({
"endpoint": "http://iot.your-region.amazonaws.com",
"accessKeyId": "yourAccessKeyId", //IAM User's
"secretAccessKey": "secretAccessKey" //IAM User's
});var iot = new AWS.Iot();var greengrass = new AWS.Greengrass();var GG_Group_Name = 'GG-DEMO-GROUP'var params = {
thingName: GG_Group_Name+'_Core',
thingTypeName: 'core_device'
};
iot.createThing(params, function(err, iotThing) {
if (err){
res.status(400).send({
message: err.message
});
}
else{
console.log('GG group Core created ..');
// create certificate and keys
params = {
setAsActive: true
};
iot.createKeysAndCertificate(params, function(err, certAndKey) {
if (err){
res.status(400).send({
message: err.message
});
}
else{
console.log('certificate and key created ...');// attach policy to certificate
params = {
policyName: 'policy-name', /* required */ // assuming you have already policy created
principal: certAndKey.certificateArn /* required */
};
iot.attachPrincipalPolicy(params, function(err, data) {
if (err){
res.json({'attachPrincipalPolicy error:': err})
}
else{
console.log('attached policy with created certificate...');//attched created certificate to the created core thing
params = {
principal: certAndKey.certificateArn, /* required */
thingName: GG_Group_Name+'_Core' /* required */
};
iot.attachThingPrincipal(params, function(err, data) {
if (err) {
res.status(400).send({
message: err.message
});
}
else{
console.log('certificate attached to core device');// create core definition
params = {
InitialVersion: {
Cores: [
{
CertificateArn: certAndKey.certificateArn, //cert of core device
Id: iotThing.thingId, //id of core device device
// SyncShadow: true || false,
ThingArn: iotThing.thingArn //core thing arn
},
/* more items */
]
},
Name: iotThing.thingName
};
greengrass.createCoreDefinition(params, function(err, coreDef) {
if (err){
res.status(400).send({
message: err.message
});
}
else{
console.log('core definition created ..');
// now create greengrass group
params = {
InitialVersion: {
CoreDefinitionVersionArn: coreDef.LatestVersionArn
},
Name: GG_Group_Name
};
greengrass.createGroup(params, function(err, ggGroup) {
if (err){
res.status(400).send({
message: err.message
});
}
else{
console.log('GG group'+GG_Group_Name+'created ..');
var resp = {
'core': iotThing,
'certAndKey': certAndKey,
'ggGroup': ggGroup,
'coreDef': coreDef
};
res.json(resp);
}
});
}
});}
});
}
});
}
});
}
});