Python code for launch EC2 instance in AWS

 Here is an example of how you can launch an EC2 instance in AWS using the boto3 library in Python


import boto3

# Create an EC2 client
ec2 = boto3.client('ec2')

# Specify the ID of the AMI for the instance
ami_id = 'ami-0c94855ba95c71c99'

# Specify the instance type
instance_type = 't2.micro'

# Launch the instance
response = ec2.run_instances(ImageId=ami_id,
                             InstanceType=instance_type,
                             MinCount=1,
                             MaxCount=1)

# Print the instance ID
print(response['Instances'][0]['InstanceId'])



library and create an EC2 client. Then, we specify the ID of the Amazon Machine Image (AMI) that we want to use for the instance, as well as the instance type (in this case, a t2.micro instance). Finally, we use the run_instances method to launch the instance and print the ID of the new instance.


You need to have AWS credentials and configuration(profile) set in your local machine or pass them as parameter while creating the client.
Also the above code is just an example and you need to modify it according to your specific requirement before running it.

Comments