Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Automation/src/multiple-ec2-instances/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ACCESS_KEY="{your aws access key}"
SECRET_KEY="{your aws secret key}"
AMI="{your ami}"
REGION="{your preferred region}"
ZONE="{your zone}"
TYPE="{instance type}"
SUBNET="{subnet}"
28 changes: 28 additions & 0 deletions Automation/src/multiple-ec2-instances/ec2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import boto3,os
from dotenv import load_dotenv
load_dotenv()

#Load env variables
Comment on lines +3 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment describing what the script is about and how to use it 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey I have added the steps in the ec2.py file , if I should add in any other way please let me know

access_key= os.getenv("ACCESS_KEY")
secret_key= os.getenv("SECRET_KEY")
ami= os.getenv("AMI")
region= os.getenv("REGION")
zone= os.getenv("ZONE")
type= os.getenv("TYPE")
subnet = os.getenv("SUBNET")

client = boto3.client(service_name='ec2', region_name=region, aws_access_key_id= access_key,aws_secret_access_key= secret_key)

# Create ec2 resource
ec2 = boto3.resource('ec2', region_name=region, aws_access_key_id= access_key,aws_secret_access_key= secret_key)
# create an instance
instance = ec2.create_instances(
ImageId = ami,
MinCount = 1,
MaxCount = {specify max instances needed here},
InstanceType = type,
KeyName = key_name,
SubnetId = subnet)

instance.wait_until_running()
print("Instance Up and Running")