Skip to content

Instantly share code, notes, and snippets.

@dzimine
Last active May 31, 2021 21:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dzimine/208d2dfb836239c24bca11aa9aaf752e to your computer and use it in GitHub Desktop.
Save dzimine/208d2dfb836239c24bca11aa9aaf752e to your computer and use it in GitHub Desktop.
Create AWS Greengrass group and core.
import json
import yaml
import boto3
gg = boto3.client('greengrass')
iot = boto3.client('iot')
group = gg.create_group(Name="my_group")
keys_cert = iot.create_keys_and_certificate(setAsActive=True)
core_thing = iot.create_thing(thingName="my_group_core_1")
iot.attach_thing_principal(
thingName=core_thing['thingName'],
principal=keys_cert['certificateArn'])
core_policy_doc = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["iot:Publish", "iot:Subscribe", "iot:Connect", "iot:Receive", "iot:GetThingShadow", "iot:DeleteThingShadow", "iot:UpdateThingShadow"],
"Resource": ["arn:aws:iot:" + boto3.session.Session().region_name + ":*:*"]
},
{
"Effect": "Allow",
"Action": ["greengrass:AssumeRoleForGroup", "greengrass:CreateCertificate", "greengrass:GetConnectivityInfo", "greengrass:GetDeployment", "greengrass:GetDeploymentArtifacts", "greengrass:UpdateConnectivityInfo", "greengrass:UpdateCoreDeploymentStatus"],
"Resource": ["*"]
}
]
}
policy = iot.create_policy(
policyName="my_core_1_policy",
policyDocument=json.dumps(core_policy_doc))
iot.attach_principal_policy(
policyName=policy['policyName'],
principal=keys_cert['certificateArn'])
initial_version = {'Cores': [
{
'Id': core_thing['thingName'], # Quite intuitive, eh?
'CertificateArn': keys_cert['certificateArn'],
'SyncShadow': False, # Up to you, True|False
'ThingArn': core_thing['thingArn']
}
]}
core_definition = gg.create_core_definition(
Name="{0}_core_def".format(group['Name']),
InitialVersion=initial_version)
# SAVE created entities to the file.
# You'll thank me for that when it's time to clean things up.
state = {
'group': group,
'core_thing': core_thing,
'keys_cert': keys_cert,
'group_ver': group_ver,
'core_definition': core_definition,
'policy': policy
}
with open('./state.json', 'w') as f:
json.dump(state, f, indent=4)
@kripscorp
Copy link

This script works fine. Except the core which is not getting created. Is there a way for it ?

@dev-td7
Copy link

dev-td7 commented Jul 16, 2019

One last step was missing. You should add the core definition version to the group you just created.

gg.create_group_version(
    CoreDefinitionVersionArn=core_definition['LatestVersionArn'],
    GroupId=group['Arn']
)

This will create the core for the group. Now go to your group and under 'Cores' you will be able to see the thing listed as a core.

@dzimine
Copy link
Author

dzimine commented Jul 18, 2019

Thank you! I figured that out and codified in in greengo: https://github.com/dzimine/greengo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment