如何获取使用boto3创建点实例的IP地址

发布于 2025-01-31 02:57:52 字数 2393 浏览 2 评论 0原文

我能够使用BOTO3使用以下代码创建斑点实例

import boto3
import datetime, random, string
client = boto3.client('ec2')
response = client.request_spot_instances(
    DryRun=False,
    ClientToken=''.join(random.choices(string.ascii_uppercase + string.digits, k=10)),
    InstanceCount=1,
    Type='one-time',
    LaunchSpecification={
        'ImageId': 'ami-053ebee0bc662f9b3',
        'SecurityGroups': ['default'],
        'InstanceType': 't3.medium',
        'Placement': {
            'AvailabilityZone': 'us-east-1a',
        },
        'BlockDeviceMappings': [
            {
            },
        ],
        'EbsOptimized': True,
        'Monitoring': {
            'Enabled': True
        },
        'SecurityGroupIds': [
            'sg-03432e1f',
        ]
    }
)
print(response)

,而我得到的响应在下面。如何获得创建的所有机器的IP地址。我知道该请求是异步的,但是如果我想对创建实例的细节进行轮询,那是什么?

{
  "SpotInstanceRequests": [
    {
      "CreateTime": "2022-05-22 05:21:54+00:00",
      "LaunchSpecification": {
        "SecurityGroups": [
          {
            "GroupName": "default",
            "GroupId": "sg-03432e1f"
          }
        ],
        "EbsOptimized": true,
        "ImageId": "ami-053ebee0bc662f9b3",
        "InstanceType": "t3.medium",
        "Placement": {
          "AvailabilityZone": "us-east-1a"
        },
        "SubnetId": "subnet-b9f4aadf",
        "Monitoring": {
          "Enabled": true
        }
      },
      "ProductDescription": "Linux/UNIX",
      "SpotInstanceRequestId": "sir-88tef9gh",
      "SpotPrice": "0.041600",
      "State": "open",
      "Status": {
        "Code": "pending-evaluation",
        "Message": "Your Spot request has been submitted for review, and is pending evaluation.",
        "UpdateTime": "2022-05-22 05:21:55+00:00"
      },
      "Type": "one-time",
      "InstanceInterruptionBehavior": "terminate"
    }
  ],
  "ResponseMetadata": {
    "RequestId": "ac39fdad-cb4d-4e26-82a5-1f55c1f0d5af",
    "HTTPStatusCode": 200,
    "HTTPHeaders": {
      "x-amzn-requestid": "ac39fdad-cb4d-4e26-82a5-1f55c1f0d5af",
      "cache-control": "no-cache, no-store",
      "strict-transport-security": "max-age=31536000; includeSubDomains",
      "content-type": "text/xml;charset=UTF-8",
      "content-length": "1696",
      "date": "Sun, 22 May 2022 05:21:54 GMT",
      "server": "AmazonEC2"
    },
    "RetryAttempts": 0
  }
}

I am able to create spot instance with boto3 with the following code

import boto3
import datetime, random, string
client = boto3.client('ec2')
response = client.request_spot_instances(
    DryRun=False,
    ClientToken=''.join(random.choices(string.ascii_uppercase + string.digits, k=10)),
    InstanceCount=1,
    Type='one-time',
    LaunchSpecification={
        'ImageId': 'ami-053ebee0bc662f9b3',
        'SecurityGroups': ['default'],
        'InstanceType': 't3.medium',
        'Placement': {
            'AvailabilityZone': 'us-east-1a',
        },
        'BlockDeviceMappings': [
            {
            },
        ],
        'EbsOptimized': True,
        'Monitoring': {
            'Enabled': True
        },
        'SecurityGroupIds': [
            'sg-03432e1f',
        ]
    }
)
print(response)

And the response I got is below. How can I get ip address of all the machines created. I know that the request is asynchronous but if I want to poll for the details of created instances, what is the way?

{
  "SpotInstanceRequests": [
    {
      "CreateTime": "2022-05-22 05:21:54+00:00",
      "LaunchSpecification": {
        "SecurityGroups": [
          {
            "GroupName": "default",
            "GroupId": "sg-03432e1f"
          }
        ],
        "EbsOptimized": true,
        "ImageId": "ami-053ebee0bc662f9b3",
        "InstanceType": "t3.medium",
        "Placement": {
          "AvailabilityZone": "us-east-1a"
        },
        "SubnetId": "subnet-b9f4aadf",
        "Monitoring": {
          "Enabled": true
        }
      },
      "ProductDescription": "Linux/UNIX",
      "SpotInstanceRequestId": "sir-88tef9gh",
      "SpotPrice": "0.041600",
      "State": "open",
      "Status": {
        "Code": "pending-evaluation",
        "Message": "Your Spot request has been submitted for review, and is pending evaluation.",
        "UpdateTime": "2022-05-22 05:21:55+00:00"
      },
      "Type": "one-time",
      "InstanceInterruptionBehavior": "terminate"
    }
  ],
  "ResponseMetadata": {
    "RequestId": "ac39fdad-cb4d-4e26-82a5-1f55c1f0d5af",
    "HTTPStatusCode": 200,
    "HTTPHeaders": {
      "x-amzn-requestid": "ac39fdad-cb4d-4e26-82a5-1f55c1f0d5af",
      "cache-control": "no-cache, no-store",
      "strict-transport-security": "max-age=31536000; includeSubDomains",
      "content-type": "text/xml;charset=UTF-8",
      "content-length": "1696",
      "date": "Sun, 22 May 2022 05:21:54 GMT",
      "server": "AmazonEC2"
    },
    "RetryAttempts": 0
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷夜 2025-02-07 02:57:52

与常规实例相同。您必须获得点networkInterfaceID,可以使用 descript_instances 。呼叫的结果为您提供InstanceLifeCycle等于spot用于点实例。

一旦拥有networkInterfaceID您的现场实例,您可以使用

The same as with regular instances. You have to get NetworkInterfaceId of the spot instances which you can get using describe_instances. The outcome of the call gives you InstanceLifecycle equal to spot for spot instances.

Once you have NetworkInterfaceId of your spot instances you can use describe_network_interfaces to get PublicIp associated with the interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文