【发布时间】:2021-07-01 14:56:57
【问题描述】:
有没有办法通过python代码使用boto3获取aws账户中的所有资源。我浏览了文档,没有找到任何可以解决这个问题的列表函数。
【问题讨论】:
-
您还可以查看各种 GitHub 项目,例如JohannesEbke/aws_list_all.
标签: python amazon-web-services boto3
有没有办法通过python代码使用boto3获取aws账户中的所有资源。我浏览了文档,没有找到任何可以解决这个问题的列表函数。
【问题讨论】:
标签: python amazon-web-services boto3
试试这个命令。
但前提条件是在运行此命令之前为此区域启用aws config。
导入 boto3 session = boto3.Session(profile_name='your-profilename')client = session.client('config')resources = [“AWS::EC2::CustomerGateway”,”AWS::EC2::EIP”,”AWS: :EC2::Host”,”AWS::EC2::Instance”,”AWS::EC2::InternetGateway”,”AWS::EC2::NetworkAcl”,”AWS::EC2::NetworkInterface”,”AWS: :EC2::RouteTable”,”AWS::EC2::SecurityGroup”,”AWS::EC2::Subnet”,”AWS::CloudTrail::Trail”,”AWS::EC2::Volume”,”AWS: :EC2::VPC”,”AWS::EC2::VPNConnection”,”AWS::EC2::VPNGateway”,”AWS::EC2::RegisteredHAInstance”,”AWS::EC2::NatGateway”,”AWS: :EC2::EgressOnlyInternetGateway”,”AWS::EC2::VPCEndpoint”,”AWS::EC2::VPCEndpointService”,”AWS::EC2::FlowLog”,”AWS::EC2::VPCPeeringConnection”,”AWS: :IAM::Group”,”AWS::IAM::Policy”,”AWS::IAM::Role”,”AWS::IAM::User”,”AWS::ElasticLoadBalancingV2::LoadBalancer”,”AWS: :ACM::Certificate”,”AWS::RDS::DBInstance”,”AWS::RDS::DBParameterGroup”,”AWS::RDS::DBOptionGroup”,”AWS::RDS::DBSubnetGroup”,”AWS: :RDS::DBSecurityGroup”,”AWS::RDS::DBSnapshot”,”AWS::RDS::DBCluster”,”AWS::RDS::DBClusterParameterGroup”,”AWS: :RDS::DBClusterSnapshot”,”AWS::RDS::EventSubscription”,”AWS::S3::Bucket”,”AWS::S3::AccountPublicAccessBlock”,”AWS::Redshift::Cluster”,”AWS: :Redshift::ClusterSnapshot”,”AWS::Redshift::ClusterParameterGroup”,”AWS::Redshift::ClusterSecurityGroup”,”AWS::Redshift::ClusterSubnetGroup”,”AWS::Redshift::EventSubscription”,”AWS: :SSM::ManagedInstanceInventory”,”AWS::CloudWatch::Alarm”,”AWS::CloudFormation::Stack”,”AWS::ElasticLoadBalancing::LoadBalancer”,”AWS::AutoScaling::AutoScalingGroup”,”AWS: :AutoScaling::LaunchConfiguration”,”AWS::AutoScaling::ScalingPolicy”,”AWS::AutoScaling::ScheduledAction”,”AWS::DynamoDB::Table”,”AWS::CodeBuild::Project”,”AWS: :WAF::RateBasedRule”,”AWS::WAF::Rule”,”AWS::WAF::RuleGroup”,”AWS::WAF::WebACL”,”AWS::WAFRegional::RateBasedRule”,”AWS: :WAFRegional::Rule”,”AWS::WAFRegional::RuleGroup”,”AWS::WAFRegional::WebACL”,”AWS::CloudFront::Distribution”,”AWS::CloudFront::StreamingDistribution”,”AWS: :Lambda::Alias”,”AWS::Lambda::Function”,”AWS::ElasticBeanstalk::Application”,” AWS::ElasticBeanstalk::ApplicationVersion”,”AWS::ElasticBeanstalk::Environment”,”AWS::MobileHub::Project”,”AWS::XRay::EncryptionConfig”,”AWS::SSM::AssociationCompliance”,” AWS::SSM::PatchCompliance”,”AWS::Shield::Protection”,”AWS::ShieldRegional::Protection”,”AWS::Config::ResourceCompliance”,”AWS::LicenseManager::LicenseConfiguration”,” AWS::ApiGateway::DomainName”,”AWS::ApiGateway::Method”,”AWS::ApiGateway::Stage”,”AWS::ApiGateway::RestApi”,”AWS::ApiGatewayV2::DomainName”,” AWS::ApiGatewayV2::Stage”,”AWS::ApiGatewayV2::Api”,”AWS::CodePipeline::Pipeline”,”AWS::ServiceCatalog::CloudFormationProvisionedProduct”,”AWS::ServiceCatalog::CloudFormationProduct”,” AWS::ServiceCatalog::Portfolio”]for resource in resources:response = client.list_discovered_resources(resourceType=resource)print('#################### {} ################'.format(resource))for i in range(len(response['resourceIdentifiers'])-1):print( '{} , {} '.format(response['resourceIdentifiers'][i]['resourceType'],response['resource标识符'][i]['resourceId']))
【讨论】:
# encoding=utf8 只需将 PEP-263 编码注释放在文件顶部即可:,有帮助吗?
在 boto3 中,您可以使用 ResourceGroupsTaggingAPI 方法 get_resources()。主要用于根据标签获取资源,但您可以将标签过滤参数留空并获取所有支持的资源。 考虑到并非所有资源都包括在内,并且仅限于特定区域,但我希望它可以帮助您。
例子:
获取所有资源:
import boto3
client = boto3.client('resourcegroupstaggingapi')
client.get_resources()
获取特定服务类型的资源:
import boto3
client = boto3.client('resourcegroupstaggingapi')
client.get_resources(
ResourceTypeFilters=[
'ec2:instance'
])
官方文档:
【讨论】:
根据奥马尔给出的答案,我得出以下结论:
from pprint import pprint
import boto3
from botocore.exceptions import ClientError
client = boto3.client('resourcegroupstaggingapi', )
regions = boto3.session.Session().get_available_regions('ec2')
for region in regions:
print(region)
try:
client = boto3.client('resourcegroupstaggingapi', region_name=region)
pprint([x.get('ResourceARN') for x in client.get_resources().get('ResourceTagMappingList')])
except ClientError as e:
print(f'Could not connect to region with error: {e}')
print()
这将循环大多数区域并列出该区域中的所有 ARN,如下所示:
eu-north-1
[]
eu-west-1
['arn:aws:mq:eu-west-1:xxxxxxxxxxxx:broker:example:b-125099aa-8e22-462e-a8e9-bcc6b29c010a']
eu-west-2
[]
eu-west-3
[]
【讨论】: