【发布时间】:2021-04-15 22:22:04
【问题描述】:
boto3 中是否有任何函数可以判断 IMDSV2 是否启用?
【问题讨论】:
标签: amazon-web-services amazon-ec2 boto3
boto3 中是否有任何函数可以判断 IMDSV2 是否启用?
【问题讨论】:
标签: amazon-web-services amazon-ec2 boto3
import boto3
ec2 = boto3.client('ec2')
instance_id = 'i-xxx'
res = ec2.describe_instances(InstanceIds=[instance_id])
metadata_options = res['Reservations'][0]['Instances'][0]['MetadataOptions']
http_tokens = metadata_options['HttpTokens']
state = metadata_options['State']
if http_tokens == 'required' and state == 'applied':
print(f"IMDSv2 is enforced in the EC2 instance with id {instance_id}")
else:
print(f"IMDSv2 is not enforced")
print(http_tokens, state)
【讨论】: