gpt4 book ai didi

amazon-web-services - AWS Fargate - 如何使用 python boto3 获取任务的公共(public) IP 地址

转载 作者:行者123 更新时间:2023-12-04 01:20:58 24 4
gpt4 key购买 nike

我正在使用以下 python 脚本创建新的 fargate 任务。

import boto3
import json


def handler():
client = boto3.client('ecs')
response = client.run_task(
cluster='fargate-learning', # name of the cluster
launchType = 'FARGATE',
taskDefinition='fargate-learning:1', # replace with your task definition name and revision
count = 1,
platformVersion='LATEST',
networkConfiguration={
'awsvpcConfiguration': {
'subnets': [
'subnet-0a024d8ac87668b64', # replace with your public subnet or a private with NAT
],
'assignPublicIp': 'ENABLED'
}
})

print(response)
return str(response)


if __name__ == '__main__':
handler()
这是我从 boto3 得到的回应。
https://jsonblob.com/5faf3ae6-bc31-11ea-8cae-53bd90c38587
尽管脚本正在分配公共(public) IP 地址并且我可以在网站上看到它,但我看不到公共(public) IP 地址作为响应。
aws fargate ip address
那么,如何使用 boto3 获取这个公共(public) IP 地址呢?
谢谢

最佳答案

这可以在 中完成两步 :

  • 使用describe_tasks获取与您的 Fargate 关联的 ENI id awsvpc界面。 eni 的值,例如eni-0c866df3faf8408d0 , 将在 attachments 中给出和 details从调用的结果。
  • 一旦你有了eni,你就可以使用EC2.NetworkInterface .例如:
  • eni_id = 'eni-0c866df3faf8408d0' # from step 1

    eni = boto3.resource('ec2').NetworkInterface(eni_id)

    print(eni.association_attribute['PublicIp'])

    关于amazon-web-services - AWS Fargate - 如何使用 python boto3 获取任务的公共(public) IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62690894/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com