Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created script to automate launch of mulitple ec2 instances #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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}"
32 changes: 32 additions & 0 deletions Automation/src/multiple-ec2-instances/ec2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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

# Add all these environment variables in your env file
# format of env file has been provided by adding an empty .env
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}, # here replace brancket with the number of instances you need
InstanceType = type,
KeyName = key_name,
SubnetId = subnet)

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

# To use the script run python3 ec2.py