- Using credentials with AWS CLI involves a file at `~/.aws/credentials`, with the following example format: ``` [] aws_access_key_id = aws_secret_access_key = aws_session_token = ``` - Add `--profile demo` to use the above with AWS CLI commands - List accounts belonging to organization - `aws organizations list-accounts` - Get information about IAM role - `aws iam get-role --role-name --profile ` - List details on instances - ` aws ec2 describe-instances --region= --profile ` - List details on container repositories - `aws ecr describe-repositories --region= --profile gretsch1` - Get user information - `aws iam get-user --profile ` - `aws iam list-attached-user-policies --user-name= --profile ` - Get information on policy - ` aws iam get-policy --policy-arn mxrads-self-manage --profile kevin` - Version - ` aws iam iam get-policy --policy-arn --profile ` - Get Content - ` aws iam iam get-policy-version --policy-arn --version --profile ` - List users and groups affiliated with default Administrator policy - `aws iam list-entities-for-policy --policy-arn arn:aws:iam::aws:policy/AdministratorAccess` - List current access keys for user (there can only be 2, so anything less allows you to add one) - `aws iam list-access-keys --user b.daniella | jq ".AccessKeyMetadata[].AccessKeyId"` - Create access key for user - `aws iam create-access-key --user b.daniella` - Change role policy - `aws iam update-assume-role-policy --role-name lambda-dmp-sync --policy-document file://new_policy.json` - Find roles capable of `assume-role` calls for `lambda.amazonaws.com` - `aws iam list-roles | jq -r '.Roles[] | .RoleName + ", " + .AssumeRolePolicyDocument.Statement[].Principal.Service' | grep "lambda.amazonaws.com"` - Check IAM policies for role - `aws iam list-attached-role-policies --role --profile ` - Look for roles with high permissions like `IAMFullAccess` and which lack write permissions to CloudWatch - Inspect security groups - `aws ec2 describe-security-groups --group-ids ` - Assume role - `aws sts assume-role --role-arn arn:aws:iam::886371554408:user/lambda-dmp-sync --role-session-name AWSCLI-Session --duration-seconds 43200` - List existing lambda functions - `aws iam lambda list-functions -region=` - Get information on Lambda function - `aws lambda get-function --function-name --region --profile ` - Get information on Kubernetes cluster - `aws eks describe-cluster --name --profile --region=` - Get information from Resource Groups Tagging API - `aws resourcegroupstaggingapi get-resources --region --profile ` - List secrets - `aws secretsmanager list-secrets --region --profile ` - Download secret - `aws secretsmanager get-secret-value --secret-id '' --region=eu-west-1 --profile it-role | jq -r .SecretString | base64 -d` - List buckets accessible with these credentials/this role: - `aws s3api listbuckets --profile ` - List buckets and show bucket names only - `aws s3api list-buckets --profile --query "Buckets[].Name"` - Sync bucket - mounted locally - `aws s3 sync s3:// ` - With another bucket - `aws s3 sync s3://source-bucket/ s3://destination-bucket` - List keys inside a single bucket 1. `aws s3api list-objects-v2 --profile --bucket > list_objects_dl.txt` 2. `grep '"Key"' list_objects_dl | sed 's/[",]//g' > list_keys_dl.txt` - Check for S3 bucket logging - `aws s3api get-bucket-logging --profile --bucket ` - Check bucket policy - `aws s3api get-bucket-policy --bucket ` - Get account ID - `aws sts get-caller-identity --profile ` - Create a new bucket: - `aws s3api create-bucket --bucket --region= --create-bucket-configuration LocationConstraint=` - Upload file to bucket: - ` aws s3api put-object --bucket --key --body ` - Change file permissions in bucket: - `aws s3api put-bucket-policy --bucket --policy file://` - Exchange service account token for IAM keys (only for proper service account tokens with OpenID info in AWS) 1. `AWS_ROLE_ARN=""` - e.g. `AWS_ROLE_ARN="arn:aws:iam::886477354405:role/api-core.ec2"` 2. `TOKEN =""` 3. `aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name sessionID --web-identity-token $TOKEN --duration-seconds 43200` - Exchange IAM key for Kubernetes token - `aws eks get-token --cluster-name --profile ` - Create kubectl config - `aws eks update-kubeconfig --name --profile ` - Get all instances that match a specific tag - `while read p; do instanceID=$(aws ec2 describe-instances --filter "Name=tag:Name,Values=*$p*" --query 'Reservations[0].Instances[].InstanceId' --region=eu-west-1 --output=text; echo $instanceID > list_ids.txt; done $p.txt done` - Get launch configurations - `aws autoscaling describe-launch-configurations` - `aws ec2 describe-launch-templates` - Start instance with user data script that runs on startup: - `aws ec2 run-instances --image-id ami- --count 1 --instance-type m3.medium --iam-instance-profile --subnet-id subnet- --security-group-ids sg- --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=spark-worker-5739ecea19a4}]' --user-data file:// --profile --region ` - Redshift - Get info on clusters - `aws redshift describe-clusters` - Get credentials for cluster - `aws get-cluster-credentials --db-user root --db-name --cluster-identifier --duration-seconds 3600` - Check monitoring - Access Analyzer - `aws accessanalyzer list-analyzers --region=` - CloudTrail - `aws cloudtrail describe-trails --region=` - GuardDuty - `aws guardduty list-detectors --region=` - Extract info from CloudTrail - `aws logs describe-log-groups --region= --profile ` - Filter for activity referring to a specific account - `aws logs filter-log-events --log-group-name "CloudTrail/DefaultLogGroup" --filter-pattern "" --max-items 10 --profile --region | jq ".events[].message" | sed 's/\\//g' `