# GROUP4 - AWS Storage lab **Snapshot hands on lab** *Run the following commands with Powershell (aws cli installation required)* ![](https://i.imgur.com/ZzajvRV.png) ## Init variable ``` $keyName = "tuannd67" $sgGroupId = "sg-05306f6795fe2025b" $subnetId = "subnet-061a0f7fde133b9b8" $Server1Name = "group4-svr1" $Server2Name = "group4-svr2" ``` ## Create EC2 ### Create server1 with nginx (userdata) ``` $Server1ID = aws ec2 run-instances ` --image-id ami-0574da719dca65348 ` --instance-type t2.micro ` --key-name $keyName ` --security-group-ids $sgGroupId ` --subnet-id $subnetId ` --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$Server1Name}]" ` --associate-public-ip-address ` --query "Instances[0].InstanceId" ` --user-data file://server1_user_data.txt ``` #### server1_user_data.txt ``` #!/bin/bash sudo apt-get update sudo apt-get upgrade -y sudo apt-get install nginx -y sudo systemctl enable nginx sudo systemctl start nginx sudo echo "server 1" > /var/www/html/index.html ``` ### Create server 2 without nginx ``` $Server2ID = aws ec2 run-instances ` --image-id ami-0574da719dca65348 ` --instance-type t2.micro ` --key-name $keyName ` --security-group-ids $sgGroupId ` --subnet-id $subnetId ` --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$Server2Name}]" ` --associate-public-ip-address ` --query "Instances[0].InstanceId" ``` ### Get volume ID of server 1 ``` $Server1DiskID = aws ec2 describe-volumes ` --filters Name=attachment.instance-id,Values=$Server1ID ` --query 'Volumes[0].VolumeId' ``` ### Get Volume ID of server 2 ``` $Server2DiskID = aws ec2 describe-volumes ` --filters Name=attachment.instance-id,Values=$Server2ID ` --query 'Volumes[0].VolumeId' ``` ### Stop Server2 to detach volume ``` aws ec2 stop-instances --instance-ids $Server2ID ``` ### Detach volume of Server2 ``` aws ec2 detach-volume --volume-id $Server2DiskID ``` ## Create snapshot for server1's volume ``` $Server1DiskSnapshotID = aws ec2 create-snapshot ` --volume-id $Server1DiskID ` --description "This is snapshot of $Server1Name" ` --tag-specifications "ResourceType=snapshot,Tags=[{Key=Name,Value=$Server1Name`-snapshot`}]" ` --query 'SnapshotId' ``` ### Get status snapshot waiting.... ``` aws ec2 describe-snapshots ` --snapshot-ids $Server1DiskSnapshotID ` --query "Snapshots[0].State" ``` ## Create new volume from snapshot ``` $NewDiskId = aws ec2 create-volume ` --volume-type gp2 --size 8 ` --availability-zone us-east-1a ` --snapshot-id $Server1DiskSnapshotID ` --tag-specifications "ResourceType=volume,Tags=[{Key=Name,Value=group4-new-disk}]" ` --query VolumeId ``` ## Attach new volume to Server 2 ``` aws ec2 attach-volume ` --volume-id $NewDiskId ` --instance-id $Server2ID ` --device "/dev/sda1" ``` ## start server 2 ``` aws ec2 start-instances --instance-ids $Server2ID ``` ## Testing ### Get Ip address of server ``` $Server1Ip = aws ec2 describe-instances ` --instance-ids $Server1ID ` --query "Reservations[*].Instances[].PublicIpAddress | [0]" $Server2Ip = aws ec2 describe-instances ` --instance-ids $Server2ID ` --query "Reservations[*].Instances[].PublicIpAddress | [0]" ``` ### Open ip of server2 in browser ``` Start-Process -FilePath Chrome -ArgumentList "--new-tab http://$Server1Ip" Start-Process -FilePath Chrome -ArgumentList "--new-tab http://$Server2Ip" ``` ### SSH ``` ssh -i $keyName'.pem' ubuntu@$Server1Ip ssh -i $keyName'.pem' ubuntu@$Server2Ip ``` # Clean up ``` aws ec2 terminate-instances --instance-ids $Server1ID aws ec2 terminate-instances --instance-ids $Server2ID aws ec2 delete-volume --volume-id $NewDiskId aws ec2 delete-snapshot --snapshot-id $Server1DiskSnapshotID ```