//snippet-sourcedescription:[PutSubscriptionFilter.java demonstrates how to creates a CloudWatch logs subscription filter.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[cloudwatch]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[03/02/2020]
//snippet-sourceauthor:[scmacdon-aws]

/*
 * 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.cloudwatch;
// snippet-start:[cloudwatch.java2.put_subscription_filter.complete]
// snippet-start:[cloudwatch.java2.put_subscription_filter.import]
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
import software.amazon.awssdk.services.cloudwatchlogs.model.CloudWatchLogsException;
import software.amazon.awssdk.services.cloudwatchlogs.model.PutSubscriptionFilterRequest;
import software.amazon.awssdk.services.cloudwatchlogs.model.PutSubscriptionFilterResponse;
// snippet-end:[cloudwatch.java2.put_subscription_filter.import]

/**
 * Creates a CloudWatch Logs subscription filter.
 */
// snippet-start:[cloudwatch.java2.put_subscription_filter.main]
public class PutSubscriptionFilter {
    public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply:\n" +
                        "* a filter name\n" +
                        "* filter pattern\n" +
                        "* log group name\n" +
                        "* lambda function arn\n\n" +
                        "Ex: PutSubscriptionFilter <filter-name> \\\n" +
                        "                          <filter pattern> \\\n" +
                        "                          <log-group-name> \\\n" +
                        "                          <lambda-function-arn>\n";

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

        String filter = args[0];
        String pattern = args[1];
        String logGroup = args[2];
        String functionArn = args[3];

        try {
            CloudWatchLogsClient cwl = CloudWatchLogsClient.builder()
                    .build();

            PutSubscriptionFilterRequest request =
                    PutSubscriptionFilterRequest.builder()
                            .filterName(filter)
                            .filterPattern(pattern)
                            .logGroupName(logGroup)
                            .destinationArn(functionArn)
                            .build();

            PutSubscriptionFilterResponse response =
                    cwl.putSubscriptionFilter(request);

        } catch (CloudWatchLogsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }

        System.out.printf(
                "Successfully created CloudWatch logs subscription filter %s",
                filter);
    }
}
// snippet-end:[cloudwatch.java2.put_subscription_filter.main]
// snippet-end:[cloudwatch.java2.put_subscription_filter.complete]

@@ -208,22 +208,29 @@
 ervice:%5B
-c
+Amazon C
 loud
-w
+W
 atch%5D%0D%0A/
@@ -362,18 +362,8 @@
 ght 
-2010-2020 
 Amaz
@@ -969,76 +969,8 @@
 h;%0D%0A
-// snippet-start:%5Bcloudwatch.java2.put_subscription_filter.complete%5D
 %0D%0A//
@@ -1035,16 +1035,63 @@
 mport%5D%0D%0A
+import software.amazon.awssdk.regions.Region;%0D%0A
 import s
@@ -1561,72 +1561,8 @@
 */%0D%0A
-// snippet-start:%5Bcloudwatch.java2.put_subscription_filter.main%5D
 %0D%0Apu
@@ -1862,32 +1862,77 @@
 roup name%5Cn%22 +%0D%0A
+                        %22* a role arn %5Cn%22 +%0D%0A
                 
@@ -2190,32 +2190,103 @@
 -name%3E %5C%5C%5Cn%22 +%0D%0A
+                        %22                          %3Crole-arn%3E %5C%5C%5Cn%22 +%0D%0A
                 
@@ -2380,9 +2380,9 @@
  != 
-4
+5
 ) %7B%0D
@@ -2565,24 +2565,59 @@
 = args%5B2%5D;%0D%0A
+        String roleArn = args%5B3%5D;%0D%0A
         Stri
@@ -2634,25 +2634,25 @@
 nArn = args%5B
-3
+4
 %5D;%0D%0A%0D%0A      
@@ -2657,19 +2657,43 @@
     
-try %7B%0D%0A    
+Region region = Region.US_WEST_2;%0D%0A
     
@@ -2767,39 +2767,584 @@
             
-    .build();%0D%0A
+.region(region)%0D%0A                .build();%0D%0A%0D%0A        putSubFilters(cwl, filter, pattern, logGroup, roleArn, functionArn ) ;%0D%0A    %7D%0D%0A%0D%0A    // snippet-start:%5Bcloudwatch.java2.put_subscription_filter.main%5D%0D%0A    public static void putSubFilters(CloudWatchLogsClient cwl,%0D%0A                                     String filter,%0D%0A                                     String pattern,%0D%0A                                     String logGroup,%0D%0A                                     String roleArn,%0D%0A                                     String functionArn) %7B%0D%0A%0D%0A        try %7B
 %0D%0A          
@@ -3543,24 +3543,71 @@
 n(pattern)%0D%0A
+                            .roleArn(roleArn)%0D%0A
             
@@ -4229,72 +4229,4 @@
 n%5D%0D%0A
-// snippet-end:%5Bcloudwatch.java2.put_subscription_filter.complete%5D%0D%0A

Update PutSubscriptionFilter.java