Try to search your question here, if you can't find : Ask Any Question Now ?

Switch AWS Accounts in Python – EC2 Object has no attribute 'instances' error

HomeCategory: stackoverflowSwitch AWS Accounts in Python – EC2 Object has no attribute 'instances' error
Avatarcraig asked 5 months ago

I’m trying to list the EC2 instances in a specific AWS account using named profiles and boto3 in Python.

The error says:

File ".aws_ec2_list_instance_info.py", line 18, in <module>
    running_instances = ec2.instances.filter(Filters=[{
  File "C:UserstdunphyAppDataLocalProgramsPythonPython37-32libsite-packagesbotocoreclient.py", line 601, in __getattr__
    self.__class__.__name__, item)
AttributeError: 'EC2' object has no attribute 'instances'

This is my code:

from collections import defaultdict
import boto3
aws_account = input("Enter the name of the AWS account you'll be working in: ")
# Connect to EC2
session = boto3.Session(profile_name=aws_account)
ec2 = session.client('ec2')
# Get information for all running instances
running_instances = ec2.instances.filter(Filters=[{
    'Name': 'instance-state-name',
    'Values': ['running']}])

What am I doing wrong?

1 Answers
Best Answer
AvatarMannu answered 5 months ago
Your Answer

8 + 18 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com