Skip to content

Instantly share code, notes, and snippets.

Avatar

Ben Kehoe benkehoe

View GitHub Profile
@benkehoe
benkehoe / aws_orgs_for_each_account.py
Last active Jun 23, 2021
Template for running work in every account in an organization
View aws_orgs_for_each_account.py
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')
@benkehoe
benkehoe / my_package.py
Last active Apr 7, 2021
Single sourcing a python package version using importlib.metadata.version()
View my_package.py
try:
# importlib.metadata is present in Python 3.8 and later
import importlib.metadata as importlib_metadata
except ImportError:
# use the shim package importlib-metadata pre-3.8
import importlib_metadata as importlib_metadata
try:
__version__ = importlib_metadata.version(__package__ or __name__)
except importlib_metadata.PackageNotFoundError:
@benkehoe
benkehoe / aws_assume_role.py
Last active Dec 12, 2020
Assumed role session chaining (with credential refreshing) for boto3
View aws_assume_role.py
# This is now a proper library, see https://github.com/benkehoe/aws-assume-role-lib
# pip install aws-assume-role-lib
# Copyright 2020 Ben Kehoe
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
@benkehoe
benkehoe / .pythonrc.py
Last active Dec 12, 2020
Configuration for interactive python sessions
View .pythonrc.py
# This Python script will run at the start of any interactive
# Python session (that is, where it is giving you a prompt).
# I like to use it to preload libraries and define functions I
# find useful in those situations.
# This script also provides for per-directory .pythonrc.py files.
# NOTE: any variables in here (e.g., loop variables) persist
# into your session. I recommend prefixing any variables you use
# in .pythonrc.py with underscores to keep them out of the way.
@benkehoe
benkehoe / py-args-for-bash.sh
Last active Dec 22, 2020
Python argument parsing for bash scripts
View py-args-for-bash.sh
#!/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-profile-for-bashrc.sh
Last active Jul 21, 2021
AWS_PROFILE env var management
View aws-profile-for-bashrc.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
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
View aws-configure-sso-profile.sh
#!/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 May 16, 2020
Error matching for botocore
View aws_error_utils.py
# 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
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
@benkehoe
benkehoe / aws.opml
Last active Jul 27, 2021
AWS RSS feeds
View aws.opml
<?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 Mar 21, 2019
Simple pattern for adding a REPL to argparse-based command line programs
View argparse_repl.py
# 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():