From 26a2fbaf24407f30e42ab8aba47b19ba026d21cd Mon Sep 17 00:00:00 2001 From: DiptoChakrabarty Date: Mon, 7 Sep 2020 15:00:23 +0530 Subject: [PATCH 1/2] created script to automate launch of mulitple ec2 instances --- Automation/src/multiple-ec2-instances/.env | 7 +++++ Automation/src/multiple-ec2-instances/ec2.py | 28 ++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 Automation/src/multiple-ec2-instances/.env create mode 100644 Automation/src/multiple-ec2-instances/ec2.py diff --git a/Automation/src/multiple-ec2-instances/.env b/Automation/src/multiple-ec2-instances/.env new file mode 100644 index 00000000..9ca2fcab --- /dev/null +++ b/Automation/src/multiple-ec2-instances/.env @@ -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}" diff --git a/Automation/src/multiple-ec2-instances/ec2.py b/Automation/src/multiple-ec2-instances/ec2.py new file mode 100644 index 00000000..c05cff7c --- /dev/null +++ b/Automation/src/multiple-ec2-instances/ec2.py @@ -0,0 +1,28 @@ +import boto3,os +from dotenv import load_dotenv +load_dotenv() + +#Load env variables +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") From c64d6f46760d561b2f81f924970f71ed8e94e300 Mon Sep 17 00:00:00 2001 From: Dipto Chakrabarty <45638240+DiptoChakrabarty@users.noreply.github.com> Date: Thu, 1 Oct 2020 00:30:07 +0530 Subject: [PATCH 2/2] add commands to run and steps to use --- Automation/src/multiple-ec2-instances/ec2.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Automation/src/multiple-ec2-instances/ec2.py b/Automation/src/multiple-ec2-instances/ec2.py index c05cff7c..140fe3fc 100644 --- a/Automation/src/multiple-ec2-instances/ec2.py +++ b/Automation/src/multiple-ec2-instances/ec2.py @@ -3,6 +3,8 @@ load_dotenv() #Load env variables +# 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") @@ -19,10 +21,12 @@ instance = ec2.create_instances( ImageId = ami, MinCount = 1, - MaxCount = {specify max instances needed here}, - InstanceType = type, - KeyName = key_name, + 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