Overview
When you want to bring your idea to life as an application, the process typically involves the following steps:
Development:
- You need a developer to write the code for your application using a programming language such as Python, Java, or others.
Hosting the Application:
- After the application is developed, it requires an operating system and hardware resources like CPU, RAM, and storage to run. These resources can be obtained from cloud service providers such as AWS (Amazon Web Services).
Scaling the Application:
- If the application receives high traffic, more resources (scaling out) might be needed. If traffic decreases, fewer resources are required (scaling in).
Using AWS Services
AWS offers two ways to use its services:
Manual:
- This involves using the AWS Management Console. While straightforward, it can be slow and time-consuming for repetitive tasks.
Automation:
Automation is faster and more efficient. It can be categorized into two types:
Imperative: You explicitly tell the system what to do and how to do it. For example, writing scripts in Python or Java.
Declarative: You only specify what you want, and the system figures out how to do it. Tools like Terraform use this approach.
Here’s your original code, rewritten with the sensitive AWS credentials removed and placed into comments for security best practices. It’s otherwise unaltered:
import boto3
# Create EC2 client
myec1 = boto3.client(
"ec2",
region_name="ap-south-1",
aws_access_key_id="YOUR_AWS_ACCESS_KEY", # Replace with your AWS access key
aws_secret_access_key="YOUR_AWS_SECRET_KEY" # Replace with your AWS secret key
)
myoslist = [] # List to store instance IDs
def lwoslaunch():
"""
Launch an EC2 instance and return its instance ID.
"""
myid = myec1.create_instances(
InstanceType="t2.micro",
ImageId="ami-0fd05997b4dff7aac",
MinCount=1,
MaxCount=1
)
return myid
# Example usage
myos = lwoslaunch()
myoslist.append(myos[0].id) # Add the instance ID to the list
print(myoslist)
def lwosterminate(n):
"""
Terminate an EC2 instance by its instance ID.
"""
myec1.terminate_instances(InstanceIds=[n])
# Terminate instance example
los = myoslist[0] # Replace with the instance ID to terminate
lwosterminate(los)
Important Notes
Hide Your AWS Credentials:
Never hard-code AWS credentials in your script. Instead:
Use environment variables.
Use the AWS CLI to configure credentials.
Assign an IAM role to your EC2 instance (if running within AWS).
Improving Security:
- Replace
"YOUR_AWS_ACCESS_KEY"
and"YOUR_AWS_SECRET_KEY"
with a secure method like environment variables.
- Replace
Declarative Approach:
- Use tools like Terraform if you prefer a declarative approach where you define what to create rather than how.
Summary
Imperative Automation: Python and Boto3 allow you to write scripts to launch and terminate EC2 instances by specifying all steps.
Declarative Automation: Tools like Terraform simplify resource management by letting you define the desired end state.
This process enables you to host your application efficiently and scale it based on traffic requirements.