AWS Fargate - 如何使用 python boto3 获取任务的 public ip 地址
AWS Fargate - How to get the public ip address of task by using python boto3
我正在使用以下 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 地址,我可以在网站上看到它。
那么,如何使用 boto3 获取此 public ip 地址?
谢谢
这可以通过 两个步骤完成:
使用 describe_tasks 获取与您的 fargate awsvpc
接口关联的 ENI id。 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'])
尝试将@Marcin 的答案作为一个函数来实现。希望这会有所帮助
def get_service_ips(cluster, tasks):
tasks_detail = ecs.describe_tasks(
cluster=cluster,
tasks=tasks
)
# first get the ENIs
enis = []
for task in tasks_detail.get("tasks", []):
for attachment in task.get("attachments", []):
for detail in attachment.get("details", []):
if detail.get("name") == "networkInterfaceId":
enis.append(detail.get("value"))
# now the ips
ips = []
for eni in enis:
eni_resource = boto3.resource("ec2").NetworkInterface(eni)
ips.append(eni_resource.association_attribute.get("PublicIp"))
return ips
作为要点here。
我正在使用以下 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 地址,我可以在网站上看到它。
那么,如何使用 boto3 获取此 public ip 地址?
谢谢
这可以通过 两个步骤完成:
使用 describe_tasks 获取与您的 fargate
awsvpc
接口关联的 ENI id。 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'])
尝试将@Marcin 的答案作为一个函数来实现。希望这会有所帮助
def get_service_ips(cluster, tasks):
tasks_detail = ecs.describe_tasks(
cluster=cluster,
tasks=tasks
)
# first get the ENIs
enis = []
for task in tasks_detail.get("tasks", []):
for attachment in task.get("attachments", []):
for detail in attachment.get("details", []):
if detail.get("name") == "networkInterfaceId":
enis.append(detail.get("value"))
# now the ips
ips = []
for eni in enis:
eni_resource = boto3.resource("ec2").NetworkInterface(eni)
ips.append(eni_resource.association_attribute.get("PublicIp"))
return ips
作为要点here。