AWS
Destroy stuff from S3
Section titled “Destroy stuff from S3”# Delete bucketsbuckets=( "bucket1" "bucket2")for bucket in "${buckets[@]}";do bucketname=$(aws s3api list-buckets | jq -r '.Buckets[].Name | select(contains("'"$bucket"'"))') [[ -z "$bucketname" ]] && echo "Couldn't find bucket: $bucket" && continue echo "Emptying bucket: $bucketname" aws s3 rm "s3://$bucketname" --recursive --only-show-errors echo "Deleting bucket: $bucketname" aws s3api delete-bucket --bucket "$bucketname"done
# Empty bucket with some string in the namebucketname=$(aws s3api list-buckets | jq -r '.Buckets[].Name | select(contains("'"something"'"))')if [ -n "$bucketname" ]; then echo "Emptying bucket: $bucketname" aws s3 rm "s3://$bucketname" --recursive --only-show-errorsfi
CodeBuild
Section titled “CodeBuild”# Start buildaws codebuild start-build --project-name name --source-version "main"
# Get status of latest buildaws codebuild batch-get-builds --ids $(aws codebuild list-builds-for-project --project-name name --query "ids[0]" --output text) | jq -r '.builds[] | {Name: .id, Status: .buildStatus}'
# Wait while build is in progresswhile [[ $(aws codebuild batch-get-builds --ids $(aws codebuild list-builds-for-project --project-name name --query "ids[0]" --output text) | jq -r '.builds[].buildStatus') = "IN_PROGRESS" ]]; do echo -n "CodeBuild in progress, $(date)" for i in {1..10}; do sleep 6 echo -n "." done echodoneecho "❤️ CodeBuild completed ❤️"
CloudFormation DRIFT status
Section titled “CloudFormation DRIFT status”# Drifted stacksaws cloudformation list-stacks --stack-status-filter 'UPDATE_COMPLETE' --query 'StackSummaries[?DriftInformation.StackDriftStatus==`DRIFTED`].StackName'
# Stack drift detailsaws cloudformation describe-stack-resource-drifts --stack-name foobaz# Not IN_SYNCaws cloudformation describe-stack-resource-drifts --stack-name foobaz --query 'StackResourceDrifts[?StackResourceDriftStatus!=`IN_SYNC`]'aws cloudformation describe-stack-resource-drifts --stack-name foobaz --query 'StackResourceDrifts[?StackResourceDriftStatus!=`IN_SYNC`]' --output yaml # json,text,table,yaml
for drifted_stack in $(aws cloudformation list-stacks --stack-status-filter 'UPDATE_COMPLETE' --query 'StackSummaries[?DriftInformation.StackDriftStatus==`DRIFTED`].StackName' --output text);do echo "###" echo "# $drifted_stack" echo "###" aws cloudformation describe-stack-resource-drifts --stack-name $drifted_stack --query 'StackResourceDrifts[?StackResourceDriftStatus!=`IN_SYNC`]' --output yamldone > drifted_stacks.yaml
Give access to sub
Section titled “Give access to sub”Create IAM role
Section titled “Create IAM role”OTHER_AWS_ACCOUNT=xxxxNEW_ROLE_NAME=test-accessaws iam create-role --role-name "$NEW_ROLE_NAME" --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":"arn:aws:iam::'$OTHER_AWS_ACCOUNT':root"},"Action":"sts:AssumeRole","Condition":{}}]}'aws iam attach-role-policy --role-name "$NEW_ROLE_NAME" --policy-arn "arn:aws:iam::aws:policy/AdministratorAccess"# New role Arn:aws iam get-role --role-name $NEW_ROLE_NAME --query "Role.Arn" --output text
Assume role CLI
Section titled “Assume role CLI”Set env vars (don’t use this)
Section titled “Set env vars (don’t use this)”ENV.use foobar-name # Set env variablesaws sts get-caller-identityexport AWS_ROLE_ARN=arn:aws:iam::xxxx:role/foobar-accessexport $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \ $(aws sts assume-role \ --role-arn $AWS_ROLE_ARN \ --role-session-name foobar \ --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \ --output text))aws sts get-caller-identityaws s3 ls
Awsume with role
Section titled “Awsume with role”ENV.use foobar-name # Set env variablesaws sts get-caller-identityawsume --role-arn arn:aws:iam::xxxx:role/foobar-accessaws sts get-caller-identityaws s3 ls
Create profile
Section titled “Create profile”[foobar-with-foobaz]source_profile = foobar-namerole_arn = arn:aws:iam::xxxx:role/foobar-access
Awsume with profile
Section titled “Awsume with profile”aws sts get-caller-identityawsume foobar-with-foobazaws sts get-caller-identityaws s3 ls
Get AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY when using profile
Section titled “Get AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY when using profile”aws configure get aws_access_key_idaws configure get aws_secret_access_key
ECR / Helm
Section titled “ECR / Helm”# Login:aws ecr get-login-password \ --region us-east-1 | helm registry login \ --username AWS \ --password-stdin xxxx.dkr.ecr.us-east-1.amazonaws.com
# Pullhelm pull oci://xxxx.dkr.ecr.us-east-1.amazonaws.com/release/charts/some-chart --version 1.2.3# creates file some-chart-1.2.3.tgz
# Pushhelm push some-chart-1.2.3.tgz oci://xxxx.dkr.ecr.us-east-1.amazonaws.com/release/charts
Lambda
Section titled “Lambda”# Get big Lambdasaws lambda list-functions --query "Functions[?CodeSize>to_number('1000000')].{CodeSize:CodeSize, FunctionName:FunctionName}" --output table