Home > Software design >  How to create an AWS service client object for a .NET Core desktop app
How to create an AWS service client object for a .NET Core desktop app

Time:04-13

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?

CodePudding user response:

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]
    }
}
  • Related