Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View benkehoe's full-sized avatar

Ben Kehoe benkehoe

View GitHub Profile
@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.
#
@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 / 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
@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_console_launcher.py
Created October 5, 2022 17:17
Launch the AWS web console from the CLI
# Copyright 2022 Ben Kehoe
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
@benkehoe
benkehoe / string_template_demo.py
Last active June 23, 2023 21:17
Demo of the two new methods of string.Template in Python 3.11
#!/usr/bin/env python3.11
# MIT No Attribution
#
# Copyright 2023 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 / ddb_composite_key_escaping.py
Last active May 14, 2023 21:33
Example composite key escaping for DynamoDB
import random
import re
import string
from typing import Iterable
import dataclasses
def escape(s: str) -> str:
return s.replace("#", "##")
def unescape(s: str) -> str:
@benkehoe
benkehoe / kms_random.md
Created April 28, 2023 14:45
Python random numbers from KMS.GenerateRandom

Python random numbers from KMS.GenerateRandom

Spurred by this twitter conversation. random.SystemRandom uses os.urandom as a source of bytes, but doesn't provide a way to use a different source of bytes. So stream_random.py is exactly that. Then kms_random.py has raw and buffered bytestreams pulling from KMS.GenerateRandom.

The main interface is kms_random.get_kms_random(boto3_session, buffer_size=None). The default buffer size is 16, chosen arbitrarily.

I do not vouch for the randomness properties of the results.

@benkehoe
benkehoe / dont-use-aws-s3-ls-to-check-credentials.md
Last active April 23, 2023 16:22
Use "aws sts get-caller-identity" instead of "aws s3 ls" for checking credentials

People shouldn't use aws s3 ls to check credentials

Here's why, and an SCP to stop them

Lots of people use aws s3 ls to check that they have valid credentials. If it succeeds, they assume they are good to go. Even AWS blog tutorials often use it. They're all wrong.

There's multiple things wrong with using aws s3 ls to check credential validity. The first is that it has an IAM permission, s3:ListAllMyBuckets, associated with it.

@benkehoe
benkehoe / timedelta_iso.py
Last active December 22, 2022 17:54
IS8601 functions for datetime.timedelta
# 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.
#