[英]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.