Skip to content

Instantly share code, notes, and snippets.

View tfentonz's full-sized avatar

Tom Fenton tfentonz

  • MAGIQ Software
  • Christchurch, New Zealand
  • 03:25 - 12h ahead
  • LinkedIn in/tfentonz
View GitHub Profile
@tfentonz
tfentonz / route53-com-au.sh
Created August 25, 2021 22:13
List Route 53 Domains ending with .com.au
aws route53domains list-domains \
--query 'Domains[?ends_with(DomainName, `.com.au`)].{DomainName:DomainName}' \
--output text \
--profile us-east-1
@tfentonz
tfentonz / stack-parameters.sh
Created July 23, 2021 01:40
AWS CLI CloudFormation stack parameters
aws cloudformation describe-stacks \
--stack-name <STACK_NAME> \
--query 'Stacks[].Parameters[].{Key:ParameterKey,Value:ParameterValue}' \
--output table
@tfentonz
tfentonz / ec2-name-tag.sh
Last active June 22, 2021 22:10
Get EC2 Name tag value
#!/bin/bash
instance_id=$(curl -s 169.254.169.254/latest/meta-data/instance-id)
name=$(aws ec2 describe-tags \
--filters \
"Name=resource-type,Values=instance" \
"Name=resource-id,Values=$instance_id" \
"Name=key,Values=Name" \
--query "Tags[].Value" \
--output text)
echo "$name"
@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
@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 / 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 / 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 / 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 / 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 / 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}'