Skip to content

Instantly share code, notes, and snippets.

Avatar

Peter Baumgartner ipmb

View GitHub Profile
@ipmb
ipmb / pg_chown.py
Last active May 5, 2021
change ownership of all items in public schema using Python/psycopg2
View pg_chown.py
import psycopg2
NEW_USER = "fred"
types = {"c": "TABLE", "t": "TABLE", "r": "TABLE", "S": "SEQUENCE", "i": "INDEX", "v": "VIEW"}
with psycopg2.connect(**kwargs) as conn:
conn.autocommit = True
with conn.cursor() as curs:
for kind, name in types.items():
curs.execute(
@ipmb
ipmb / s3_upload.sh
Last active Apr 25, 2021
testing s3 upload credentials from your shell
View s3_upload.sh
pip install awscli # if you don't have it installed already
export AWS_ACCESS_KEY_ID=your-access-key # starts with AK
export AWS_SECRET_ACCESS_KEY=your-secret-key
echo "hello world" | aws s3 cp - s3://your-bucket-name/test.txt
aws s3 cp s3://your-bucket-name/test.txt - # this should output "hello world"
@ipmb
ipmb / cloudformation_template.json
Created Feb 3, 2021
Snippet of Cloudformation template to enable load balancer logging
View cloudformation_template.json
{
"Mappings": {
"LBAccountId": {
"af-south-1": {
"AccountId": "098369216593"
},
"ap-east-1": {
"AccountId": "754344448648"
},
"ap-northeast-1": {
@ipmb
ipmb / transfer.tf
Created Dec 18, 2020
AWS Transfer Server with Elastic IP in terraform
View transfer.tf
variable "TRANSFER_HOST_KEY" {}
locals {
transfer = {
az_count = 2
}
}
resource "aws_eip" "transfer" {
count = local.transfer.az_count
@ipmb
ipmb / check.sh
Last active Nov 11, 2020
pipenv version test
View check.sh
$ chmod +x install.sh
$ docker build -t pipenv .
$ docker create --name pipenv pipenv
$ docker cp pipenv:/pipenv .
$ diff pipenv/2018.11.14/Pipfile pipenv/2018.05.18/Pipfile && echo "no difference"
2d1
< name = "pypi"
5,6c4
<
< [dev-packages]
View terraform_crash.log
Error: Error in function call
on .terraform/modules/client_vpc/terraform-aws-vpc-2.39.0/main.tf line 20, in locals:
20: vpce_tags = merge(
21: var.tags,
22: var.vpc_endpoint_tags,
23: )
|----------------
| var.tags is empty map of string
| var.vpc_endpoint_tags is empty map of string
@ipmb
ipmb / push_static_to_s3.py
Created Apr 15, 2020
Django management command to push whitenoise static files to S3, respecting headers
View push_static_to_s3.py
import logging
from concurrent import futures
from typing import List, Tuple
import boto3
from django.conf import settings
from django.core.management import BaseCommand
from whitenoise.middleware import WhiteNoiseMiddleware
log = logging.getLogger(__name__)
@ipmb
ipmb / Dockerfile
Last active Jan 14, 2021
amazon-ssm-agent Dockerfile
View Dockerfile
FROM golang:1.12-alpine as builder
ARG VERSION=2.3.930.0
RUN set -ex && apk add --no-cache make git gcc libc-dev curl bash && \
curl -sLO https://github.com/aws/amazon-ssm-agent/archive/${VERSION}.tar.gz && \
mkdir -p /go/src/github.com && \
tar xzf ${VERSION}.tar.gz && \
mv amazon-ssm-agent-${VERSION} /go/src/github.com/amazon-ssm-agent && \
cd /go/src/github.com/amazon-ssm-agent && \
echo ${VERSION} > VERSION && \
gofmt -w agent && make checkstyle || ./Tools/bin/goimports -w agent && \
@ipmb
ipmb / cloudfront_signed_storage.py
Created Mar 25, 2020
Django S3 Storage backend to generate signed Cloudfront URLs
View cloudfront_signed_storage.py
"""
S3 Storage served via private Cloudfront URL
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/PrivateContent.html
https://github.com/boto/boto3/blob/develop/boto3/examples/cloudfront.rst
"""
import datetime
from botocore.signers import CloudFrontSigner
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
@ipmb
ipmb / docker-exec-ecs.sh
Last active Feb 16, 2021
docker exec on AWS ECS with SSM
View docker-exec-ecs.sh
#!/bin/bash
# USAGE: CLUSTER=mycluster SERVICE=myservice ./docker-exec-ecs.sh
set -euf -o pipefail
TASK_ARN=$(aws ecs list-tasks --cluster=$CLUSTER --service=$SERVICE \
| jq -r .taskArns[0])
if [ "$TASK_ARN" = "null" ]; then
echo "Could not find any running tasks for $SERVICE on cluster:$CLUSTER."
exit 1
fi