Use CreateLogGroup with an AWS SDK or CLI
The following code examples show how to use CreateLogGroup.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in
context in the following code example:
- .NET
-
- SDK for .NET
-
using System;
using System.Threading.Tasks;
using Amazon.CloudWatchLogs;
using Amazon.CloudWatchLogs.Model;
/// <summary>
/// Shows how to create an Amazon CloudWatch Logs log group.
/// </summary>
public class CreateLogGroup
{
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";
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.");
}
}
}
- CLI
-
- AWS CLI
-
The following command creates a log group named my-logs:
aws logs create-log-group --log-group-name my-logs
- JavaScript
-
- SDK for JavaScript (v3)
-
import { CreateLogGroupCommand } from "@aws-sdk/client-cloudwatch-logs";
import { client } from "../libs/client.js";
const run = async () => {
const command = new CreateLogGroupCommand({
// The name of the log group.
logGroupName: process.env.CLOUDWATCH_LOGS_LOG_GROUP,
});
try {
return await client.send(command);
} catch (err) {
console.error(err);
}
};
export default run();
For a complete list of AWS SDK developer guides and code examples, see
Using CloudWatch Logs with an AWS SDK.
This topic also includes information about getting started and details about previous SDK versions.