How to create an Ec2 instance using Python boto3?

+1 vote
I want to create an Ec2 instance using Python boto3?

Can anyone help me with the code?
Dec 3, 2018 in AWS by Nitesh
• 3,020 points

1 answer to this question.

0 votes

You can use the following code, it worked for me perfectly.

import boto3
ec2 = boto3.resource('ec2')
instance = ec2.create_instances(
    ImageId = 'ami-009d6802948d06e52',
    MinCount = 1,
    MaxCount = 1,
    InstanceType = 't2.micro',
    KeyName = 'nabarupa',
    SubnetId = 'subnet-0yhg678990d56c277')
print (instance[0].id)

Hope this helps.

answered Dec 3, 2018 by Nabarupa
this step is very helpfull thankyou ,

but in lambda it showing following error:

Execution result: failed

  "errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'",
  "errorType": "Runtime.HandlerNotFound"

but ec2 is creating but showing above msg..please kindly help

You need to define a lambda_handler function in your code. The code is missing the function named lambda_handler.

"errorMessage": "2020-08-30T05:17:17.365Z d6deccb1-bdb1-4bab-859a-766e3a793d18 Task timed out after 3.00 seconds"
START RequestId: 31379099-a659-48fe-be16-be1511599616 Version: $LATEST
module initialization error: An error occurred (UnauthorizedOperation) when calling the RunInstances operation: You are not authorized to perform this operation. Encoded authorization failure message: cjw_mA7MY5Y7RLlOKo-ABM1f_7L1LR8zcjv8Woa9-wPBOZlSi_jySa6oGOhWVB_c2RFlUWrskj4LD2L57kFVZ5sH2eSx5qzZJJIrsLp3yBs9dHHYA3SBdkNjiBRVk0zOxbxRUsRD54QBpWKj6BxJactIGGeZ4YG6QGcokDzoqMZ_xPOmndjlUTxjW1bDEvf9VO13TnpqSrJrkWC05S4ycrEGVpyYhTl7q5cJrTmpbpxrdr2ZCNOLUjVMoahJ-e4aVKHGO1GgH4ISMSPOkC0kMeUpwWTRgWGDQF5gs1-Mv5_kWvNPZKtbF71TmDieWrxJ3fTJSxbiHGDqiAeIy3C8Oa7Wbr9C2ua7X3_lgSWEgMYVUMr58H2O3LMX1J0U3FmflZ573GHjhfRoXXZXwZT5rwcHO12gzWu582dNDNNoSUFw3uKqfmLB3dieYfAgExaliUZCUua3GuGHF2S21rndHRtZ1qsHv0Pe0NZKs4ztDn7f2M5v09Nr70VyReNP4uxz5ma4z_ZU-J3wAJL1OfFXYgNnxjjtv2fISF__ERUOauLQilYx4snR3Mjy3h_k6Q1FKoXGM-yaWB3vwD9K-7geZeJc3_JzWoHMukD4KUOrWi8k0fuJo2zZHeeg0spIKafCfbkjC1U0jErvnasHigTmaBhogaE6Kq1MAqo5YmZ9RS2uEWU
Hi@parvathi konda,

I think there is a problem with your AWS credentials. Check your credentials properly.

Related Questions In AWS

0 votes
1 answer

How to create an EC2 instance using Python?

Hey JunDevOps, Have a look these scripts: 1. ...READ MORE

answered Jun 24, 2019 in AWS by Aysha

edited Jun 25, 2019 4,560 views
0 votes
1 answer

How to Pass the VPC ID while creating the Ec2 instance in AWS using Python Boto3

import boto3 ec2 = boto3.resource('ec2') instance = ec2.create_instances( ...READ MORE

answered Jan 29, 2019 in AWS by Priyaj
• 57,660 points
+1 vote
2 answers
0 votes
1 answer

AWS S3 uploading hidden files by default

versioning is enabled in your bucket.….... the ...READ MORE

answered Oct 4, 2018 in AWS by Priyaj
• 57,660 points
0 votes
1 answer

How to decrypt the encrypted S3 file using aws-encryption-cli --decrypt

Use command : aws s3 presign s3://mybucket/abc_count.png you get ...READ MORE

answered Oct 22, 2018 in AWS by Priyaj
• 57,660 points
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,770 points
0 votes
2 answers
+1 vote
1 answer