Skip to content

Instantly share code, notes, and snippets.

View benkehoe's full-sized avatar

Ben Kehoe benkehoe

View GitHub Profile
@benkehoe
benkehoe / .pythonrc.py
Last active January 27, 2022 22:06
Configuration for interactive python sessions
# MIT No Attribution
#
# Copyright 2022 Ben Kehoe
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
@benkehoe
benkehoe / py-args-for-bash.sh
Last active August 3, 2023 08:58
Python argument parsing for bash scripts
#!/bin/sh
# MIT No Attribution
#
# Copyright 2020 Ben Kehoe
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
@benkehoe
benkehoe / aws-profile-for-bashrc.sh
Last active April 2, 2024 10:41
AWS_PROFILE env var management
# MIT No Attribution
#
# Copyright 2022 Ben Kehoe
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
#!/bin/sh
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
@benkehoe
benkehoe / aws_error_utils.py
Last active January 27, 2022 22:09
Error matching for botocore
# This is a now proper library (that can still be copied into your project as a single file)
# https://github.com/benkehoe/aws-error-utils
# MIT No Attribution
#
# Copyright 2022 Ben Kehoe
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
@benkehoe
benkehoe / aws.opml
Last active November 20, 2023 20:32
AWS RSS feeds
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>AWS RSS feeds 2019-04-22</title>
</head>
<body>
<outline text="AWS" title="AWS">
<outline type="rss" text="Infrastructure &amp; Automation" title="Infrastructure &amp; Automation" xmlUrl="https://aws.amazon.com/blogs/infrastructure-and-automation/feed/" htmlUrl="https://aws.amazon.com/blogs/infrastructure-and-automation/"/>
<outline type="rss" text="AWS Developer Blog" title="AWS Developer Blog" xmlUrl="http://feeds.feedburner.com/AwsDeveloperBlog" htmlUrl="https://aws.amazon.com/blogs/developer/"/>
@benkehoe
benkehoe / argparse_repl.py
Last active March 21, 2019 04:32
Simple pattern for adding a REPL to argparse-based command line programs
# This came up when I couldn't add dependencies beyond the stdlib for a certain script
import argparse
import sys, os.path
def run(args):
# Do actual things
pass
def main():
@benkehoe
benkehoe / LambdaBase.py
Last active October 23, 2023 20:30
Code pattern for implementing class-based AWS Lambda handlers in Python
"""Base class for implementing Lambda handlers as classes.
Used across multiple Lambda functions (included in each zip file).
Add additional features here common to all your Lambdas, like logging."""
class LambdaBase(object):
@classmethod
def get_handler(cls, *args, **kwargs):
def handler(event, context):
return cls(*args, **kwargs).handle(event, context)
return handler

Use case: hit counter

At every path, just returns the number of times that path has been requested. Each stage should get its own table to store the counters, rather than sharing a table. Ideally, the linking between the table and the Lambda function happens at deployment time (i.e., the stages share the same code object in S3, not each having separate but identical objects) and not at build time (with separate, different code objects) or run time (that requires making extra API calls inside the Lambda function).

The crux of it is the following:

@benkehoe
benkehoe / awssh
Last active January 31, 2017 21:59
AWS EC2 ssh shell functions
# Shell functions for ssh-ing into and
# Features automatic adding of the key file when it is specified in
# AWSSH_KEY. If no user is given in the hostname, it will use the value
# in AWSSH_DEFAULT_USER, or ec2-user if that is not set.
# Sets StrictHostKeyChecking off, so it doesn't ask you if you want to
# connect. Doesn't add the IP to known_hosts, though it will warn you
# every time that it has.
awssh ()
(