AWS JAVA SDK 获取 public 任务的 IP
AWS JAVA SDK get public IP of Task
我有个小问题。我正在通过 JAVA SDK 启动任务(docker 容器)。这很好用。
但是现在想通过SDK获取publicIP,不知道怎么获取。
这是我目前已有的代码。
RunTaskRequest request = new RunTaskRequest()
.withCluster("JuiceShop")
.withTaskDefinition("startJuiceShop:1")
.withNetworkConfiguration(networkConfiguration)
.withLaunchType("FARGATE");
RunTaskResult response = client.runTask(request);
响应包含容器,但网络设备尚未连接。有没有简单的方法来获取 public IPV4?
task metadata endpoint 可让您获取有关容器的网络信息。如果您要附加 EIP,则必须使用容器中的 IPv4 才能通过 SDK 找到 EIP。
您需要进行多次 AWS API 调用才能获得 Public IPv4 地址。以下是步骤。
- 一旦你执行了taskRun操作。从输出中保留 taskFullArn。
- 使用上面的taskArn和集群名称,进行describeTasks操作调用。
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ecs/AmazonECS.html#describeTasks-com.amazonaws.services.ecs.model.DescribeTasksRequest-.
示例 -
AmazonECS client = AmazonECSClientBuilder.standard().build();
DescribeTasksRequest request = new DescribeTasksRequest().withTasks("c5cba4eb-5dad-405e-96db-71ef8eefe6a8");
DescribeTasksResult response = client.describeTasks(request);
- 上面 API 会给你一个包含网络附件详细信息的回复。
"attachments": [
{
"id": "xxxxx-d02c-4a9d-ae79-xxxxxxx",
"type": "ElasticNetworkInterface",
"status": "ATTACHED",
"details": [
{
"name": "subnetId",
"value": "subnet-xxxxx"
},
{
"name": "networkInterfaceId",
"value": "eni-e5aa89a3"
},
{
"name": "macAddress",
"value": "xxxxx"
},
{
"name": "privateIPv4Address",
"value": "172.31.94.215"
}
]
}
],
- 从上面的 API 响应中获取 networkInterfaceId 并进行以下调用。
- 调用 AWS EC2 describeNetworkInterfaces。
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/AmazonEC2Client.html#describeNetworkInterfaces-com.amazonaws.services.ec2.model.DescribeNetworkInterfacesRequest-
示例 -
AmazonEC2 client = AmazonEC2ClientBuilder.standard().build();
DescribeNetworkInterfacesRequest request = new DescribeNetworkInterfacesRequest().withNetworkInterfaceIds("eni-e5aa89a3");
DescribeNetworkInterfacesResult response = client.describeNetworkInterfaces(request);
- 上面应该给 DescricerNetworkInterfaceResult 和容器的 PublicIp。
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/model/NetworkInterfaceAssociation.html#getPublicIp--
{
"NetworkInterfaces": [
{
"Association": {
"IpOwnerId": "amazon",
"PublicDnsName": "ec2-52-xx-xx-xx.compute-1.amazonaws.com",
"PublicIp": "52.xx.xx.xx"
}
] }
- 注意 - 您需要执行步骤 2 直到任务完成并且 运行,否则您将无法获得所需的结果。所以可能,在 runTask 之后休眠几秒钟,看看 Task 是否启动,然后 运行 然后执行剩余的步骤。
我有个小问题。我正在通过 JAVA SDK 启动任务(docker 容器)。这很好用。
但是现在想通过SDK获取publicIP,不知道怎么获取。
这是我目前已有的代码。
RunTaskRequest request = new RunTaskRequest()
.withCluster("JuiceShop")
.withTaskDefinition("startJuiceShop:1")
.withNetworkConfiguration(networkConfiguration)
.withLaunchType("FARGATE");
RunTaskResult response = client.runTask(request);
响应包含容器,但网络设备尚未连接。有没有简单的方法来获取 public IPV4?
task metadata endpoint 可让您获取有关容器的网络信息。如果您要附加 EIP,则必须使用容器中的 IPv4 才能通过 SDK 找到 EIP。
您需要进行多次 AWS API 调用才能获得 Public IPv4 地址。以下是步骤。
- 一旦你执行了taskRun操作。从输出中保留 taskFullArn。
- 使用上面的taskArn和集群名称,进行describeTasks操作调用。 https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ecs/AmazonECS.html#describeTasks-com.amazonaws.services.ecs.model.DescribeTasksRequest-.
示例 -
AmazonECS client = AmazonECSClientBuilder.standard().build();
DescribeTasksRequest request = new DescribeTasksRequest().withTasks("c5cba4eb-5dad-405e-96db-71ef8eefe6a8");
DescribeTasksResult response = client.describeTasks(request);
- 上面 API 会给你一个包含网络附件详细信息的回复。
"attachments": [ { "id": "xxxxx-d02c-4a9d-ae79-xxxxxxx", "type": "ElasticNetworkInterface", "status": "ATTACHED", "details": [ { "name": "subnetId", "value": "subnet-xxxxx" }, { "name": "networkInterfaceId", "value": "eni-e5aa89a3" }, { "name": "macAddress", "value": "xxxxx" }, { "name": "privateIPv4Address", "value": "172.31.94.215" } ] } ],
- 从上面的 API 响应中获取 networkInterfaceId 并进行以下调用。
- 调用 AWS EC2 describeNetworkInterfaces。 https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/AmazonEC2Client.html#describeNetworkInterfaces-com.amazonaws.services.ec2.model.DescribeNetworkInterfacesRequest-
示例 -
AmazonEC2 client = AmazonEC2ClientBuilder.standard().build();
DescribeNetworkInterfacesRequest request = new DescribeNetworkInterfacesRequest().withNetworkInterfaceIds("eni-e5aa89a3");
DescribeNetworkInterfacesResult response = client.describeNetworkInterfaces(request);
- 上面应该给 DescricerNetworkInterfaceResult 和容器的 PublicIp。 https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/model/NetworkInterfaceAssociation.html#getPublicIp--
{ "NetworkInterfaces": [ { "Association": { "IpOwnerId": "amazon", "PublicDnsName": "ec2-52-xx-xx-xx.compute-1.amazonaws.com", "PublicIp": "52.xx.xx.xx" } ] }
- 注意 - 您需要执行步骤 2 直到任务完成并且 运行,否则您将无法获得所需的结果。所以可能,在 runTask 之后休眠几秒钟,看看 Task 是否启动,然后 运行 然后执行剩余的步骤。