Skip to content

Instantly share code, notes, and snippets.

@kapcod
kapcod / export_athena_history.py
Created August 16, 2022 16:05
Exports Athena history in hours json.gz files on S3 for storage and analysis. Can then be analyzed using Athena itself.
import boto3
import gzip
import json
import time
import re
def export(workgroup, region, hours=1):
athena = boto3.client('athena', region_name=region)
current_hour_hist = []
next_token = None
@kapcod
kapcod / join_csv.rb
Last active August 3, 2022 13:38
Join 2 unsorted CSV files using key column, requires loading 1 of the 2 CSV in memory. Allows filtering.
require 'csv'
# This will create join of 2 CSVs based on key-column without deduplication
# The output CSV by order of csv1
# more memory-efficient is to put csv1 longest table and csv2 shortest with less key column duplication
def join_csv(csv1_path, key1, csv2_path, key2, csv3_path, key_convertor: nil, ignore_cols: [], filter_csv1: nil, filter_csv2: nil)
start_ts = Time.now.to_i
puts "Reading #{csv2_path}, size: #{File.size(csv2_path)}..."
csv2 = CSV.read(csv2_path, headers: true)
@kapcod
kapcod / aws-assume-role
Last active August 22, 2022 07:13
Bash script to run any command in assumed AWS role, requires aws-cli, tested on aws-cli 2, includes caching for 12 hours
#!/bin/bash -e
usage(){
cat <<'HELP'
Usage: aws-assume-role <base-profile> <mfa-ARN> <role-ARN> <session-name> [<command>...]
This script is designed to be used from alias like this (of course you can also call it from other scripts):
alias assume-admin-prod='/path/to/aws-assume-role bob arn:aws:iam::1234567:mfa/bob arn:aws:iam::321321321:role/admin bob'
Arguments are positional and not key-word options on purpose. You just copy-paste it into .bash_profile to create alias and replace the parameters with right values.
<base-profile> is needed in case your default profile already includes role switch, in this case 'sts assume-role' won't work.
@kapcod
kapcod / export_athena_usage.rb
Created December 29, 2021 11:17
Export AWS Athena usage history as CSV
require 'csv'
require 'aws-sdk-athena'
def export_athena_usage(limit:, region:, filter_scanned: 10**9)
athena = Aws::Athena::Client.new region: region
next_token = nil
fields = %w[query_execution_id query work_group]
stats_fields = %w[data_scanned_in_bytes total_execution_time_in_millis]
scanned = 0
found = 0
@kapcod
kapcod / cleanup_airflow_logs.rb
Created October 10, 2021 18:27
Airflow DAG log cleaner. Assumes log structure /var/log/airflow/<dag>/<operator>/<date>. Call: `cleanup_airflow_logs.rb 7` to retain only last week of logs
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'fileutils'
require 'time'
class AirflowLogCleaner
attr_reader :list, :to_delete
def list_folders
# frozen_string_literal: true
# SimpleThreadPool gives a simple implementation for producer/consumers pattern
# Example:
#
# pool = SimpleThreadPool.new(10, max_queue: 5) do |user_params|
# Net::HTTP.post(UPDATE_API, user_params)
# end
# users.each do |user|
# pool << user.update_api_params
@kapcod
kapcod / action_rate_limit.rb
Last active April 9, 2021 20:05
Simple action rate limit module for Ruby on Rails, intended to be included in ApplicationController
# frozen_string_literal: true
module ActionRateLimit
LIMIT_BY_OPTIONS = {
user: proc { request.cookies['user_id'] },
ip: proc { request.remote_ip },
}.freeze
def self.included(klass)
klass.extend ClassMethods
@kapcod
kapcod / README.md
Created August 25, 2020 15:33 — forked from skwashd/README.md
Copy AWS SSM Parameter Store Path

This Python (3.6+) script is for migrating Amazon AWS System Manager (SSM) Parameter Store keys from one path to another.

Quick Start

To install the script do the following:

  • Configure your AWS credentials
  • Grab the code from this gist
  • Make it executable (chmod +x /path/to/copy-ssm-ps-path.py)
  • pip install boto3 (if you don't have it installed already)
@kapcod
kapcod / case_sensitive_headers_post.rb
Created October 16, 2017 20:09
Hack to send request using Net::HTTP with preserved headers case
class CaseSensitiveHeadersPost < Net::HTTP::Post
def initialize_http_header(headers)
@header = {}
headers.each do |k,v|
@header[k] = [v]
end
end
def capitalize(name)
name
@kapcod
kapcod / fix_cookies_escaping.rb
Created January 5, 2017 12:57
Fix Rack::Utils.set_cookie_header! and delete_cookie_header! escaping
class << Rack::Utils
alias_method :escape_with_plus, :escape
def escape_without_plus(s)
URI.encode_www_form_component(s).gsub('+', '%20')
end
def mock_escape_for_cookies
singleton_class.send :alias_method, :escape, :escape_without_plus
yield