-
-
Save JamesOBenson/473e98b065ff6b15a4c8e42b77b91c39 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment