Last active
May 31, 2021 21:23
-
-
Save dzimine/208d2dfb836239c24bca11aa9aaf752e to your computer and use it in GitHub Desktop.
Create AWS Greengrass group and core.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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.
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
This script works fine. Except the core which is not getting created. Is there a way for it ?