Skip to content

Instantly share code, notes, and snippets.

@pwillis-els
pwillis-els / how-to-switch-roles-aws-adfs.md
Last active June 23, 2023 07:05
How to set up AWS CLI profiles to switch between roles while using Federated SAML authentication

Using profiles, assume-role, and Federated SAML authentication with AWS CLI

Let's say you use a Federated authentication method for AWS (like ADFS), and by default you have access to multiple roles and accounts. You want to be able to easily switch between accounts, roles, and even assume a second role after assuming a first one. The following guide explains how this works using [aws-adfs][1] and the [AWS CLI][2].

Background info about profiles

Profiles are how AWS CLI configures the settings for individual credentials, and allows you to switch between them. You can specify a profile either by passing the --profile NAME option to AWS CLI, or with an environment variable AWS_PROFILE=name.

SELECT (1-filter(uniqueCount(uuid), WHERE category='Crash') / uniqueCount(uuid)) * 100 as Crash Free FROM MobileSession, MobileCrash WHERE crashFingerprint NOT IN () where appName like 'PROD%' SINCE 1 week AGO LIMIT 1000

SELECT average(cpuPercent) AS '' FROM SystemSample FACET entityId SINCE 1 hour ago

SELECT average(databaseCallCount) FROM Transaction SINCE last month until this month COMPARE WITH 1 month ago WITH TIMEZONE 'Europe/London'

SELECT average(databaseDuration) FROM Transaction SINCE last month until this month COMPARE WITH 1 month ago WITH TIMEZONE 'Europe/London'

SELECT average(duration+backendDuration) AS 'Total', average(duration) AS 'Frontend', average(backendDuration) AS 'Backend' FROM PageView WHERE countryCode = 'NZ' AND appName = 'appname' SINCE 1 day ago

@pwillis-els
pwillis-els / install_newrelic_agent.sh
Created September 23, 2019 11:08
Install NewRelic agent on EC2
#!/bin/bash
if [ ! -n "$LICENSE_KEY" ] ; then
read -p "NewRelic license key: " LICENSE_KEY
fi
if [ -z "$LICENSE_KEY" ] ; then
echo "Error: failed to read license key" ; exit 1
fi
echo "license_key: $LICENSE_KEY" | sudo tee -a /etc/newrelic-infra.yml
if grep "Amazon Linux 2" /etc/os-release ; then
sudo curl -s -o /etc/yum.repos.d/newrelic-infra.repo https://download.newrelic.com/infrastructure_agent/linux/yum/el/7/x86_64/newrelic-infra.repo
@pwillis-els
pwillis-els / Fix-dot-jenkins-run-time-problem.md
Created September 20, 2019 14:56
Fix Jenkins to stop running from a ".jenkins" hidden directory

Fixing Jenkins to stop adding /.jenkins to your JENKINS_HOME path

When running Jenkins, you may try to set the JENKINS_HOME path that Jenkins uses at start-up, only to find the following output message:

Jenkins home directory: /var/jenkins_home/.jenkins found at: $user.home/.jenkins

You're trying to get Jenkins to read its files from /var/jenkins_home, but it keeps tacking on a /.jenkins hidden folder, and you can't get it to stop.

You've googled around and read all the documentation, but none of it describes how to fix this.

@pwillis-els
pwillis-els / get_keys_from_inst_metadata.sh
Created July 22, 2019 21:24
Get IAM access key from AWS EC2 instance profile metadata
#!/bin/sh
set -e
AWS_INST_PROFILE=$(curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/ | tail)
curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/${AWS_INST_PROFILE} \
| python -c 'import sys,json;j=json.load(sys.stdin);print("AWS_ACCESS_KEY_ID=%s\nAWS_SECRET_ACCESS_KEY=%s\n" % (j["AccessKeyId"],j["SecretAccessKey"]))'
@pwillis-els
pwillis-els / example.sh
Last active March 11, 2019 16:36
Scrape and print the variables used in a shell script
cat foo.sh | perl -lne 'print $1 if /\$\{?([a-z0-9_]+)\}?/i' | sort -u