Skip to content
Hunter edited this page Jan 18, 2023 · 2 revisions

Logs

aws logs create-export-task \
  --profile my-app-1 \
  --task-name "mylog-stdout-2018-12-10" \
  --log-group-name "/logs/mylog/stdout" \
  --from 1544400000000 \
  --to 1544486399999 \
  --destination "my-bucket" \
  --destination-prefix "myapp/api_logs"

Lambda

aws lambda update-function-code
  --function-name {account-id}:my_function_1
  --zip-file fileb://my_function_1.zip

EC2

aws --profile vpn ec2 stop-instances --instance-ids i-0ae6ed95 --region ap-northeast-1

S3

  • AWS S3 cli 的 cp 沒有 copy folder 的概念,會把來源打散
  • 相當於 linux cp somewhere/folder/* ./ -r
aws --profile mybucket s3 cp s3://mybucket/folder ./ --recursive
download: s3://mybucket/folder/sub-folder/a.png to sub-folder/a.png
download: s3://mybucket/folder/sub-folder/b.png to sub-folder/b.png
  • 反之也是
aws --profile mybucket s3 cp sub-folder s3://mybucket/folder --recursive
upload: sub-folder/a.png to s3://mybucket/folder/a.zip
  • sync 則以 folder 為主
aws s3 sync /data/backup-folder s3://mybucket/backup-folder
  • 以月份和主機名區分目錄的備份方式
export AWS_ACCESS_KEY_ID=...;
export AWS_SECRET_ACCESS_KEY=...;
YESTERDAY_YM=`date -d yesterday +%Y.%m`
/usr/bin/aws s3 sync \
    /path/to/mylogs/ \
    s3://mybucket/mylogs/${YESTERDAY_YM}/${HOSTNAME}/ \
    --exclude "*" \
    --include "prefix.log.${YESTERDAY_YM}*";
  • 複製特定檔案
aws --profile myprofile s3 cp dist/files/ s3://mybucket/files/ \
  --recursive --acl public-read --exclude "*" --include "*.jpg" \
  --content-type "image/jpg" --content-encoding "image/jpg" --cache-control "max-age=864000"
  • 透過 PresignedURL 上傳 S3
curl -XPUT -H "Content-Type: image/jpg" -T photo.jpg "PresignedURL"
curl -XPUT -H "x-amz-acl:public-read" -T file.bin "PresignedURL"
  • 模擬建立目錄
aws s3api put-object --bucket mybucket --key path/to/new/folder/

CloudFront

aws --profile myprofile cloudfront create-invalidation \
  --distribution-id EXXXXXXXXXXX \
  --invalidation-batch \
  '{"Paths":{"Quantity":2,"Items":["/index.html","/data.json"]},"CallerReference":"my-invalidation-'`date +%Y%m%d%H%M%S`'"}'
aws --profile myprofile cloudfront get-invalidation --distribution-id EXXXXXXXXXXX --id I7XXXXXXXXX

DynamoDB

  • Desc & Update
aws dynamodb describe-table --region us-west-2 --table-name My_Log
aws dynamodb update-table --region us-west-2 --table-name My_Log --provisioned-throughput ReadCapacityUnits=100,WriteCapacityUnits=20
  • Query
aws dynamodb query \
  --region=ap-southeast-1 \
  --table-name=GeoList \
  --index-name=geohash-index \
  --key-condition-expression="geohash=:h" \
  --expression-attribute-values='{":h":{"S":"wsmf88"}}'
aws dynamodb scan \
  --region=us-west-2 \
  --table-name=vpnServers \
  --filter-expression="instance_type<>:supprt_type" \
  --expression-attribute-values='{":supprt_type":{"S":"support"}}'
  • PutItem
aws --profile $PROFILE \
    dynamodb put-item \
    --table-name $TABLE_NAME \
    --item '{"order_id":{"S":"'$ORDER_ID'"},"timestamp":{"N":"1674007200"},"investment":{"N":"110"},"total_balance":{"N":"110.25"}}'
  • BatchWrite
aws --profile $PROFILE \
    dynamodb batch-write-item \
    --request-items '{"'$TABLE_NAME'":
    [
        {"PutRequest":{"Item":{"order_id":{"S":"'$ORDER_ID'"},"timestamp":{"N":"1672797600"},"investment":{"N":"500"},"total_balance":{"N":"500"}}}},
        {"PutRequest":{"Item":{"order_id":{"S":"'$ORDER_ID'"},"timestamp":{"N":"1672801200"},"investment":{"N":"500"},"total_balance":{"N":"500"}}}}
    ]}'

CloudWatch

  • ReadThrottleEvents
aws dynamodb list-tables --output text | awk '{print $2}' |
while read line
	do aws cloudwatch put-metric-alarm --alarm-name $line"-ReadThrottleEvents"  \
	--alarm-description "ReadThrottleEvents exceeds 100 in a minute" --namespace AWS/DynamoDB \
	--metric-name ReadThrottleEvents --dimensions Name=TableName,Value=$line --statistic Sum --threshold 100 \
	--comparison-operator GreaterThanOrEqualToThreshold --period 60 --evaluation-periods 1 \
	--alarm-actions arn:aws:sns:ap-northeast-1:123456789012:dynamodb
done
  • WriteThrottleEvents
aws dynamodb list-tables --output text | awk '{print $2}' |
while read line
	do aws cloudwatch put-metric-alarm --alarm-name $line"-WriteThrottleEvents"  \
	--alarm-description "WriteThrottleEvents exceeds 100 in a minute" --namespace AWS/DynamoDB \
	--metric-name WriteThrottleEvents --dimensions Name=TableName,Value=$line --statistic Sum --threshold 100 \
	--comparison-operator GreaterThanOrEqualToThreshold --period 60 --evaluation-periods 1 \
	--alarm-actions arn:aws:sns:ap-northeast-1:123456789012:dynamodb
done
Clone this wiki locally