使用Java监视EC2实例

时间:2015-12-13 10:55:32

标签: java amazon-web-services amazon-s3 amazon-ec2

我尝试使用此代码获取EC2实例说明:

public void testEC2ServiceInRegion() throws Exception
    {
        BasicAWSCredentials bawsc = new BasicAWSCredentials(
            "AKIAIUYDKF4K255DALPA", "Onv+nq3134kiLlKIb2H1JtIB7Y4QMEesh01Jl7tL");

        describeInstance(new AmazonEC2Client(bawsc), "i-02f5bd34");

    }

    private Instance describeInstance(AmazonEC2Client ec2, String ec2InstanceId)
    {
        Instance ec2Instance = null;
        {
            DescribeInstancesRequest request = new DescribeInstancesRequest();
            request.setInstanceIds(Collections.singletonList(ec2InstanceId));
            DescribeInstancesResult response = ec2.describeInstances(request);

            List<Reservation> reservations = response.getReservations();
            for (Reservation reservation : reservations)
            {
                for (Instance i : reservation.getInstances())
                {
                    if (ec2Instance != null)
                    {
                        throw new IllegalStateException();
                    }
                    ec2Instance = i;
                }
            }

            if (ec2Instance == null)
            {
                throw new IllegalStateException("EC2 instance not found: " + ec2InstanceId);
            }
        }
        return ec2Instance;
    }

但我收到错误

com.amazonaws.AmazonServiceException: The instance ID 'i-02f5bd34' does not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidInstanceID.NotFound; Request ID: 4079522f-7cae-4a58-877a-051abb5035a2)

你能帮我解决一下吗?

是否有任何可用于获取实例状态的示例?

0 个答案:

没有答案