0
0
AWScloud~5 mins

EC2 pricing models (on-demand, reserved, spot) in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Choosing the right pricing model for Amazon EC2 helps you save money and match your server costs to your needs. On-demand lets you pay for what you use, reserved saves money if you commit ahead, and spot offers big discounts for flexible workloads.
When you need a server immediately and want to pay only for the time you use it without long-term commitment.
When you have steady, predictable workloads and want to save money by committing to use servers for 1 or 3 years.
When you can handle interruptions and want to run tasks at a much lower cost using spare AWS capacity.
When you want to test a new application quickly without upfront costs.
When you run batch jobs or background tasks that can pause and resume without issues.
Commands
Launches an EC2 instance using the on-demand pricing model by default, which means you pay for the instance by the hour or second without any upfront commitment.
Terminal
aws ec2 run-instances --image-id ami-0abcdef1234567890 --count 1 --instance-type t3.micro --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=OnDemandInstance}]'
Expected OutputExpected
{ "Instances": [ { "InstanceId": "i-0123456789abcdef0", "ImageId": "ami-0abcdef1234567890", "InstanceType": "t3.micro", "State": { "Code": 0, "Name": "pending" }, "Tags": [ { "Key": "Name", "Value": "OnDemandInstance" } ] } ] }
--image-id - Specifies the Amazon Machine Image to use for the instance.
--instance-type - Defines the hardware configuration of the instance.
--tag-specifications - Adds tags to help identify the instance.
Lists available reserved instance offerings for t3.micro with Linux, showing options where you pay upfront for a 1 or 3 year term to save money.
Terminal
aws ec2 describe-reserved-instances-offerings --instance-type t3.micro --product-description "Linux/UNIX" --offering-type "All Upfront"
Expected OutputExpected
{ "ReservedInstancesOfferings": [ { "ReservedInstancesOfferingId": "abcdef12-3456-7890-abcd-ef1234567890", "InstanceType": "t3.micro", "Duration": 31536000, "FixedPrice": 50.0, "UsagePrice": 0.0, "CurrencyCode": "USD", "OfferingType": "All Upfront", "ProductDescription": "Linux/UNIX" } ] }
--instance-type - Filters offerings by instance type.
--offering-type - Filters by payment option like All Upfront.
Requests a spot instance with a maximum price of $0.005 per hour, which uses spare AWS capacity at a discount but can be interrupted.
Terminal
aws ec2 request-spot-instances --spot-price "0.005" --instance-count 1 --launch-specification '{"ImageId":"ami-0abcdef1234567890","InstanceType":"t3.micro"}'
Expected OutputExpected
{ "SpotInstanceRequests": [ { "SpotInstanceRequestId": "sir-08b93456", "State": "open", "Status": { "Code": "pending-evaluation", "Message": "Your Spot request is being evaluated." }, "LaunchSpecification": { "ImageId": "ami-0abcdef1234567890", "InstanceType": "t3.micro" } } ] }
--spot-price - Sets the maximum price you are willing to pay per hour.
--launch-specification - Defines the instance details for the spot request.
Checks the status of the spot instance request to see if it has been fulfilled and the instance is running.
Terminal
aws ec2 describe-spot-instance-requests --spot-instance-request-ids sir-08b93456
Expected OutputExpected
{ "SpotInstanceRequests": [ { "SpotInstanceRequestId": "sir-08b93456", "State": "active", "Status": { "Code": "fulfilled", "Message": "Your Spot request is fulfilled." }, "InstanceId": "i-0abcdef1234567890" } ] }
Key Concept

If you remember nothing else from this pattern, remember: On-demand is pay-as-you-go, reserved saves money with commitment, and spot offers big discounts with possible interruptions.

Common Mistakes
Trying to launch a reserved instance directly with the run-instances command.
Reserved instances are a billing discount applied to matching running instances, not a separate instance type you launch.
Purchase reserved instances separately via the AWS console or CLI, then launch normal instances that match the reserved instance attributes.
Setting a spot price higher than the on-demand price.
Spot prices never exceed on-demand prices, so setting a higher price wastes money.
Set the spot price at or below the current on-demand price to save costs.
Not checking spot instance request status after requesting.
Spot requests may not be fulfilled immediately or can be interrupted, so you might think your instance is running when it is not.
Use describe-spot-instance-requests to verify the request status and instance ID.
Summary
Use 'aws ec2 run-instances' to launch on-demand instances that charge by usage without commitment.
Use 'aws ec2 describe-reserved-instances-offerings' to find reserved instance options that save money with upfront payment.
Use 'aws ec2 request-spot-instances' to request low-cost spot instances that can be interrupted.
Always check the status of spot requests with 'aws ec2 describe-spot-instance-requests' to confirm fulfillment.