Cập nhật cấu hình Workspace

Cập nhật cấu hình EC2 Workspace

Vì chúng ta đã gắn IAM Role trực tiếp vào máy ảo EC2, hệ thống sẽ tự động sử dụng quyền hạn của Role này. Chúng ta chỉ cần thiết lập lại các biến môi trường cần thiết.

  1. Để đảm bảo không có thông tin chứng thực cũ nào bị lưu lại trong máy ảo, chúng ta sẽ xóa file credentials (nếu có) bằng câu lệnh dưới đây.
rm -vf ${HOME}/.aws/credentials

Update configure role

  1. Tiếp theo chúng ta sẽ cấu hình AWS CLI sử dụng Region hiện tại.
export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
TOKEN=$(curl -sX PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
export AWS_REGION=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region')
export AZS=($(aws ec2 describe-availability-zones --query 'AvailabilityZones[].ZoneName' --output text --region $AWS_REGION))

Update configure role

  1. Kiểm tra xem AWS_REGION đã được thiết lập đúng Region hiện tại chưa.
test -n "$AWS_REGION" && echo AWS_REGION is "$AWS_REGION" || echo AWS_REGION is not set

Update configure role

  1. Tiếp theo chúng ta sẽ lưu các thông tin cấu hình vào bash_profile
echo "export ACCOUNT_ID=${ACCOUNT_ID}" | tee -a ~/.bash_profile
echo "export AWS_REGION=${AWS_REGION}" | tee -a ~/.bash_profile
echo "export AZS=(${AZS[@]})" | tee -a ~/.bash_profile
aws configure set default.region ${AWS_REGION}
aws configure get default.region

Update configure role

  1. Kiểm tra IAM Role
  • Chúng ta sẽ sử dụng câu lệnh để kiểm tra EC2 Workspace đang sử dụng IAM Role có chính xác không.
aws sts get-caller-identity --query Arn | grep eksworkshop-admin -q && echo "IAM role valid" || echo "IAM role NOT valid"

Update configure role