//snippet-sourcedescription:[MonitorInstance.java demonstrates how to toggle detailed monitoring for an EC2 instance.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[ec2]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[2020-01-10]
//snippet-sourceauthor:[scmacdon]
/*
 * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.example.ec2;
// snippet-start:[ec2.java2.monitor_instance.complete]
// snippet-start:[ec2.java2.monitor_instance.import]
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.MonitorInstancesRequest;
import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesRequest;

// snippet-end:[ec2.java2.monitor_instance.import]
/**
 * Toggles detailed monitoring for an EC2 instance
 */
public class MonitorInstance {

    public static void main(String[] args) {
        final String USAGE =
                "To run this example, supply an instance id and a monitoring " +
                        "status\n" +
                        "Ex: MonitorInstance <instance-id> <true|false>\n";

        if (args.length != 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String instanceId = args[0];
        boolean monitor = Boolean.valueOf(args[1]);

        if (monitor) {
            monitorInstance(instanceId);
        } else {
            unmonitorInstance(instanceId);
        }
    }


    public static void monitorInstance(String instanceId) {
        // snippet-start:[ec2.java2.monitor_instance.main]
        Ec2Client ec2 = Ec2Client.create();

        MonitorInstancesRequest request = MonitorInstancesRequest.builder()
                .instanceIds(instanceId).build();

        ec2.monitorInstances(request);

        // snippet-end:[ec2.java2.monitor_instance.main]
        System.out.printf(
                "Successfully enabled monitoring for instance %s",
                instanceId);
    }

    public static void unmonitorInstance(String instanceId) {
        // snippet-start:[ec2.java2.monitor_instance.stop]
        Ec2Client ec2 = Ec2Client.create();

        UnmonitorInstancesRequest request = UnmonitorInstancesRequest.builder()
                .instanceIds(instanceId).build();

        ec2.unmonitorInstances(request);
        // snippet-end:[ec2.java2.monitor_instance.stop]
        System.out.printf(
                "Successfully disabled monitoring for instance %s",
                instanceId);
    }

}

// snippet-end:[ec2.java2.monitor_instance.complete]

@@ -89,32 +89,39 @@
 nitoring for an 
+Amazon 
 EC2 instance.%5D%0D%0A
@@ -210,18 +210,25 @@
 ervice:%5B
-ec
+Amazon EC
 2%5D%0D%0A//sn
@@ -287,16 +287,15 @@
 e:%5B2
-020-01-1
+/12/202
 0%5D%0D%0A
@@ -350,18 +350,8 @@
 ght 
-2010-2020 
 Amaz
@@ -948,62 +948,8 @@
 2;%0D%0A
-// snippet-start:%5Bec2.java2.monitor_instance.complete%5D
 %0D%0A//
@@ -1000,16 +1000,63 @@
 mport%5D%0D%0A
+import software.amazon.awssdk.regions.Region;%0D%0A
 import s
@@ -1254,18 +1254,16 @@
 quest;%0D%0A
-%0D%0A
 // snipp
@@ -1306,16 +1306,18 @@
 mport%5D%0D%0A
+%0D%0A
 /**%0D%0A * 
@@ -1883,24 +1883,212 @@
 rgs%5B1%5D);%0D%0A%0D%0A
+        //Create an Ec2Client object%0D%0A        Region region = Region.US_WEST_2;%0D%0A        Ec2Client ec2 = Ec2Client.builder()%0D%0A                .region(region)%0D%0A                .build();%0D%0A%0D%0A
         if (
@@ -2119,32 +2119,37 @@
 monitorInstance(
+ec2, 
 instanceId);%0D%0A  
@@ -2194,16 +2194,21 @@
 nstance(
+ec2, 
 instance
@@ -2266,32 +2266,48 @@
 monitorInstance(
+ Ec2Client ec2, 
 String instanceI
@@ -2376,55 +2376,8 @@
 n%5D%0D%0A
-        Ec2Client ec2 = Ec2Client.create();%0D%0A%0D%0A
     
@@ -2778,16 +2778,32 @@
 nstance(
+ Ec2Client ec2, 
 String i
@@ -2880,55 +2880,8 @@
 p%5D%0D%0A
-        Ec2Client ec2 = Ec2Client.create();%0D%0A%0D%0A
     
@@ -3253,60 +3253,4 @@
 %0A%7D%0D%0A
-%0D%0A// snippet-end:%5Bec2.java2.monitor_instance.complete%5D%0D%0A

Update MonitorInstance.java