├── s3 ├── multipart-upload.txt ├── list-versioned-bucket ├── other-commands.MD └── local-folder-automated-backup-S3 ├── awscli.jpg ├── Bash+CLI ├── .DS_Store ├── EC2 │ └── runInstanceDefaultVPC └── ELB │ ├── elb.sh │ └── README.md ├── ec2 ├── list-ubuntu-images-with-description.txt ├── EC2-CLI-commands.MD └── ec2-create-snapshots-and-restore.txt ├── Create an AMI ├── Create an Image-AMI and launch EC2.txt └── userdata.txt ├── Readme.MD ├── SNS └── Readme.MD ├── lambda ├── stopEC2.py └── Lambda CLI instructions.MD ├── README.md ├── configure └── environment-vars.MD ├── rekognition └── Readme.MD ├── cloudwatch └── cloudwatch-cli-useful-commands.MD └── vpc └── create-a-vpc-with-subnets-RT-GATEWAYS-etc.MD /s3/multipart-upload.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /awscli.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravsau/AWS-CLI-Commands/HEAD/awscli.jpg -------------------------------------------------------------------------------- /Bash+CLI/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravsau/AWS-CLI-Commands/HEAD/Bash+CLI/.DS_Store -------------------------------------------------------------------------------- /Bash+CLI/EC2/runInstanceDefaultVPC: -------------------------------------------------------------------------------- 1 | aws ec2 run-instances --image-id ami-55ef662f --instance-type t2.micro --key-name MyKeyPair1 2 | -------------------------------------------------------------------------------- /ec2/list-ubuntu-images-with-description.txt: -------------------------------------------------------------------------------- 1 | aws ec2 describe-images --filters 'Name=name,Values=*Ubuntu*' --query 'Images[*].[ImageId , Description]' 2 | -------------------------------------------------------------------------------- /Create an AMI/Create an Image-AMI and launch EC2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravsau/AWS-CLI-Commands/HEAD/Create an AMI/Create an Image-AMI and launch EC2.txt -------------------------------------------------------------------------------- /s3/list-versioned-bucket: -------------------------------------------------------------------------------- 1 | # To List all the object versions in your bucket . s3api is installed with aws cli. 2 | 3 | aws s3api list-object-versions --bucket your-bucket-name 4 | -------------------------------------------------------------------------------- /s3/other-commands.MD: -------------------------------------------------------------------------------- 1 | ## Get bucket summary: Find the bucket size and no of objects 2 | 3 | ```console 4 | aws s3 ls --summarize --human-readable --recursive s3://yourbucket 5 | ``` 6 | -------------------------------------------------------------------------------- /Bash+CLI/ELB/elb.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | elblist=`aws elb describe-load-balancers --query LoadBalancerDescriptions[].LoadBalancerName --output text` 3 | 4 | aws elb describe-tags --load-balancer-name $elblist 5 | -------------------------------------------------------------------------------- /Readme.MD: -------------------------------------------------------------------------------- 1 | ## SNS use cases with the AWS CLI 2 | 3 | ### Send a quick SMS with the AWS CLI 4 | ```console 5 | aws sns publish --phone-number +12012022034 --message " This is bob. My phone's dead. I'll be home soon." 6 | ``` 7 | -------------------------------------------------------------------------------- /SNS/Readme.MD: -------------------------------------------------------------------------------- 1 | ## SNS use cases with the AWS CLI 2 | 3 | ### Send a quick SMS with the AWS CLI 4 | ```console 5 | aws sns publish --phone-number +12012022034 --message " This is bob. My phone's dead. I'll be home soon." 6 | ``` 7 | -------------------------------------------------------------------------------- /Bash+CLI/ELB/README.md: -------------------------------------------------------------------------------- 1 | This bash script will let you describe tags of all the ELB's that's on your current region. 2 | 3 | Make sure you change the permission of the bash script to make it executable. 4 | 5 | You can use the command $ chmod +x elb.sh 6 | -------------------------------------------------------------------------------- /Create an AMI/userdata.txt: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | yum update -y 3 | yum install -y httpd 4 | service httpd start 5 | chkconfig httpd on 6 | groupadd www 7 | usermod -a -G www ec2-user 8 | chown -R root:www /var/www 9 | chmod 2775 /var/www 10 | find /var/www -type d -exec chmod 2775 {} + 11 | find /var/www -type f -exec chmod 0664 {} + 12 | echo "

Hello World

" > /var/www/html/index.html 13 | -------------------------------------------------------------------------------- /lambda/stopEC2.py: -------------------------------------------------------------------------------- 1 | 2 | import boto3 3 | 4 | client=boto3.client('ec2') 5 | 6 | def lambda_handler(event, context): 7 | 8 | response=client.describe_instances() 9 | 10 | 11 | for reservation in response["Reservations"]: 12 | 13 | for instance in reservation["Instances"]: 14 | 15 | print(instance["InstanceId"] + "stopping") 16 | 17 | id=[instance["InstanceId"]] 18 | 19 | client.stop_instances(InstanceIds=id) 20 | 21 | 22 | 23 | 24 | return("Completed") 25 | -------------------------------------------------------------------------------- /lambda/Lambda CLI instructions.MD: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Create a Lambda function with the AWS CLI to stop EC2 Instances: 4 | 5 | 6 | ### Steps: 7 | 1. make a file with a python code (stop-ec2.py ). **Stop-ec2.py is included in the same folder as this file 8 | 2. zip -r lambda stop.py 9 | 3. Create a lambda function (handler should be filename.handlerfunctionname) 10 | 11 | 12 | 13 | ### Find the role to use: 14 | ``` 15 | aws iam list-roles 16 | ``` 17 | 18 | ### Create a lambda function 19 | ``` 20 | aws lambda create-function --function-name CLI-lambda-test --runtime python2.7 --role arn:aws:iam::771452637355:role/lambda_full --handler stop.lambda_handler --zip-file fileb://lambda.zip 21 | ``` 22 | 23 | ### Run 3 instances with this command that we will stop later: 24 | 25 | ``` 26 | aws ec2 run-instances --image-id ami-6057e21a --instance-type t2.micro --count 3 27 | ``` 28 | 29 | 30 | 31 | ### Invoke the function Asynchronously: 32 | 33 | ``` 34 | aws lambda invoke --invocation-type Event --function-name CLI-lambda-test output.txt 35 | ``` 36 | 37 | 38 | 39 | ### Alternately invoke synchronously with 40 | 41 | ``` 42 | aws lambda invoke --invocation-type RequestResponse --function-name CLI-lambda-test output.txt 43 | ``` 44 | 45 | 46 | If you need to pass certain values pass them like this : 47 | 48 | ``` 49 | --payload '{"key1":"value1", "key2":"value2", "key3":"value3"}' 50 | ``` 51 | 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AWS-CLI-Commands 2 | This repo is intended to be used for mine Introduction to the AWS CLI course on Udemy. But this content will be useful to any AWS CLI user. 3 | 4 | ## Full Course Link and Coupon 5 | https://www.udemy.com/aws-cli-course/?couponCode=CERTIFIED 6 | --- 7 | 8 | The Bash+CLI folder has some powerful scripts that combine the features of Linux Bash and AWS CLI which let you do some cool things. 9 | 10 | #### First example: 11 | you could quickly provision an EC2 by calling an script like this: 12 | ``` 13 | ./runInstanceDefaultVPC 14 | 15 | ``` 16 | 17 | And you have an instance running!! 18 | Remember to make the file executable and to rename the key to that you own. 19 | 20 | #### Second Example: 21 | You can combine bash+CLI to describe tags for all your Elastic Load Balancers on a certain region. 22 | You can find this script under Bash+CLI/ELB/ folder or by [clicking here](https://github.com/ravsau/AWS-CLI-Commands/tree/master/Bash%2BCLI/ELB) 23 | 24 | 25 | 26 | ## My AWS CLI Videos on Youtube 27 | I have 10+ videos on youtube related to AWS CLI where I use commands in this repo to accomplish tasks like creating a VPC etc. Click the pic below to go to the playlist. 28 | 29 | [![](/awscli.jpg)](https://www.youtube.com/watch?v=v2GdoN4vCjY&list=PLQP5dDPLts67DnDIb2IvXd6qPFbLabDCO) 30 | 31 | 32 | ## Resources 33 | - AWS CLI Official Documentation: 34 | - https://docs.aws.amazon.com/cli/index.html 35 | - Install AWS CLI 36 | - https://docs.aws.amazon.com/cli/latest/userguide/installing.html 37 | - CLI Exit codes 38 | - https://docs.aws.amazon.com/cli/latest/topic/return-codes.html 39 | -------------------------------------------------------------------------------- /configure/environment-vars.MD: -------------------------------------------------------------------------------- 1 | You can use Environment vars to work with AWS CLI commands temporarily 2 | 3 | - things specified in CLI command > Environment variable > AWS credential file 4 | - For example if you have us-west-1 in AWS credential file, environment var as us-east-2 and pass --region us-east-1 in the aws cli command. the us-east-1 wins !! 5 | 6 | ## The AWS CLI supports the following environment variables: 7 | 8 | AWS_ACCESS_KEY_ID – Specifies an AWS access key associated with an IAM user or role. 9 | 10 | AWS_SECRET_ACCESS_KEY – Specifies the secret key associated with the access key. This is essentially the "password" for the access key. 11 | 12 | AWS_SESSION_TOKEN – Specifies the session token value that is required if you are using temporary security credentials. For more information, see the Output section of the assume-role command in the AWS CLI Command Reference. 13 | 14 | AWS_DEFAULT_REGION – Specifies the AWS Region to send the request to. 15 | 16 | AWS_DEFAULT_OUTPUT – Specifies the output format to use. 17 | 18 | AWS_DEFAULT_PROFILE – Specifies the name of the CLI profile with the credentials and options to use. This can be the name of a profile stored in a credentials or config file, or the value default to use the default profile. If you specify this environment variable, it overrides the behavior of using the profile named [default] in the configuration file. 19 | 20 | AWS_CA_BUNDLE – Specifies the path to a certificate bundle to use for HTTPS certificate validation. 21 | 22 | AWS_SHARED_CREDENTIALS_FILE – Specifies the location of the file that the AWS CLI uses to store access keys (the default is ~/.aws/credentials). 23 | 24 | AWS_CONFIG_FILE – Specifies the location of the file that the AWS CLI uses to store configuration profiles (the default is ~/.aws/config). 25 | 26 | 27 | # Reference 28 | https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html 29 | -------------------------------------------------------------------------------- /s3/local-folder-automated-backup-S3: -------------------------------------------------------------------------------- 1 | Prepared by Saurav Sharma | Sauravsharma.net 2 | 3 | S3 CLI commands 4 | 5 | 6 | Create a bucket: 7 | aws s3 mb s3://your-bucket 8 | 9 | Remove the bucket: 10 | aws s3 rb s3://your-bucket 11 | 12 | Remove a bucket with files in it: 13 | aws s3 rb s3://your-bucket --force 14 | 15 | Creating empty files in Linux : 16 | touch file{1..10} 17 | 18 | Copy a local file to a bucket: 19 | aws s3 cp file1 s3://your-bucket 20 | 21 | 22 | Copy Everything in local file to a bucket: 23 | aws s3 sync s3://your-bucket 24 | 25 | 26 | 27 | ### This next section works for Linux and Mac computers. We want to automatically backup local folder to S3 28 | 29 | crontab -e 30 | 31 | 32 | Create a script to save all files to S3 bucket every (x) minutes/hours/days or months 33 | 34 | * vim sync.sh 35 | 36 | * press i for insert 37 | 38 | * #!/bin/bash 39 | * aws s3 sync /home/ec2-user/Documents/ s3://copy-file-saurav 40 | 41 | * Press Esc 42 | * Type :wq! To save and exit 43 | * Chmod 500 sync.sh 44 | * The above command will make it executable 45 | 46 | 47 | 48 | On command line type this to create a cron job: 49 | * crontab -e 50 | 51 | * press I to insert 52 | 53 | * paste: */1 * * * * /home/ec2-user/sync.sh 54 | 55 | * The above line will sync files every minute. For every day use: 56 | 57 | * */1 * * * /home/ec2-user/sync.sh 58 | 59 | 60 | * save with :wq! 61 | 62 | 63 | 64 | To exclude a file with certain extension: 65 | 66 | aws s3 sync /home/ec2-user/Documents/ s3://copy-file-sauravÊ --exclude '*.rtf' --acl public-read --storage-class STANDARD_IA 67 | 68 | To exclude a file with public read permission and put in in s3- IA storage class: 69 | 70 | aws s3 sync /home/ec2-user/Documents/ s3://copy-file-saurav --acl public-read --storage-class STANDARD_IA 71 | 72 | 73 | 74 | 75 | 76 | For further reference visit: 77 | http://docs.aws.amazon.com/cli/latest/reference/s3/index.html 78 | -------------------------------------------------------------------------------- /ec2/EC2-CLI-commands.MD: -------------------------------------------------------------------------------- 1 | EC2 CLI 2 | 3 | More Reference:http://docs.aws.amazon.com/cli/latest/reference/ec2/index.html#cli-aws-ec2 4 | 5 | 6 | 7 | List Instances: 8 | 9 | ```console 10 | aws ec2 describe-instances 11 | aws ec2 describe-instances --output text 12 | aws ec2 describe-instances --output json 13 | aws ec2 describe-instances --output table 14 | 15 | ``` 16 | 17 | 18 | List only few things like a dashboard: 19 | 20 | ```console 21 | aws ec2 describe-instances --query 'Reservations[].Instances[].[Placement.AvailabilityZone, State.Name, InstanceId, InstanceType,VpcId,SubnetId,ImageId,Tags[?Key==`Name`].Value|[0]]' 22 | ``` 23 | 24 | 25 | 26 | Create a key pair and output that to a file: 27 | ```console 28 | aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem 29 | ``` 30 | 31 | 32 | *After that make sure to chmod 400 MyKeyPair file. 33 | 34 | 35 | Create an instance: 36 | ```console 37 | aws ec2 run-instances --image-id ami-8c1be5f6 --instance-type t2.micro --key-name MyKeyPair 38 | ``` 39 | 40 | 41 | This will create an instance in the default VPC. Specify the subnet name and the right security groups within that VPC if you want to be specific. 42 | 43 | 44 | Create an instance in an specific subnet: 45 | ```console 46 | aws ec2 run-instances --image-id ami-8c1be5f6 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-beb3eacc --subnet-id subnet-ed36c3c2 47 | ``` 48 | 49 | 50 | If needed change the ingress of a security group: 51 | ```console 52 | aws ec2 authorize-security-group-ingress --group-id sg-814134f2 --protocol tcp --port 22 --cidr 0.0.0.0/0 53 | ``` 54 | 55 | 56 | Terminate 1 or multiple ec2s at once : 57 | ```console 58 | aws ec2 terminate-instances --instance-ids i-0b20d7680fa0e6ba0 i-00251da28fa34ffd1 59 | ``` 60 | 61 | 62 | 63 | Describe and filter to show only windows Instance( use of filter switch ) 64 | 65 | ```console 66 | aws ec2 describe-instances --query 'Reservations[].Instances[].PublicIpAddress' --filters "Name=platform,Values=windows" 67 | 68 | ``` 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /rekognition/Readme.MD: -------------------------------------------------------------------------------- 1 | ## Amazon Recognition CLI Commands 2 | Amazon Recognition is a service that let's you perform various machine learning functions over an image/video with a simple API call. In this case, we will use the AWS CLI to work with this service 3 | 4 | 5 | ## Pre-Requisite 6 | 1) Upload some files to Amazon S3 bucket 7 | 2) Have CLI credentials set with S3 and Rekognition permissions 8 | 9 | ## Commands 10 | 11 | 1) Find Labels in an image ( I'll use image of NYC Time Square) 12 | ```console 13 | 14 | aws rekognition detect-labels --image "S3Object={Bucket=image-rekogniton-cli,Name=times-square.jpg}" 15 | ``` 16 | 17 | 2) Clean this query with only the wanted labels as output 18 | ```console 19 | 20 | aws rekognition detect-labels --image "S3Object={Bucket=image-rekogniton-cli,Name=times-square.jpg}" --query 'Labels[].Name' 21 | ``` 22 | 23 | 3) Celebrity Detection: with Ronaldo and Messi 24 | ```console 25 | aws rekognition recognize-celebrities --image "S3Object={Bucket=image-rekogniton-cli,Name=ronaldo.jpg}" --query 'CelebrityFaces[].Name' 26 | ``` 27 | 4) Compare Messi with a lookalike 28 | ```console 29 | aws rekognition compare-faces --source-image "S3Object={Bucket=image-rekogniton-cli,Name=messi.jpg}" --target-image "S3Object={Bucket=image-rekogniton-cli,Name=lookalike.jpg}" --query 'FaceMatches[].Similarity' 30 | ``` 31 | 32 | 5) Compare Messi(Bearded) with a young Messi ( No Beard). Results are crazy accurate! 33 | 34 | 35 | 6) Video Recognition with a video of a busy city street. You'll get a JobID in return. Save this we need this for later 36 | ```console 37 | aws rekognition start-label-detection --video "S3Object={Bucket=image-rekogniton-cli,Name=Pedestrians.mp4}" 38 | 39 | ``` 40 | 41 | 7) Get the labels from Video 42 | ```console 43 | aws rekognition get-label-detection --job-id 206a930fce775abeed05203637342a36ced6a52d71b99e77228c1fd8fe551471 --query Labels[].Label[].[Confidence,Name] 44 | ``` 45 | 46 | 47 | 8) Adult content Detection 48 | ``` 49 | aws rekognition detect-moderation-labels --source-image file:///Users/your-username/downloads/example.jpg 50 | 51 | ``` 52 | # Further Reference 53 | https://docs.aws.amazon.com/cli/latest/reference/rekognition/index.html#cli-aws-rekognition 54 | -------------------------------------------------------------------------------- /cloudwatch/cloudwatch-cli-useful-commands.MD: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## get-metric-statistics 4 | 5 | Gets statistics for the specified metric. 6 | 7 | The maximum number of data points returned from a single call is 1,440. If you request more than 1,440 data points, CloudWatch returns an error. To reduce the number of data points, you can narrow the specified time range and make multiple requests across adjacent time ranges, or you can increase the specified period. Data points are not returned in chronological order. 8 | 9 | ## Amazon CloudWatch retains metric data as follows: 10 | 11 | - Data points with a period of less than 60 seconds are available for 3 hours. These data points are high-resolution metrics and are available only for custom metrics that have been defined with a StorageResolution of 1. 12 | - Data points with a period of 60 seconds (1-minute) are available for 15 days. 13 | - Data points with a period of 300 seconds (5-minute) are available for 63 days. 14 | - Data points with a period of 3600 seconds (1 hour) are available for 455 days (15 months). 15 | 16 | 17 | This command will return 18 | 19 | ``` 20 | aws cloudwatch get-metric-statistics --metric-name CPUUtilization --start-time 2018-06-01T23:18:00Z --end-time 2018-06-19T23:18:00Z --period 3600 --namespace AWS/EC2 --statistics Average --dimensions Name=InstanceId,Value=i-07442b7dca24a5740 21 | ``` 22 | 23 | ```json 24 | "Timestamp": "2018-06-04T20:15:00Z", 25 | "Average": 8.612647031582847, 26 | "Unit": "Percent" 27 | }, 28 | { 29 | "Timestamp": "2018-06-18T14:15:00Z", 30 | "Average": 8.627103207681818, 31 | "Unit": "Percent" 32 | }, 33 | 34 | { 35 | "Timestamp": "2018-06-15T19:15:00Z", 36 | "Average": 9.349541539316677, 37 | "Unit": "Percent" 38 | }, 39 | 40 | { 41 | "Timestamp": "2018-06-05T10:15:00Z", 42 | "Average": 9.144206724089889, 43 | "Unit": "Percent" 44 | }, 45 | { 46 | "Timestamp": "2018-06-08T17:15:00Z", 47 | "Average": 10.231900836651965, 48 | "Unit": "Percent" 49 | }, 50 | { 51 | "Timestamp": "2018-06-11T11:15:00Z", 52 | "Average": 9.000470809792224, 53 | "Unit": "Percent" 54 | }, 55 | { 56 | "Timestamp": "2018-06-08T04:15:00Z", 57 | "Average": 12.447770985768054, 58 | "Unit": "Percent" 59 | } 60 | ], 61 | "Label": "CPUUtilization" 62 | } 63 | 64 | ``` 65 | To get the max CPU Utilization use this command: 66 | 67 | ``` 68 | aws cloudwatch get-metric-statistics --metric-name CPUUtilization --start-time 2018-06-01T23:18:00Z --end-time 2018-06-19T23:18:00Z --period 3600 --namespace AWS/EC2 --statistics Maximum --dimensions Name=InstanceId,Value=i-07442b7dca24a5740 69 | ``` 70 | 71 | ```json 72 | { 73 | "Timestamp": "2018-06-18T00:15:00Z", 74 | "Maximum": 28.3333333333455, 75 | "Unit": "Percent" 76 | }, 77 | { 78 | "Timestamp": "2018-06-05T10:15:00Z", 79 | "Maximum": 30.5084745762589, 80 | "Unit": "Percent" 81 | }, 82 | { 83 | "Timestamp": "2018-06-08T17:15:00Z", 84 | "Maximum": 29.9999999999879, 85 | "Unit": "Percent" 86 | }, 87 | { 88 | "Timestamp": "2018-06-11T11:15:00Z", 89 | "Maximum": 28.3333333333455, 90 | "Unit": "Percent" 91 | }, 92 | { 93 | "Timestamp": "2018-06-08T04:15:00Z", 94 | "Maximum": 100.0, 95 | "Unit": "Percent" 96 | } 97 | ], 98 | "Label": "CPUUtilization" 99 | ``` 100 | 101 | -------------------------------------------------------------------------------- /ec2/ec2-create-snapshots-and-restore.txt: -------------------------------------------------------------------------------- 1 | Creating Snapshots with the CLI Lab 2 | 3 | 4 | 5 | To create a snapshot 6 | This example command creates a snapshot of the volume with a volume ID of vol-1234567890abcdef0 and a short description to identify the snapshot. 7 | Command: 8 | aws ec2 create-snapshot --volume-id vol-1234567890abcdef0 --description "This is my root volume snapshot." 9 | To create a new volume 10 | This example command creates an 10 GiB General Purpose (SSD) volume in the Availability Zone us-east-1a. 11 | Command: 12 | aws ec2 create-volume --size 10 --region us-east-1 --availability-zone us-east-1a --volume-type gp2 13 | 14 | To attach a volume to an instance 15 | This example command attaches a volume (vol-1234567890abcdef0) to an instance (i-01474ef662b89480) as /dev/sdf. 16 | Command: 17 | aws ec2 attach-volume --volume-id vol-1234567890abcdef0 --instance-id i-01474ef662b89480 --device /dev/sdf 18 | 19 | 20 | 21 | To make an EBS volume available for use on Linux 22 | 23 | 1. Connect to your instance using SSH. 24 | 2. Use the lsblk command to view your available disk devices and their mount points (if applicable) to help you determine the correct device name to use. 25 | 3. New volumes are raw block devices, and you must create a file system on them before you can mount and use them. Use the sudo file -s dev/xvdf  command to list special information, such as file system type. 26 | 4. Use the following command to create an ext4 file system on the volume. Substitute the device name: sudo mkfs -t ext4 /dev/xvdf 27 | 5. Make a mount point : [ec2-user ~]$ sudo mkdir mount_point 28 | 6. Use the following command to mount the volume at the location you just created: [ec2-user ~]$ sudo mount /dev/xvdf mount_point 29 | 7. Write files to your new mount point. 30 | 31 | 32 | 33 | • cd newDrive/ 34 | • ls 35 | • sudo chown ec2-user:ec2-user -R newDrive/ 36 | • cd newDrive/ 37 | • echo "hello">hello.txt 38 | • ls 39 | • cd .. 40 | • umount newDrive/ 41 | • sudo umount newDrive/ 42 | • ls 43 | • cd newDrive/ 44 | • ls 45 | 46 | create a snapshot 47 | Take a snapshot of the device you just wrote to. 48 | Command: 49 | aws ec2 create-snapshot --volume-id vol-1234567890abcdef0 --description "This is my second volume snapshot." 50 | 51 | To detach a volume from an instance 52 | This example command detaches the volume (vol-049df61146c4d7901) from the instance it is attached to. 53 | Command: 54 | aws ec2 detach-volume --volume-id vol-1234567890abcdef0 55 | 56 | Attach the volume to another instance: 57 | aws ec2 attach-volume --volume-id vol-0d234f62cfca629c5 --instance-id i-07442b7dca24a5740 --device /dev/sdf 58 | 59 | Mount the volume by following the same steps as above. 60 | Then: 61 | lsblk 62 | mkdir test 63 | sudo mount /dev/xvdf test/ 64 | ls 65 | cd test/ 66 | ls 67 | 68 | 69 | You should see the file hello.txt 70 | 71 | 72 | 73 | 74 | Now let’s detach delete the volume: 75 | aws ec2 detach-volume --volume-id vol-0d234f62cfca629c5 76 | 77 | 78 | Then let’s delete the volume: 79 | 80 | aws ec2 delete-volume --volume-id vol-0d234f62cfca629c5 81 | 82 | Now let’s list all the snapshot: 83 | aws ec2 describe-snapshots 84 | 85 | Find the snapshot we created earlier by looking at the description: 86 | Restore the snapshot: 87 | aws ec2 create-volume --region us-east-1 --availability-zone us-east-1a --volume-type gp2 --snapshot-id snap-06e1af3ca95e5a387 88 | 89 | Attach that to one of the instances: 90 |  sudo file -s /dev/xvdf 91 | sudo mount /dev/xvdf newDrive/ 92 |  ls 93 |  cd newDrive/ 94 |  ls 95 | 96 | You should see your file you saved earlier. 97 | 98 | 99 | This concludes the lab. We took snapshots and restored them later. 100 | 101 | Make sure to delete the instances, volumes and snapshots after you’re done. 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /vpc/create-a-vpc-with-subnets-RT-GATEWAYS-etc.MD: -------------------------------------------------------------------------------- 1 | If you don’t have the CLI configured download and configure it : 2 | http://docs.aws.amazon.com/cli/latest/userguide/installing.html 3 | 4 | 5 | I have emphasized tagging everything you create because it will save a lot of frustration later 6 | when trying to figure out which resource is which. 7 | 8 | 9 | Here are the steps: 10 | 11 | Create VPC: 12 | ```console 13 | aws ec2 create-vpc --cidr-block 10.0.0.0/16 14 | ``` 15 | 16 | Tag That VPC: 17 | ```console 18 | aws ec2 create-tags --resources vpc-d363afab --tags Key=Name,Value=CLI-VPC 19 | ``` 20 | 21 | Create a public Subnet: 22 | ```console 23 | aws ec2 create-subnet --vpc-id vpc-d363afab --cidr-block 10.0.1.0/24 24 | ``` 25 | 26 | Tag: 27 | ```console 28 | aws ec2 create-tags --resources subnet-7314ad17 --tags Key=Name,Value=CLI-Public-Subnet 29 | ``` 30 | 31 | Create a private Subnet: 32 | ```console 33 | aws ec2 create-subnet --vpc-id vpc-d363afab --cidr-block 10.0.2.0/24 34 | ``` 35 | Tag: 36 | ```console 37 | aws ec2 create-tags --resources subnet-4109b025 --tags Key=Name,Value=CLI-Private-Subnet 38 | ``` 39 | 40 | Create an Internet Gateway: 41 | ```console 42 | aws ec2 create-internet-gateway 43 | ``` 44 | 45 | Tag: 46 | ```console 47 | aws ec2 create-tags --resources igw-afdd01d6 --tags Key=Name,Value=CLI-Internet-Gateway 48 | ``` 49 | 50 | Attach Internet Gateway: 51 | ```console 52 | aws ec2 attach-internet-gateway --internet-gateway-id igw-5d685a38 --vpc-id vpc-d363afab 53 | ``` 54 | 55 | 56 | 57 | 58 | Allocate Elastic IP: 59 | ```console 60 | aws ec2 allocate-address --domain vpc 61 | ``` 62 | 63 | Create a Nat-gateway and place it in the public Subnet: 64 | ```console 65 | aws ec2 create-nat-gateway --subnet-id subnet-1a2b3c4d --allocation-id eipalloc-37fc1a52 66 | ``` 67 | 68 | Tag: 69 | ```console 70 | aws ec2 create-tags --resources nat-0e4d97e539eadf232 --tags Key=Name,Value=CLI-Nat-Gateway 71 | ``` 72 | 73 | Create Route Table 1 for public Subnet: 74 | ```console 75 | aws ec2 create-route-table --vpc-id vpc-d363afab 76 | # Tag 77 | aws ec2 create-tags --resources rtb-14c3736e --tags Key=Name,Value=CLI-PUBLIC_RT 78 | ``` 79 | 80 | Create Route Table 2 for private Subnet: 81 | 82 | ```console 83 | aws ec2 create-route-table --vpc-id vpc-d363afab 84 | 85 | #Tag: 86 | 87 | aws ec2 create-tags --resources rtb-cbc070b1 --tags Key=Name,Value=CLI-PRIVATE_RT 88 | ``` 89 | 90 | 91 | 92 | 93 | 94 | Create a route to the internet in Route Table 1: 95 | ```console 96 | aws ec2 create-route --route-table-id rtb-14c3736e --destination-cidr-block 0.0.0.0/0 --gateway-id igw-afdd01d6 97 | ``` 98 | 99 | Create a route to the internet in Route Table 2 via Nat: 100 | ```console 101 | aws ec2 create-route --route-table-id rtb-cbc070b1 --destination-cidr-block 0.0.0.0/0 -- gateway-id nat-0e4d97e539eadf232 102 | ``` 103 | 104 | Associate Route Table 1 to PublicSubnet : 105 | ```console 106 | aws ec2 associate-route-table --route-table-id rtb-14c3736e --subnet-id subnet-7314ad17 107 | ``` 108 | 109 | Associate Route Table 2 to PrivateSubnet: 110 | ```console 111 | aws ec2 associate-route-table --route-table-id rtb-1245623e --subnet-id subnet-234567as 112 | ``` 113 | 114 | Create a Security Group for Web Access and SSH: 115 | ```console 116 | aws ec2 create-security-group --group-name CLI-WEB-SecurityGroup --description "My 117 | security group" --vpc-id vpc-d363afab 118 | ``` 119 | 120 | Tag: 121 | ```console 122 | aws ec2 create-tags --resources sg-03ca1371 --tags Key=Name,Value=CLI_SECURITY_GROUP 123 | Add Ingress Port 22 and 80: 124 | ``` 125 | 126 | Create Key Pair and copy the key part and write it to a file MyKeyPairCLI.pem : 127 | ```console 128 | aws ec2 create-key-pair --key-name MyKeyPairCLI 129 | ``` 130 | 131 | Change the permissions on that file to restrict access: 132 | ```console 133 | chmod 400 MyKeyPairCLI.pem 134 | ``` 135 | 136 | Launch EC2 Instance In public subnet with Amazon AMI ami-8c1be5f6 : 137 | ```console 138 | aws ec2 run-instances --image-id ami-8c1be5f6 --count 1 --instance-type t2.micro --key- name MyKeyPairCLI --security-group-ids sg-c3ed34b1 --subnet-id subnet-7314ad17 -- associate-public-ip-address 139 | ``` 140 | 141 | TAG EC2: 142 | ```console 143 | aws ec2 create-tags --resources i-05c8b15394d0905b8 --tags Key=Name,Value=CLI_EC2 144 | ``` 145 | 146 | Describe Instance to get the IP or check the console: 147 | ```console 148 | aws ec2 describe-instances 149 | ``` 150 | 151 | SSH into your web browser: 152 | ```console 153 | ssh ec2-user@34.34.234.4 -i MyKeyPairCLI.pem 154 | ``` 155 | 156 | 157 | And you’re now logged in! Have fun with your setup. 158 | --------------------------------------------------------------------------------