简体   繁体   English

如何使用 python 获取 AWS 实例总大小 memory?

[英]How to get AWS instance total memory size using python?

I want to run a python startup script inside AWS instance in which I want to get the total memory size selected during instance creation.我想在 AWS 实例中运行一个 python 启动脚本,我想在其中获得实例创建期间选择的总大小 memory。

I have tried free -h , grep MemTotal /proc/meminfo commands.我试过free -h , grep MemTotal /proc/meminfo命令。 But, the problem with these commands is, I get some less amount of RAM than the actual memory size I selected while creating an instance (due to system use maybe).但是,这些命令的问题是,我获得的 RAM 数量比我在创建实例时选择的实际 memory 大小要少(可能是由于系统使用)。 I want to get the exact memory size I selected while creating an instance.我想获得我在创建实例时选择的确切大小 memory 。 eg, "2 GB" for "t2.small", "4 GB" for "c5.large",etc.例如,“t2.small”为“2 GB”,“c5.large”为“4 GB”等。

Also, there is no metadata URL available to get AWS instance memory size.此外,没有元数据 URL 可用于获取 AWS 实例 memory 大小。

Is there any way to do that?有什么办法吗?

I haven't done this myself, but I think the process might be something like:我自己没有这样做过,但我认为这个过程可能是这样的:

client = boto3.client('ec2') client = boto3.client('ec2')

type = client.describe_instance_attribute(Attribute='instanceType', InstanceId='YOUR_ID' ) type = client.describe_instance_attribute(Attribute='instanceType', InstanceId='YOUR_ID' )

details = client.describe_instance_types( InstanceTypes=[ type ] ) details = client.describe_instance_types(实例类型=[类型])

memory = details['InstanceTypes']['MemoryInfo']['SizeInMiB'] memory = 详细信息['InstanceTypes']['MemoryInfo']['SizeInMiB']

You'll need to give the instance the right IAM permissions to get the data (it's not the same as the http://169.254.169.254 metadata).您需要为实例授予正确的 IAM 权限以获取数据(它与http://169.254.169.254元数据不同)。

You can also get the instance type from http://169.254.169.254/2020-10-27/meta-data/instance-type/ but I'm trying to go for a fully python solution with boto3.您还可以从http://169.254.169.254/2020-10-27/meta-data/instance-type/获取实例类型,但我正在尝试使用 go 获取带有 boto3 的完整 python 解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python boto3 获取 AWS 的 S3(所有存储桶)中每个 object 的大小 - How to get the size of each object in S3 (all buckets) for an AWS using python boto3 如何使用 spring 引导或任何 aws 端点获取 AWS EC2 实例名称 - How to get the AWS EC2 Instance name using spring boot or any aws endpoint 如何获取实例中的 AWS OpsWorks 实例 ID? - How to get AWS OpsWorks instance id within an instance? 如何获取使用 AWS CLI 手动创建的 RDS 数据库实例的所有快照的名称? - How can I get the name of the all snapshot of RDS DB instance created manually using the AWS CLI? 如何在aws中使用php获取EC2实例可用区和id? - How to get EC2 instance availability zone and id using php in aws? 如何在 Python 中使用 Boto3 AWS Lambda 获取 aws cloudwatch 指标统计信息? - How to get aws cloudwatch metrics statistics using Boto3 AWS Lambda in Python? 如何从 AWS Glue Python Shell 连接到 RDS 实例? - How to Connect to RDS Instance from AWS Glue Python Shell? 如何在 AWS EC2 实例上安装 Python 3? - How do I install Python 3 on an AWS EC2 instance? 如何在 Terraform 中获取 AWS RDS 编写器实例 - How I can get the AWS RDS writer instance in Terraform 如何以编程方式获取实例类型的内存/cpu 值? - How to get memory/cpu values for instance types programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM