Skip to content

Instantly share code, notes, and snippets.

View benkehoe's full-sized avatar

Ben Kehoe benkehoe

View GitHub Profile
@benkehoe
benkehoe / aws_assume_role.py
Last active November 13, 2022 13:48
Assumed role session chaining (with credential refreshing) for boto3
# *** WARNING ***
# This gist is no longer maintained
# It has been replaced by aws-assume-role-lib in PyPI
# Documentation at https://github.com/benkehoe/aws-assume-role-lib
# It is still a single-file library, you can find the stable version here:
# https://raw.githubusercontent.com/benkehoe/aws-assume-role-lib/stable/aws_assume_role_lib/aws_assume_role_lib.py
# (link also available in the docs)
# Copyright 2020 Ben Kehoe
#
@benkehoe
benkehoe / package_with_single_sourced_version.py
Last active June 22, 2022 13:31
Single sourcing a python package version using importlib.metadata.version()
# 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 / 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 / .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 / get_boto3_session_with_config.py
Last active January 27, 2022 22:03
A drop-in replacement for relying on well-known profiles in ~/.aws/config
# 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
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
@benkehoe
benkehoe / aws_orgs_for_each_account.py
Last active September 2, 2021 11:47
Template for running work in every account in an organization
import aws_assume_role_lib # https://github.com/benkehoe/aws-assume-role-lib
account_role_name = "YOUR_ACCOUNT_ROLE_NAME_HERE" # TODO: put your role name here
management_account_session = boto3.Session()
# if you're using AWS SSO in your management account and there's a specific role for this work, you could use aws-sso-lib
# https://github.com/benkehoe/aws-sso-util/blob/master/lib/README.md
# management_account_session = aws_sso_lib.get_boto3_session(start_url, sso_region, management_account_id, management_role_name, region=sso_region)
orgs = management_account_session.client('organizations')
<!DOCTYPE html>
<html>
<head>
<title>Draw Polygon</title>
<script type="text/javascript" src="http://lib.ivank.net/ivank.js"></script>
<script type="text/javascript" src="http://polyk.ivank.net/polyk.js"></script>
<script type="text/javascript">
var stage, s, dragged, complete, stopClick, rmv, cent, jsonText, cvs, sbt, area, centX, centY;
var remove = false;
//dragged is set to true if a Dot is being dragged
@benkehoe
benkehoe / JSON <-->YAML coversion scripts.md
Last active August 26, 2020 15:25
JSON <--> YAML conversion scripts

JSON <--> YAML conversion scripts

JSON is very standard. YAML, a superset of JSON, is very readable. Got something that needs JSON, and want to write the input in YAML? Use yaml2json. Have JSON and want to view it in a more readable format? Use json2yaml.

Use with files or stdin/stdout. Pretty-print the JSON if you want.

Requires pyyaml (via pip or easy_install), aka the python-yaml Ubuntu package.

A version of these files is available at tinyurl/json2yaml and tinyurl.com/yaml2json for you to wget or curl -L

#!/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 / 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():