Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Use AssociateKmsKey
with an Amazon SDK or CLI
The following code example shows how to use AssociateKmsKey
.
- .NET
-
- Amazon SDK for .NET
-
using System;
using System.Threading.Tasks;
using Amazon.CloudWatchLogs;
using Amazon.CloudWatchLogs.Model;
/// <summary>
/// Shows how to associate an AWS Key Management Service (AWS KMS) key with
/// an Amazon CloudWatch Logs log group.
/// </summary>
public class AssociateKmsKey
{
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}.");
}
}
}
For a complete list of Amazon SDK developer guides and code examples, see
Using CloudWatch Logs with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.