AWS Code Sample

Accesses a local volume resource.

# Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"). You # may not use this file except in compliance with the License. A copy of # the License is located at # # # # or in the "license" file accompanying this file. This file is # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. # # This sample is used in the AWS IoT Greengrass Developer Guide: # # # Demonstrates a simple use case of local resource access. # This Lambda function writes a file "test" to a volume mounted inside # the Lambda environment under "/dest/LRAtest". Then it reads the file and # publishes the content to the AWS IoT "LRA/test" topic. import sys import greengrasssdk import platform import os import logging # Create a Greengrass Core SDK client. client = greengrasssdk.client('iot-data') volumePath = '/dest/LRAtest' def function_handler(event, context): client.publish(topic='LRA/test', payload='Sent from AWS IoT Greengrass Core.') try: volumeInfo = os.stat(volumePath) client.publish(topic='LRA/test', payload=str(volumeInfo)) with open(volumePath + '/test', 'a') as output: output.write('Successfully write to a file.\n') with open(volumePath + '/test', 'r') as myfile: data = client.publish(topic='LRA/test', payload=data) except Exception as e: logging.error("Experiencing error :{}".format(e)) return

Sample Details

Service: greengrass

Last tested: 2019-01-02

Author: AWS

Type: full-example

On this page: