Skip to content

Instantly share code, notes, and snippets.

View tfentonz's full-sized avatar

Tom Fenton tfentonz

  • MAGIQ Software
  • Christchurch, New Zealand
  • 16:05 (UTC +12:00)
  • LinkedIn in/tfentonz
View GitHub Profile
@tfentonz
tfentonz / aws-lightsail.md
Last active September 4, 2017 23:14
AWS Lightsail CLI

AWS Lightsail

aws lightsail get-blueprints --query 'blueprints[?group==`nginx`]'
[
    {
        "blueprintId": "nginx_1_12_0_2",
        "name": "Nginx",
        "group": "nginx",
 "type": "app",
@tfentonz
tfentonz / fix-pem.sh
Last active September 14, 2017 03:10
Fix Private Key
#!/bin/bash
#
# Replace spaces with @'s
# Replace @'s in BEGIN with spaces
# Replace @'s in END with spaces
# Replace @'s with newlines
sed -i '' -e 's/ /@/g
s/-----BEGIN@RSA@PRIVATE@KEY-----/-----BEGIN RSA PRIVATE KEY-----/
s/-----END@RSA@PRIVATE@KEY-----/-----END RSA PRIVATE KEY-----/
s/@/\'$'\n/g' $1
@tfentonz
tfentonz / describe-rules.md
Last active June 13, 2023 10:44
AWS CLI command to list ELB listener rule priorities

Get load balancer ARN

arn=$(aws elbv2 describe-load-balancers --names <value> --query 'LoadBalancers[].[LoadBalancerArn]' --output text)

Get listeners

aws elbv2 describe-listeners --load-balancer-arn "$arn" --query 'Listeners[].{ListenerArn:ListenerArn,Protocol:Protocol,Port:Port}'

[
@tfentonz
tfentonz / s3-last-modified
Created November 5, 2018 08:04
List S3 objects last modified after a specified date
aws s3api list-objects --bucket A_BUCKET --prefix A_PREFIX --query 'Contents[?LastModified>=`2012-03-04`][].{Key: Key, Size: Size, LastModified: LastModified}'
@tfentonz
tfentonz / list-stacks.sh
Created May 22, 2019 03:55
AWS CLI CloudFormation list stacks with filter and sort
aws cloudformation list-stacks --query 'StackSummaries[?starts_with(StackName, `production`)].{StackName:StackName,StackStatus:StackStatus} | sort_by(@, &StackName)'
@tfentonz
tfentonz / athena_date_time.sql
Created June 13, 2019 21:53
Athena Date and Time Functions
-- [SQL Queries, Functions, and Operators](https://docs.aws.amazon.com/athena/latest/ug/functions-operators-reference-section.html)
SELECT
from_iso8601_timestamp(time) as new_time,
date_format(from_iso8601_timestamp(time), '%Y') AS year,
hour(from_iso8601_timestamp(time)) AS hour,
from_iso8601_timestamp(time) at time zone 'Pacific/Auckland' as nzst_time,
hour(from_iso8601_timestamp(time) at time zone 'Pacific/Auckland') AS nzst_hour,
minute(from_iso8601_timestamp(time) at time zone 'Pacific/Auckland') AS nzst_minute,
*
@tfentonz
tfentonz / awssessiontoken
Last active November 28, 2023 09:17
Set environment variables to use MFA token with AWS CLI
#!/bin/bash
#
# https://aws.amazon.com/premiumsupport/knowledge-center/authenticate-mfa-cli/
#
# Usage: source ~/bin/awssessiontoken
# arn:aws:iam::12345689012:mfa/ExampleMFADevice
mfa_arn=$(aws iam list-mfa-devices --query 'MFADevices[].SerialNumber' --output text)
echo "MFA ARN: $mfa_arn"
echo -n "Enter MFA Code: "
@tfentonz
tfentonz / rds-db-snapshots.sh
Created October 30, 2019 20:21
RDS DB Snapshots
aws rds describe-db-snapshots \
--no-include-shared \
--no-include-public \
--query 'DBSnapshots[?SnapshotType!=`automated`].[
SnapshotType,
DBSnapshotIdentifier,
DBInstanceIdentifier,
SnapshotCreateTime,
Engine,
EngineVersion,
@tfentonz
tfentonz / aws-s3-metadata.sh
Last active July 23, 2020 01:59
Update S3 object metadata Content-Type and Cache-Control
aws s3 cp s3://BUCKET_NAME/PREFIX/ s3://BUCKET_NAME/PREFIX/ \
--metadata-directive 'REPLACE' \
--content-type 'image/png' \
--cache-control 'public, max-age=2592000, immutable' \
--recursive \
--exclude '*' \
--include '*.png'
@tfentonz
tfentonz / gist:b2c0d98e6e8537f009bf5e458f878a27
Created June 16, 2021 00:53
AWS CLI query for EC2 instances Customer, Name, InstanceId, InstanceType
aws ec2 describe-instances \
--query 'Reservations[].Instances[].[Tags[?Key==`Customer`]|[0].Value,Tags[?Key==`Name`]|[0].Value,InstanceId,InstanceType]' \
--output text