Rak*_*ita 2 python amazon-web-services boto3
有没有办法使用boto3通过python代码获取aws帐户中的所有资源。我浏览了文档,没有找到任何可以解决此问题的列表函数。
试试这个命令。
\n但在运行此命令之前先启用aws config该区域。
import boto3\n\nsession = boto3.Session(profile_name=\xe2\x80\x99your-profilename\xe2\x80\x99) \nclient = session.client(\xe2\x80\x98config\xe2\x80\x99) \n\nresources = ["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"]\n\nfor resource in resources: \n response = client.list_discovered_resources(resourceType=resource)\n print(\xe2\x80\x98##################### {} #################\xe2\x80\x99.format(resource)) \n \n for i in range(len(response[\xe2\x80\x98resourceIdentifiers\xe2\x80\x99])):\n print( \xe2\x80\x98{} , {}\xe2\x80\x99.format(response[\xe2\x80\x98resourceIdentifiers\xe2\x80\x99][i][\xe2\x80\x98resourceType\xe2\x80\x99], response[\xe2\x80\x98resourceIdentifiers\xe2\x80\x99][i][\xe2\x80\x98resourceId\xe2\x80\x99]))\nRun Code Online (Sandbox Code Playgroud)\n
在 boto3 中,您可以使用 ResourceGroupsTaggingAPI 方法 get_resources()。主要用于根据标签获取资源,但可以将标签过滤参数留空并获取所有支持的资源。考虑到并非所有资源都包含在内,并且仅限于特定区域,但我希望它可以对您有所帮助。
例子:
获取所有资源:
import boto3
client = boto3.client('resourcegroupstaggingapi')
client.get_resources()
Run Code Online (Sandbox Code Playgroud)
获取特定服务类型的资源:
import boto3
client = boto3.client('resourcegroupstaggingapi')
client.get_resources(
ResourceTypeFilters=[
'ec2:instance'
])
Run Code Online (Sandbox Code Playgroud)
官方文档:
| 归档时间: |
|
| 查看次数: |
11250 次 |
| 最近记录: |