AWS Jar

PyPI version Downloads Python 3.6 Code style: black

Jar Logo

AWSJar makes it easy to save data from AWS Lambda.

The data (either a dict or list) can be saved within the Lambda itself as an environment variable or on S3.

Install

pip install awsjar

Examples

Increment a sum with every invocation

import awsjar

def lambda_handler(event, context):
    jar = awsjar.Jar(context.function_name)
    data = jar.get()  # Will return an empty dict if state does not already exist.

    s = data.get("sum", 0)
    data["sum"] = s + 1

    jar.put(data)
    
    return data

Make sure your website is up

import awsjar
import requests

# Set a CloudWatch Event to run this Lambda every minute.
def lambda_handler(event, context):
    jar = awsjar.Jar(context.function_name)
    data = jar.get()  # Will return an empty dict if state does not already exist.
    
    last_status_code = data.get("last_status_code", 200)
    
    result = requests.get('http://my-website.com')
    cur_status_code = result.status_code
    
    if last_status_code != 200 and cur_status_code != 200:
        print('Website might be down!')

    jar.put({'last_status_code': cur_status_code})

Save data to S3

import awsjar

# Save your data to an S3 object - s3://my-bucket/state.json 
bkt = awsjar.Bucket('my-bucket', key='state.json')

data = {'num_acorns': 50, 'acorn_hideouts': ['tree', 'lake', 'backyard']}
bkt.put(data)

state = bkt.get()
>> {'num_acorns': 50, 'acorn_hideouts': ['tree', 'lake', 'backyard']}

Docs

User Guide

Contributing

Please see the contributing guide for more specifics.

Contact / Support

Please use the Issues page

I greatly appreciate any feedback / suggestions!

License

Distributed under the Apache License 2.0. See LICENSE for more information.