Launch AWS EC2 Instances using python script

In this blog we have to create script to launch ec2 instances using python script, same thing can we be done by AWS console or AWS CLI or AWS SDK can check here, now we are using AWS SDK(boto3) to launch instances

  • An AWS account with an IAM User and EC2 Access with access and secrete keys
  • Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
import boto3
  • We will invoke the client for EC2
client = boto3.client('ec2')
  • To launch EC2 instances we have to use method “run_instances()”. This method helps us launch AWS EC2 instances based on our requirement.
response =client.run_instances(<arguments>)
  • Go here where you will find all arguments list.

Note:- Arguments which are with “REQUIRED” tags mentioned in documentation is mandatory

resp=client.run_instances(ImageId='ami-0e6329e222e662a52',
InstanceType='t2.micro',
MinCount=2,
MaxCount=2,
KeyName='keyname',
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [{'Key':'Name','Value':'Linux Server'},
{'Key': 'Env','Value': 'Dev'}]
},
],
)
  1. MaxCount (integer) — [REQUIRED] — The maximum number of instances to launch.

2. MinCount (integer) — [REQUIRED] — The minimum number of instances to launch.

3. InstanceType (string) — Specifies the instance type to be supported by the Dedicated Hosts. If you specify an instance type, the Dedicated Hosts support instances of the specified instance type only.

4. ImageId (string) — The ID of the AMI. An AMI is required to launch an instance. The AMI ID must be specified here

5. KeyName (string) — The name of the key pair. You can create a key pair using CreateKeyPair or ImportKeyPair .

6. TagSpecifications (list) — The tags to apply to the resources that are created during instance launch.

7. ResourceType (string) — The type of resource, for example a VPC attachment.

  • Once above method will run it will launch EC2 and launched EC2 information will be captured in variable “resp”. It will return infomation in dictonary, so “resp” would be a dictonary.
  • Now we will traverse the dict using for loop to print list of instances launched by “run_instances” method.
for i in resp['Instances']:
print("Instance ID Created is :{} Instance Type Created is : {}" .format(i['InstanceId'],i['InstanceType']))

Entire Code is listed below

  • Change file mode to execute file
chmod 700 create-instances.py
  • Run the script to launch instances, make sure AWS Configure setup is done before running.
./create-instances.py
  • Can run below command to verify whether instances are running or can verify from AWS console
aws ec2 describe-instances \
--query "Reservations[*].Instances[*]" --filters "Name=instance-state-name,Values=running" \
--output json

Thanks for Reading !!

Keep Learning !! Keep Sharing !!

You can contact me on:

LinkedIn emailme

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vinodha kumara

DevSecOps, MLOps, Cloud Computing, DE, ML, DL, Programmer, Blogger, Volunteer