EC2
Created: 2016-02-17 11:24:12 -0800 Modified: 2019-09-25 10:34:11 -0700
General information:
Section titled General information:- You only pay for VMs that are in the “running” state, not in pending, shutting-down, terminated, stopping, or stopped. However, you pay for your hourly usage rounded UP, so even if you only use a VM for 1 minute, you’ll get charged the hourly rate.
- A reservation is the act of launching 1 or more instances. For example, if you launch 50 instances, you’ll get one reservation ID returned.
-
Spot instances can be super cheap compared to on-demand instances, but it’s because they can be terminated at any time without warning (reference). Prices are updated every 5 minutes, but here’s a random snapshot on 5/20/2019 1:33 PM just to show the difference for t2.micro instances in all the configurations:
Section titled Spot instances can be super cheap compared to on-demand instances, but it’s because they can be terminated at any time without warning (reference). Prices are updated every 5 minutes, but here’s a random snapshot on 5/20/2019 1:33 PM just to show the difference for t2.micro instances in all the configurations:
Cost/hr | Type | Savings |
---|---|---|
$0.0116 | On-demand | 0% |
$0.008 | 6 hours reserved usage of spot instance | 31% |
$0.006 | 1 hour of reserved usage of spot instance | 48% |
$0.0035 | No reserved usage (i.e. you can be terminated at any time) | 70% |
- If you’re looking to get the absolute most out of your CPU, then ClearLinux could be a good choice since it’s optimized for Intel hardware (reference).
- “Instance store” - this is ephemeral storage that is saved to the physical disk attached to the VM. It is only available on the higher tier instances.
- There’s are nice pricing/cost sites here (preferred) and here.
- Burstable CPUs (e.g. with t2.micro instances) are explained in a human-understandable way here, with the official docs noting how you can exceed your max credits here.
- Information on max credits earnable here.
- t2.micro instances start with 30 credits available at launch, meaning you can run at 100% CPU for 30 minutes (reference). I believe that you’ll accrue another 3 credits during that time, so it’s actually 33 minutes of 100% CPU usage.
- Credits can be burned by fractional amounts, i.e. using 100 ms of 100% CPU does not mean you spend an entire credit for that minute.
- At any time (i.e. whether your instance is off or running), you can change the “unlimited” aspect of your instance (via Actions → Instance Settings → Change T2/T3 Unlimited) so that you just end up paying for CPU credits rather than being limited to 10% CPU usage when you run out. (reference)
- To quickly run a Docker image on an EC2 instance, check out this note.
Setting up
Section titled Setting up- Run basic instructions from the AWS console.
- Note: Amazon Linux is kind of a generic Linux that has a package manager and some developer defaults: Python, Java, etc.
- The image is 8 GB (so you can’t provision any less than that) and ~1.2 GB of that is used by default.
- I set up my VM such that it was accessible by the whole world, but you can only log in through SSH.
- I believe you should be able to just connect using this command
- ssh -i “C:Usersagd13_000DownloadsFirstkeypair.pem” ec2-user@ec2-52-36-149-173.us-west-2.compute.amazonaws.com
- NOTE: the “ec2-user@” portion is very important! I believe this is needed instead of “admin” when you’re using Amazon’s flavor of Linux. If you’re using something like Debian, just use “admin”. If you’re not sure, use “root” and it will probably tell you to use “admin”.
- ssh -i “C:Usersagd13_000DownloadsFirstkeypair.pem” ec2-user@ec2-52-36-149-173.us-west-2.compute.amazonaws.com
- If the above SSH command doesn’t work, then you should do this:
- Convert the .pem file into a key format that PuTTY can use (.ppk): PuTTYGen —> Conversions —> Import key —> Save private key.
- Then, in PuTTY, I connected as ec2-user@<endpoint from AWS console> and specified my private key in Connection — >SSH —> Auth —> Browse.
- Alternatively, you could add the PPK to Pageant and then use ssh_butler (a CMD I wrote to start ssh-pageant).
- If SSH still doesn’t work (because of a timeout), you should read this page
- When I had screwed this up before, the solution ended up being that I needed to create an Internet Gateway and then modify my existing Route Table had a route for 0.0.0.0/0 that pointed to the IG.
- Transferring files: read this.
- “yum” is the package manager. To install the mysql client, I typed this:
- sudo yum install mysql
- To install NodeJS, look at this note
Elastic IPs
Section titled Elastic IPsElastic IPs are useful if you want to be able to have the same IP address point to any arbitrary running instance that you own. This way, you don’t need to wait for DNS to catch up.
Also, note that you don’t have to use elastic IPs for development; they’re mostly for production. You can use instance IPs (which I believe are temporary and could be reassigned at any time?): AWS console —> VPC —> Subnets —> right-click your subnet —> Modify Auto-Assign Public IP —> Enable them. Instance IPs are totally free.
You are charged for elastic IPs when you use too many or when you have unused IP addresses. To manage them, go to the EC2 console —> Network & Security —> Elastic IPs. You will need to allocate an IP and then associate the IP with a running instance. When you’re done with the IP, you need to release it.
Getting metadata (reference)
Section titled Getting metadata (reference)- To figure out metadata about an instance, you can access an internal AWS config service from that particular VM (not from another VM):
- curl http://169.254.169.254/latest/meta-data/
- Note: ‘latest’ refers to the version of the API, not the VM that you’re using it from.
- From here, you’ll get a list like “ami-id” and “hostname”. You can do “curl http://169.254.169.254/latest/meta-data/local-ipv4” for example to figure out your internal IP address.
- curl http://169.254.169.254/latest/meta-data/
- You can store up to 16 KB of user data using the metadata service (reference).
- At the time you call runInstances, pass a base-64-encoded UserData string. If this is an object, you need to serialize it (so JSON.stringify makes the most sense for the JavaScript).
- When you want to fetch the data, retrieve it via /latest/user-data