Skip to content

Instantly share code, notes, and snippets.

View magickatt's full-sized avatar

Andrew Kirkpatrick magickatt

View GitHub Profile
@magickatt
magickatt / tolist.tf
Last active July 19, 2019 14:16
Iterate over set for resource using count and index in Terraform
resource "aws_route" "transit_gateway_private_subnet_route" {
count = length(data.aws_route_tables.private_subnet.ids)
route_table_id = tolist(data.aws_route_tables.private_subnet.ids)[count.index]
destination_cidr_block = var.destination_cidr_block
transit_gateway_id = var.transit_gateway_id
}
data "aws_route_tables" "private_subnet" {
vpc_id = var.vpc_id
tags = {
@magickatt
magickatt / github_clone_using_token.sh
Created September 6, 2019 17:31
Clone a GitHub repository using a Personal Access Token
export GITHUB_USER=magickatt
export GITHUB_TOKEN=secret
export GITHUB_REPOSITORY=magickatt/ContainerisingLegacyApplicationsTalk
git clone https://${GITHUB_USER}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}
@magickatt
magickatt / exception_traceback.py
Last active October 30, 2019 15:07
Get class, filename and line number from Python Exception
import os, sys, logging
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
try:
number = 1 / 0
except ZeroDivisionError as exception:
exception_message = str(exception)
exception_type, exception_object, exception_traceback = sys.exc_info()
filename = os.path.split(exception_traceback.tb_frame.f_code.co_filename)[1]
@magickatt
magickatt / check_ssl_validity.sh
Created March 30, 2020 15:08
Check SSL certificate validity
HOSTNAME=example.com && \
echo | openssl s_client -showcerts \
-servername $HOSTNAME \ # Required for SNI
-connect $HOSTNAME:443 2>/dev/null | \
openssl x509 -inform pem -noout -text | \
grep Validity -A 2
@magickatt
magickatt / port_forward_elasticsearch.sh
Created April 2, 2020 18:46
Port forward to Elasticsearch Pod or Service
# Port forward to a Pod
kubectl port-forward elasticsearch-0 9200:9200
# Port forward to a Service
kubectl port-forward service/elasticsearch 9200:9200
@magickatt
magickatt / missing_go_paths.sh
Created April 14, 2020 18:45
Missing Go paths
# Check what $GOPATH and $GOBIN are currently
echo "GOPATH = ${GOPATH}\nGOBIN = ${GOBIN}"
# Set them relative to your home directory
export GOPATH=$HOME && export GOBIN=$GOPATH/bin
echo "GOPATH = ${GOPATH}\nGOBIN = ${GOBIN}"
# Port forward to kube-state-metrics on the cluster
kubectl port-forward service/kube-state-metrics 8080:8080 -n kube-system &
# Hit the metrics endpoint
curl localhost:8080/metrics
@magickatt
magickatt / service_account.tf
Created May 19, 2020 19:43
Create Google Cloud Platform service account credentials JSON using Terraform
resource "google_service_account" "service_account" {
account_id = "test
display_name = "Test"
}
resource "google_service_account_key" "service_account" {
service_account_id = google_service_account.service_account.name
public_key_type = "TYPE_X509_PEM_FILE"
}
locals {
project_ids = [123456789012, 234567890123, 345678901234]
}
# Allow every other project access to GCE VM images in a central project
resource "google_project_iam_member" "image_user" {
for_each = local.project_ids
project = "tools-275721"
role = "roles/compute.imageUser"
locals {
project_ids = [123456789012, 234567890123, 345678901234]
bucket_region = "us"
bucket_project = "something-123456"
bucket_name = "${local.bucket_region}.artifacts.${local.bucket_project}.appspot.com"
}
# Allow Cloud Build in every other project access to GCR images hosted in the central project
resource "google_storage_bucket_iam_member" "container_registry" {
for_each = var.project_ids