AWS

A Python script to create an EC2 Spot instance using boto3

CK CK 3788 
Created at  
1,603 0 0

How do you create EC2 Spot Instance

A Python script to create an EC2 Spot instance using boto3. The script launches a spot instance based on an AMI, associates it with a specific security group in a given VPC, and assigns custom tags for easy identification.

Prerequisites:

  • Install boto3: pip install boto3
  • Configure AWS CLI credentials: aws configure
import boto3
import time

# AWS Configuration
region = "us-east-1"  # Change as needed
ami_id = "ami-xxxxxxxxxxxxxxxxx"  # Replace with your AMI ID
instance_type = "t3.micro"  # Change instance type as needed
security_group_id = "sg-xxxxxxxxxxxxxx"  # Replace with your security group ID
subnet_id = "subnet-xxxxxxxxxxxxxx"  # Replace with your subnet ID within the VPC
key_name = "your-key-pair"  # Replace with your SSH key pair name
tag_name = "MySpotInstance"

# Initialize boto3 clients
ec2_client = boto3.client("ec2", region_name=region)

# Request Spot Instance
spot_request = ec2_client.request_spot_instances(
    InstanceCount=1,
    LaunchSpecification={
        "ImageId": ami_id,
        "InstanceType": instance_type,
        "KeyName": key_name,
        "SecurityGroupIds": [security_group_id],
        "SubnetId": subnet_id,
    },
    Type="one-time",
    SpotPrice="0.005",  # Adjust price based on instance type and AWS pricing
)

# Extract Spot Request ID
spot_request_id = spot_request["SpotInstanceRequests"][0]["SpotInstanceRequestId"]
print(f"Spot Instance Request ID: {spot_request_id}")

# Wait for Spot Request to be fulfilled
while True:
    response = ec2_client.describe_spot_instance_requests(SpotInstanceRequestIds=[spot_request_id])
    state = response["SpotInstanceRequests"][0]["State"]
    print(f"Spot request state: {state}")

    if state == "active":
        instance_id = response["SpotInstanceRequests"][0]["InstanceId"]
        print(f"Spot Instance Launched: {instance_id}")
        break
    elif state in ["cancelled", "failed", "closed"]:
        print("Spot request failed or was cancelled.")
        exit(1)

    time.sleep(5)  # Wait and check again

# Assign Tags to the Instance
ec2_client.create_tags(
    Resources=[instance_id],
    Tags=[
        {"Key": "Name", "Value": tag_name},
        {"Key": "Environment", "Value": "Test"},
        {"Key": "Owner", "Value": "YourName"},
    ],
)

print(f"Instance {instance_id} tagged successfully!")

Explanation:

  1. Request a Spot Instance: Uses request_spot_instances() to request a spot instance with a specified AMI, instance type, security group, and subnet.
  2. Monitor the request: The script polls AWS every 5 seconds until the instance is assigned.
  3. Assign tags: Tags are applied for easier searching and organization.

 

Search EC2 Instances with Tag Information

You can search for EC2 instances by tags using boto3 in Python. The describe_instances method allows filtering instances based on specific tags.

Example: Search EC2 Instances by Tag

This script retrieves EC2 instances with a specific tag and prints their instance IDs and details.

Python Script

import boto3

# AWS Configuration
region = "us-east-1"  # Change as needed
tag_key = "Name"  # Change to the tag key you are searching for
tag_value = "MySpotInstance"  # Change to the tag value you are searching for

# Initialize EC2 client
ec2_client = boto3.client("ec2", region_name=region)

# Search EC2 instances based on a tag
response = ec2_client.describe_instances(
    Filters=[
        {"Name": f"tag:{tag_key}", "Values": [tag_value]},
        {"Name": "instance-state-name", "Values": ["running", "pending"]},  # Optional: Filter by state
    ]
)

# Extract instance details
instances = []
for reservation in response["Reservations"]:
    for instance in reservation["Instances"]:
        instances.append({
            "Instance ID": instance["InstanceId"],
            "State": instance["State"]["Name"],
            "Type": instance["InstanceType"],
            "Public IP": instance.get("PublicIpAddress", "N/A"),
            "Private IP": instance["PrivateIpAddress"],
        })

# Display results
if instances:
    for instance in instances:
        print(instance)
else:
    print("No instances found with the given tag.")

How It Works:

  1. Uses describe_instances() with a filter on tag:Name = MySpotInstance.
  2. Filters only running or pending instances (you can modify this).
  3. Extracts instance details (ID, state, type, IP addresses).
  4. Displays results in a structured format.

Example Output:

{'Instance ID': 'i-0123456789abcdef0', 'State': 'running', 'Type': 't3.micro', 'Public IP': '34.123.45.67', 'Private IP': '10.0.1.12'}

 

 

Terminate EC2 Instances

You can terminate an EC2 instance using boto3 in Python by calling the terminate_instances() method.

Python Script to Terminate an EC2 Instance

import boto3

# AWS Configuration
region = "us-east-1"  # Change as needed
instance_id = "i-0123456789abcdef0"  # Replace with your EC2 Instance ID

# Initialize EC2 client
ec2_client = boto3.client("ec2", region_name=region)

# Terminate the instance
response = ec2_client.terminate_instances(InstanceIds=[instance_id])

# Extract termination details
for instance in response["TerminatingInstances"]:
    print(f"Instance {instance['InstanceId']} is {instance['CurrentState']['Name']}")

How It Works

  1. Uses terminate_instances() to terminate the specified EC2 instance.
  2. Prints the instance ID and its current state.

Example Output

Instance i-0123456789abcdef0 is shutting-down

Terminate EC2 Instances by Tag

If you want to terminate instances based on a tag, use this script:

# Terminate EC2 instances by tag
tag_key = "Name"
tag_value = "MySpotInstance"

# Search for instances with the given tag
instances = ec2_client.describe_instances(
    Filters=[{"Name": f"tag:{tag_key}", "Values": [tag_value]}]
)

instance_ids = [
    instance["InstanceId"]
    for reservation in instances["Reservations"]
    for instance in reservation["Instances"]
]

if instance_ids:
    response = ec2_client.terminate_instances(InstanceIds=instance_ids)
    for instance in response["TerminatingInstances"]:
        print(f"Instance {instance['InstanceId']} is {instance['CurrentState']['Name']}")
else:
    print("No instances found with the given tag.")

Precautions

  • Cannot undo termination: Once an EC2 instance is terminated, you cannot recover it.
  • Detach important volumes: If your instance has valuable EBS volumes, detach them first.
  • Verify instance ID: Double-check that you're terminating the correct instance.

 

 

Tags AWS Amazon EC2 EC2 EC2 Spot Instance Spot Instance Facebook X
Comments 0
Similar posts
  1. A Python/PHP code to deregister anormalous instaces in Amazon ECS
    1,858
  2. The Ultimate Data Engineering Roadmap
    1,073
  3. How to Set Up and Deploy Containerized Applications with AWS ECS
    10,675
  4. Check incomplete multipart uploads if AWS S3 storage size is abnormally big huge compared to the origin storage
    11,598
  5. Time series processing architecture in AWS
    10,559
  6. Where to get AWS Access Key ?
    11,264
  1. Building a Video Content Recommendation System with Amazon SageMaker
    524
  2. The Immense Scale and Management of Exabyte Data with AWS
    509
  3. How to change your AWS Credential Region in CLI?
    754
  4. Default Timeout for Amazon Bedrock invoke_model
    736
  5. When you face 504 Timeout Errors by Amazon ALB
    1,472
  6. When you face 504 Gateway Timeout while you wait a response from Bedrock in Fast API
    535
  7. How to overcome the prompt size limit when you use Claude via Bedrock - Chunking / Splitting the Input
    496
  8. Integrating VectorDB stored in S3 with Amazon Bedrock
    425
  9. When you need AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY for Amazon Bedrock
    534
  10. How Netflix Runs Recommendations for Hundreds of Millions of Users Using Amazon SageMaker & Metaflow
    175
  11. Why Amazon VPC satisfies with PCI-DSS and SOC 2 Type on Fintech Industry?
    645
  12. You shouldn't use Amazon CloudFront distribution as an origin
    634
  13. Amagi CLOUDPORT Architecture based on AWS for FAST for channel playout on AWS
    1,318
  14. Creating a serverless Apache Kafka publisher using AWS Lambda
    564
  15. AWS System Design Example - Instagram Architecture
    1,918
  16. Where to get AWS API Key at AWS Management Console?
    1,071
  17. Understanding Amazon Bedrock Agent for AI-based ChatBot
    644
  18. Media sharing service architecture in AWS
    10,367
  19. Enterprise Data Lake Architecture based on AWS
    10,407
  20. "502 Bad Gateway" Error easy to happen at the combination of nginx and node.js in AWS Elastic Beanstalk
    10,490
  21. Power of Containerized Deployment with AWS ECS
    10,597
  22. FAISS Already Does ANN... So Why Do Top Teams Still Pay for SageMaker?
    310
  23. Recommended Vector DB Structure for Amazon Bedrock Integration
    390
  24. Product Marketing & Recommendation Architecture in AWS for e-commerce website
    10,737
  25. What cause a 502 bad gateway error in AWS environment?
    444
  26. Building an AWS SAM(Serverless Application Model) Project
    899
  27. Teraform from the scratch
    1,357
  28. Importing a wild card domain SSL certificate into AWS Certificate Manager
    10,348
  29. AWS Lambda Example based on Teraform
    900
  30. Simple AWS ECS example based on Teraform
    989
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,044
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    252
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    515
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  16. TPU & GPU Cluster Management Tools
    245
  17. The Concept of Scaffolding in AI Agents
    234
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    222
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    221
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  30. Understanding Data Lineage
    263