1

I am coding an app in .NET 5.0 to run on the desktop. It will query AWS CloudWatch and download log entries.

I am using the following method to create the AWS service client that contains the query method. When the program enters the CreateServiceClient() method however, it hangs for a minute then returns directly to the method that calls my GetLogs() method, with no error.

using Amazon.CloudWatchLogs;
using Amazon.CloudWatchLogs.Model;
using Amazon.Extensions.NETCore.Setup;
.
.
.
public async Task<GetQueryResultsResponse> GetLogs()
{ 
       AWSOptions awsOptions = new AWSOptions { Profile = "myprofile" };
       IAmazonCloudWatchLogs logs = awsOptions.CreateServiceClient<IAmazonCloudWatchLogs>();
       .
       .
       .

I have a credentials file located at C:\Users\Username\.aws\credentials with the contents

[myprofile]
aws_access_key_id = <myid>
aws_secret_access_key = <mykey>

I have verified the credentials work with AWS CLI. What am I missing?

1 Answer 1

2

YOu are not using the latest .NET API. Here is an example that shows how to create an Amazon CloudWatch log group using AmazonCloudWatchLogsClient. The example was created using the AWS SDK for .NET version 3.7 and .NET Core 5.0.

You should be using version 3.0 of the AWS SDK for .NET.

More examples here:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/CloudWatchLogs

namespace CreateLogGroupExample
{
    using System;
    using System.Threading.Tasks;
    using Amazon.CloudWatchLogs;
    using Amazon.CloudWatchLogs.Model;

    /// <summary>
    /// Shows how to create an Amazon CloudWatch Logs log group. The example
    /// was created using the AWS SDK for .NET version 3.7 and .NET Core 5.0.
    /// </summary>
    public class CreateLogGroup
    {
        // snippet-start:[CloudWatchLogs.dotnetv3.CreateLogGroupExample]
        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.");
            }
        }

        // snippet-end:[CloudWatchLogs.dotnetv3.CreateLogGroupExample]
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the information. When I run this I get the same behavior when instantiating the AmazonCloudWatchLogsClient object though. I filled in my log group and tried passing RegionEndpoint in the constructor. It's not finding my credentials file for some reason.
I will try this too and see if I can reproduce the issue
I ported the code over to a server running WinSvr2019 and it creates the client. I was trying to run it on a Svr2022 machine but not sure if the OS itself makes a difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.