• 沒有找到結果。

Cancel an export task

在文檔中 Amazon CloudWatch Logs (頁 150-200)

"completionTime": 1441498600000 "creationTime": 1441495400000 },

"from": 1441490400000,

"logGroupName": "my-log-group", "status": {

"code": "FAILED", "message": "FAILED"

},

"taskId": "cda45419-90ea-4db5-9833-aade86253e66", "taskName": "my-log-group-09-10-2015",

"to": 1441494000000 }]

}

Step 6: Cancel an export task

You can cancel an export task if it is in either the PENDING or the RUNNING state.

To cancel an export task using the AWS CLI

At a command prompt, use the following cancel-export-task command:

aws logs --profile CWLExportUser cancel-export-task --task-id "cda45419-90ea-4db5-9833-aade86253e66"

You can use the describe-export-tasks command to verify that the task was canceled successfully.

Prerequisites

Streaming CloudWatch Logs data to Amazon OpenSearch Service

You can configure a CloudWatch Logs log group to stream data it receives to your Amazon OpenSearch Service cluster in near real-time through a CloudWatch Logs subscription. For more information, see Real-time processing of log data with subscriptions (p. 94).

Depending on the amount of log data being streamed, you might want to set a function-level concurrent execution limit on the function. For more information, see Lambda function scaling.

NoteStreaming large amounts of CloudWatch Logs data to OpenSearch might result in high usage charges. We recommend that you create a Budget in the Billing and Cost Management console.

For more information, see Managing your costs with AWS Budgets.

Prerequisites

Before you begin, create an OpenSearch domain. The domain can have either public access or VPC access, but you can't then modify the type of access after the domain is created. You might want to review your OpenSearch domain settings later, and modify your cluster configuration based on the amount of data your cluster will be processing. For instructions to create a domain, see Creating OpenSearch Service domains.

For more information about OpenSearch, see the Amazon OpenSearch Service Developer Guide.

Subscribe a log group to OpenSearch

You can use the CloudWatch console to subscribe a log group to OpenSearch.

To subscribe a log group to OpenSearch

1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.

2. In the navigation pane, choose Log groups.

3. Select the name of the log group.

4. Choose Actions, Create OpenSearch subscription filter.

5. Choose whether you want to stream to a cluster in this account or another account.

• If you chose this account, select the domain you created in the previous step.

• If you chose another account, provide the domain ARN and endpoint.

6. For Lambda IAM Execution Role, choose the IAM role that Lambda should use when executing calls to OpenSearch.

The IAM role you choose must fulfill these requirements:

• It must have lambda.amazonaws.com in the trust relationship.

• It must include the following policy:

Subscribe a log group to OpenSearch

{ "Version": "2012-10-17", "Statement": [

{

"Action": [ "es:*"

],

"Effect": "Allow",

"Resource": "arn:aws:es:region:account-id:domain/target-domain-name/*"

} ] }

• If the target OpenSearch domain uses VPC access, the role must have the

AWSLambdaVPCAccessExecutionRole policy attached. This Amazon-managed policy grants Lambda access to the customer's VPC, enabling Lambda to write to the OpenSearch endpoint in the VPC.

7. For Log format, choose a log format.

8. For Subscription filter pattern, type the terms or pattern to find in your log events. This ensures that you send only the data you're interested in to your OpenSearch cluster. For more information, see Creating metrics from log events using filters (p. 73).

9. (Optional) For Select log data to test, select a log stream and then choose Test pattern to verify that your search filter is returning the results you expect.

10. Choose Start streaming.

Actions

Code examples for CloudWatch Logs

The following code examples show how to use CloudWatch Logs with an AWS software development kit (SDK).

The examples are divided into the following categories:

Actions

Code excerpts that show you how to call individual service functions.

Cross-service examples

Sample applications that work across multiple AWS services.

For a complete list of AWS SDK developer guides and code examples, see Using CloudWatch Logs with an AWS SDK (p. 33). This topic also includes information about getting started and details about previous SDK versions.

Code examples

• Actions for CloudWatch Logs (p. 147)

• Associate an AWS KMS key to a CloudWatch Logs log group using an AWS SDK (p. 148)

• Cancel a CloudWatch Logs export task using an AWS SDK (p. 149)

• Create a CloudWatch Logs log group using an AWS SDK (p. 149)

• Create a CloudWatch Logs log stream using an AWS SDK (p. 150)

• Create a CloudWatch Logs subscription filter using an AWS SDK (p. 151)

• Delete a CloudWatch Logs log group using an AWS SDK (p. 154)

• Delete a CloudWatch Logs subscription filter using an AWS SDK (p. 154)

• Describe CloudWatch Logs subscription filters using an AWS SDK (p. 157)

• Describe CloudWatch Logs export tasks using an AWS SDK (p. 161)

• Describe CloudWatch Logs log groups using an AWS SDK (p. 162)

• Cross-service examples for CloudWatch Logs (p. 163)

• Use scheduled events to invoke a Lambda function (p. 163)

Actions for CloudWatch Logs

The following code examples demonstrate how to perform individual CloudWatch Logs actions with AWS SDKs. These excerpts call the CloudWatch Logs API and are not intended to be run in isolation. Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

The following examples include only the most commonly used actions. For a complete list, see the CloudWatch Logs API Reference.

Examples

• Associate an AWS KMS key to a CloudWatch Logs log group using an AWS SDK (p. 148)

• Cancel a CloudWatch Logs export task using an AWS SDK (p. 149)

• Create a CloudWatch Logs log group using an AWS SDK (p. 149)

• Create a CloudWatch Logs log stream using an AWS SDK (p. 150)

• Create a CloudWatch Logs subscription filter using an AWS SDK (p. 151)

Associate an AWS KMS key to log group

• Delete a CloudWatch Logs log group using an AWS SDK (p. 154)

• Delete a CloudWatch Logs subscription filter using an AWS SDK (p. 154)

• Describe CloudWatch Logs subscription filters using an AWS SDK (p. 157)

• Describe CloudWatch Logs export tasks using an AWS SDK (p. 161)

• Describe CloudWatch Logs log groups using an AWS SDK (p. 162)

Associate an AWS KMS key to a CloudWatch Logs log group using an AWS SDK

The following code example shows how to associate an AWS KMS key with an existing CloudWatch Logs log group.

.NET

AWS SDK for .NET

public static async Task Main() {

// This client object will be associated with the same AWS Region // as the default user on this system. If you need to use a // different AWS Region, pass it as a parameter to the client // constructor.

var client = new AmazonCloudWatchLogsClient();

string kmsKeyId = "arn:aws:kms:us-west-2:<account-number>:key/7c9eccc2-38cb-4c4f-9db3-766ee8dd3ad4";

string groupName = "cloudwatchlogs-example-loggroup";

var request = new AssociateKmsKeyRequest {

KmsKeyId = kmsKeyId, LogGroupName = groupName, };

var response = await client.AssociateKmsKeyAsync(request);

if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) {

Console.WriteLine($"Successfully associated KMS key ID: {kmsKeyId}

with log group: {groupName}.");

} else {

Console.WriteLine("Could not make the association between:

{kmsKeyId} and {groupName}.");

} }

• Find instructions and more code on GitHub.

• For API details, see AssociateKmsKey in AWS SDK for .NET API Reference.

For a complete list of AWS SDK developer guides and code examples, see Using CloudWatch Logs with an AWS SDK (p. 33). This topic also includes information about getting started and details about previous SDK versions.

Cancel an export task

Cancel a CloudWatch Logs export task using an AWS SDK

The following code example shows how to cancel an existing CloudWatch Logs export task.

.NET

AWS SDK for .NET

public static async Task Main() {

// This client object will be associated with the same AWS Region // as the default user on this system. If you need to use a // different AWS Region, pass it as a parameter to the client // constructor.

var client = new AmazonCloudWatchLogsClient();

string taskId = "exampleTaskId";

var request = new CancelExportTaskRequest {

TaskId = taskId, };

var response = await client.CancelExportTaskAsync(request);

if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) {

Console.WriteLine($"{taskId} successfully canceled.");

} else {

Console.WriteLine($"{taskId} could not be canceled.");

} }

• Find instructions and more code on GitHub.

• For API details, see CancelExportTask in AWS SDK for .NET API Reference.

For a complete list of AWS SDK developer guides and code examples, see Using CloudWatch Logs with an AWS SDK (p. 33). This topic also includes information about getting started and details about previous SDK versions.

Create a CloudWatch Logs log group using an AWS SDK

The following code example shows how to create a new CloudWatch Logs log group.

.NET

AWS SDK for .NET

public static async Task Main()

Create a new log stream

{

// This client object will be associated with the same AWS Region // as the default user on this system. If you need to use a // different AWS Region, pass it as a parameter to the client // constructor.

var client = new AmazonCloudWatchLogsClient();

string logGroupName = "cloudwatchlogs-example-loggroup";

var request = new CreateLogGroupRequest {

LogGroupName = logGroupName, };

var response = await client.CreateLogGroupAsync(request);

if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) {

Console.WriteLine($"Successfully create log group with ID:

{logGroupName}.");

} else {

Console.WriteLine("Could not create log group.");

} }

• Find instructions and more code on GitHub.

• For API details, see CreateLogGroup in AWS SDK for .NET API Reference.

For a complete list of AWS SDK developer guides and code examples, see Using CloudWatch Logs with an AWS SDK (p. 33). This topic also includes information about getting started and details about previous SDK versions.

Create a CloudWatch Logs log stream using an AWS SDK

The following code example shows how to create a new CloudWatch Logs log stream.

.NET

AWS SDK for .NET

public static async Task Main() {

// This client object will be associated with the same AWS Region // as the default user on this system. If you need to use a // different AWS Region, pass it as a parameter to the client // constructor.

var client = new AmazonCloudWatchLogsClient();

string logGroupName = "cloudwatchlogs-example-loggroup";

string logStreamName = "cloudwatchlogs-example-logstream";

var request = new CreateLogStreamRequest {

LogGroupName = logGroupName, LogStreamName = logStreamName,

Create a subscription filter

};

var response = await client.CreateLogStreamAsync(request);

if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) {

Console.WriteLine($"{logStreamName} successfully created for {logGroupName}.");

} else {

Console.WriteLine("Could not create stream.");

} }

• Find instructions and more code on GitHub.

• For API details, see CreateLogStream in AWS SDK for .NET API Reference.

For a complete list of AWS SDK developer guides and code examples, see Using CloudWatch Logs with an AWS SDK (p. 33). This topic also includes information about getting started and details about previous SDK versions.

Create a CloudWatch Logs subscription filter using an AWS SDK

The following code examples show how to create an Amazon CloudWatch Logs subscription filter.

C++

SDK for C++

Include the required files.

#include <aws/core/Aws.h>

#include <aws/logs/CloudWatchLogsClient.h>

#include <aws/logs/model/PutSubscriptionFilterRequest.h>

#include <aws/core/utils/Outcome.h>

#include <iostream>

Create the subscription filter.

Aws::CloudWatchLogs::CloudWatchLogsClient cwl;

Aws::CloudWatchLogs::Model::PutSubscriptionFilterRequest request;

request.SetFilterName(filter_name);

request.SetFilterPattern(filter_pattern);

request.SetLogGroupName(log_group);

request.SetDestinationArn(dest_arn);

auto outcome = cwl.PutSubscriptionFilter(request);

if (!outcome.IsSuccess()) {

std::cout << "Failed to create CloudWatch logs subscription filter "

<< filter_name << ": " << outcome.GetError().GetMessage() <<

std::endl;

} else

Create a subscription filter

{

std::cout << "Successfully created CloudWatch logs subscription " <<

"filter " << filter_name << std::endl;

}

• Find instructions and more code on GitHub.

• For API details, see PutSubscriptionFilter in AWS SDK for C++ API Reference.

Java

SDK for Java 2.x

public static void putSubFilters(CloudWatchLogsClient cwl, String filter,

String pattern, String logGroup, String functionArn) { try {

PutSubscriptionFilterRequest request =

PutSubscriptionFilterRequest.builder() .filterName(filter)

.filterPattern(pattern) .logGroupName(logGroup) .destinationArn(functionArn) .build();

cwl.putSubscriptionFilter(request);

System.out.printf(

"Successfully created CloudWatch logs subscription filter %s", filter);

} catch (CloudWatchLogsException e) {

System.err.println(e.awsErrorDetails().errorMessage());

System.exit(1);

} }

• Find instructions and more code on GitHub.

• For API details, see PutSubscriptionFilter in AWS SDK for Java 2.x API Reference.

JavaScript

SDK for JavaScript V3

Create the client in a separate module and export it.

import { CloudWatchLogsClient } from "@aws-sdk/client-cloudwatch-logs";

// Set the AWS Region.

const REGION = "REGION"; //e.g. "us-east-1"

// Create an Amazon CloudWatch Logs service client object.

export const cwlClient = new CloudWatchLogsClient({ region: REGION });

Import the SDK and client modules and call the API.

Create a subscription filter

// Import required AWS SDK clients and commands for Node.js import {

PutSubscriptionFilterCommand,

} from "@aws-sdk/client-cloudwatch-logs";

import { cwlClient } from "./libs/cloudWatchLogsClient.js";

// Set the parameters export const params = {

destinationArn: "LAMBDA_FUNCTION_ARN", //LAMBDA_FUNCTION_ARN filterName: "FILTER_NAME", //FILTER_NAME

filterPattern: "ERROR",

logGroupName: "LOG_GROUP", //LOG_GROUP };

export const run = async () => { try {

const data = await cwlClient.send(new PutSubscriptionFilterCommand(params));

console.log("Success", data.subscriptionFilters);

return data; //For unit tests.

} catch (err) {

console.log("Error", err);

}};

// Uncomment this line to run execution within this file.

// run();

• Find instructions and more code on GitHub.

• For more information, see AWS SDK for JavaScript Developer Guide.

• For API details, see PutSubscriptionFilter in AWS SDK for JavaScript API Reference.

SDK for JavaScript V2

// Load the AWS SDK for Node.js var AWS = require('aws-sdk');

// Set the region

AWS.config.update({region: 'REGION'});

// Create the CloudWatchLogs service object

var cwl = new AWS.CloudWatchLogs({apiVersion: '2014-03-28'});

var params = {

destinationArn: 'LAMBDA_FUNCTION_ARN', filterName: 'FILTER_NAME',

filterPattern: 'ERROR', logGroupName: 'LOG_GROUP', };

cwl.putSubscriptionFilter(params, function(err, data) { if (err) {

console.log("Error", err);

} else {

console.log("Success", data);

}});

• Find instructions and more code on GitHub.

• For more information, see AWS SDK for JavaScript Developer Guide.

• For API details, see PutSubscriptionFilter in AWS SDK for JavaScript API Reference.

Delete a log group

For a complete list of AWS SDK developer guides and code examples, see Using CloudWatch Logs with an AWS SDK (p. 33). This topic also includes information about getting started and details about previous SDK versions.

Delete a CloudWatch Logs log group using an AWS SDK

The following code example shows how to delete an existing CloudWatch Logs log group.

.NET

AWS SDK for .NET

public static async Task Main() {

var client = new AmazonCloudWatchLogsClient();

string logGroupName = "cloudwatchlogs-example-loggroup";

var request = new DeleteLogGroupRequest {

LogGroupName = logGroupName, };

var response = await client.DeleteLogGroupAsync(request);

if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) {

Console.WriteLine($"Successfully deleted CloudWatch log group, {logGroupName}.");

} }

• Find instructions and more code on GitHub.

• For API details, see DeleteLogGroup in AWS SDK for .NET API Reference.

For a complete list of AWS SDK developer guides and code examples, see Using CloudWatch Logs with an AWS SDK (p. 33). This topic also includes information about getting started and details about previous SDK versions.

Delete a CloudWatch Logs subscription filter using an AWS SDK

The following code examples show how to delete an Amazon CloudWatch Logs subscription filter.

C++

SDK for C++

Include the required files.

#include <aws/core/Aws.h>

#include <aws/core/utils/Outcome.h>

#include <aws/logs/CloudWatchLogsClient.h>

#include <aws/logs/model/DeleteSubscriptionFilterRequest.h>

Delete a subscription filter

#include <iostream>

Delete the subscription filter.

Aws::CloudWatchLogs::CloudWatchLogsClient cwl;

Aws::CloudWatchLogs::Model::DeleteSubscriptionFilterRequest request;

request.SetFilterName(filter_name);

request.SetLogGroupName(log_group);

auto outcome = cwl.DeleteSubscriptionFilter(request);

if (!outcome.IsSuccess()) {

std::cout << "Failed to delete CloudWatch log subscription filter "

<< filter_name << ": " << outcome.GetError().GetMessage() <<

std::endl;

} else {

std::cout << "Successfully deleted CloudWatch logs subscription " <<

"filter " << filter_name << std::endl;

}

• Find instructions and more code on GitHub.

• For API details, see DeleteSubscriptionFilter in AWS SDK for C++ API Reference.

Java

SDK for Java 2.x

public static void deleteSubFilter(CloudWatchLogsClient logs, String filter, String logGroup) {

try {

DeleteSubscriptionFilterRequest request = DeleteSubscriptionFilterRequest.builder() .filterName(filter)

.logGroupName(logGroup) .build();

logs.deleteSubscriptionFilter(request);

System.out.printf(

"Successfully deleted CloudWatch logs subscription filter %s", filter);

} catch (CloudWatchException e) {

System.err.println(e.awsErrorDetails().errorMessage());

System.exit(1);

} }

• Find instructions and more code on GitHub.

• For API details, see DeleteSubscriptionFilter in AWS SDK for Java 2.x API Reference.

JavaScript

SDK for JavaScript V3

Create the client in a separate module and export it.

Delete a subscription filter

import { CloudWatchLogsClient } from "@aws-sdk/client-cloudwatch-logs";

// Set the AWS Region.

const REGION = "REGION"; //e.g. "us-east-1"

// Create an Amazon CloudWatch Logs service client object.

export const cwlClient = new CloudWatchLogsClient({ region: REGION });

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {

DeleteSubscriptionFilterCommand

} from "@aws-sdk/client-cloudwatch-logs";

import { cwlClient } from "./libs/cloudWatchLogsClient.js";

// Set the parameters export const params = {

filterName: "FILTER", //FILTER

logGroupName: "LOG_GROUP", //LOG_GROUP };

export const run = async () => { try {

const data = await cwlClient.send(

new DeleteSubscriptionFilterCommand(params) );

console.log(

"Success, subscription filter deleted", data

);

return data; //For unit tests.

} catch (err) {

console.log("Error", err);

}};

// Uncomment this line to run execution within this file.

// run();

• Find instructions and more code on GitHub.

• For more information, see AWS SDK for JavaScript Developer Guide.

• For API details, see DeleteSubscriptionFilter in AWS SDK for JavaScript API Reference.

SDK for JavaScript V2

// Load the AWS SDK for Node.js var AWS = require('aws-sdk');

// Set the region

AWS.config.update({region: 'REGION'});

// Create the CloudWatchLogs service object

var cwl = new AWS.CloudWatchLogs({apiVersion: '2014-03-28'});

var params = {

filterName: 'FILTER', logGroupName: 'LOG_GROUP' };

cwl.deleteSubscriptionFilter(params, function(err, data) {

Describe existing subscription filters

if (err) {

console.log("Error", err);

} else {

console.log("Success", data);

}});

• Find instructions and more code on GitHub.

• For more information, see AWS SDK for JavaScript Developer Guide.

• For API details, see DeleteSubscriptionFilter in AWS SDK for JavaScript API Reference.

Kotlin

SDK for Kotlin

NoteThis is prerelease documentation for a feature in preview release. It is subject to change.

suspend fun deleteSubFilter( filter: String?, logGroup: String?) { val request = DeleteSubscriptionFilterRequest {

filterName = filter logGroupName = logGroup }

CloudWatchLogsClient { region = "us-west-2" }.use { logs ->

logs.deleteSubscriptionFilter(request)

println( "Successfully deleted CloudWatch logs subscription filter named $filter")

} }

• Find instructions and more code on GitHub.

• For API details, see DeleteSubscriptionFilter in AWS SDK for Kotlin API reference.

For a complete list of AWS SDK developer guides and code examples, see Using CloudWatch Logs with an AWS SDK (p. 33). This topic also includes information about getting started and details about previous SDK versions.

Describe CloudWatch Logs subscription filters using an AWS SDK

The following code examples show how to describe Amazon CloudWatch Logs existing subscription filters.

C++

SDK for C++

Include the required files.

#include <aws/core/Aws.h>

Describe existing subscription filters

#include <aws/core/utils/Outcome.h>

#include <aws/logs/CloudWatchLogsClient.h>

#include <aws/logs/model/DescribeSubscriptionFiltersRequest.h>

#include <aws/logs/model/DescribeSubscriptionFiltersResult.h>

#include <iostream>

#include <iomanip>

List the subscription filters.

Aws::CloudWatchLogs::CloudWatchLogsClient cwl;

Aws::CloudWatchLogs::Model::DescribeSubscriptionFiltersRequest request;

request.SetLogGroupName(log_group);

request.SetLimit(1);

bool done = false;

bool header = false;

while (!done) {

auto outcome = cwl.DescribeSubscriptionFilters(

request);

if (!outcome.IsSuccess()) {

std::cout << "Failed to describe CloudWatch subscription filters "

<< "for log group " << log_group << ": " <<

outcome.GetError().GetMessage() << std::endl;

break;

}

if (!header) {

std::cout << std::left << std::setw(32) << "Name" <<

std::setw(64) << "FilterPattern" << std::setw(64) <<

"DestinationArn" << std::endl;

header = true;

}

const auto &filters = outcome.GetResult().GetSubscriptionFilters();

for (const auto &filter : filters) {

std::cout << std::left << std::setw(32) <<

filter.GetFilterName() << std::setw(64) <<

filter.GetFilterPattern() << std::setw(64) <<

filter.GetDestinationArn() << std::endl;

}

const auto &next_token = outcome.GetResult().GetNextToken();

request.SetNextToken(next_token);

done = next_token.empty();

}

• Find instructions and more code on GitHub.

• For API details, see DescribeSubscriptionFilters in AWS SDK for C++ API Reference.

Java

SDK for Java 2.x

public static void describeFilters(CloudWatchLogsClient logs, String logGroup) {

try {

boolean done = false;

String newToken = null;

Describe existing subscription filters

while(!done) {

DescribeSubscriptionFiltersResponse response;

if (newToken == null) {

DescribeSubscriptionFiltersRequest request = DescribeSubscriptionFiltersRequest.builder() .logGroupName(logGroup)

.limit(1).build();

response = logs.describeSubscriptionFilters(request);

} else {

DescribeSubscriptionFiltersRequest request = DescribeSubscriptionFiltersRequest.builder() .nextToken(newToken)

.logGroupName(logGroup) .limit(1).build();

response = logs.describeSubscriptionFilters(request);

}

for(SubscriptionFilter filter : response.subscriptionFilters()) { System.out.printf(

"Retrieved filter with name %s, " + "pattern %s " +

"and destination arn %s", filter.filterName(),

filter.filterPattern(), filter.destinationArn());

System.out.println("");

}

if(response.nextToken() == null) { done = true;

} else {

newToken = response.nextToken();

} }

} catch (CloudWatchException e) {

System.err.println(e.awsErrorDetails().errorMessage());

System.exit(1);

}

System.out.printf("Done");

}

• Find instructions and more code on GitHub.

• For API details, see DescribeSubscriptionFilters in AWS SDK for Java 2.x API Reference.

JavaScript

SDK for JavaScript V3

Create the client in a separate module and export it.

import { CloudWatchLogsClient } from "@aws-sdk/client-cloudwatch-logs";

// Set the AWS Region.

const REGION = "REGION"; //e.g. "us-east-1"

const REGION = "REGION"; //e.g. "us-east-1"

在文檔中 Amazon CloudWatch Logs (頁 150-200)

相關文件