admin管理员组

文章数量:1296221

We have been using the following AWS CLI command for a while, and it can successfully create an SSH tunnel to access an instance of MySQL in AWS RDS.

aws ssm start-session \
  --target "i-xxx" --region "regionEndpoint" --profile "ssm_profile" \
  --document-name "AWS-StartPortForwardingSessionToRemoteHost" \
  --parameters host="database-xxx.rds.amazonaws",portNumber="3306",localPortNumber="3306"

The above command will establish connection to the remote host's port 3306 and map it to port 3306 on the local host. See more on the following link:

  • /

We are trying to implement the same behavior by a C# program by AWS SDK, however, the following sample program always gets exceptions on var response = ssmClient.StartSessionAsync(startSessionRequest).Result;.

System.AggregateException: 'One or more errors occurred. (i-xxx is not connected.)', with the following two inner exceptions:

  • TargetNotConnectedException: i-xxx is not connected.
  • HttpErrorResponseException: Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown.

We are new to AWS SDK, and we highly appreciate any hints and suggestions.

Details:

Sample C# source code:

using Amazon;
using Amazon.SimpleSystemsManagement;
using Amazon.SimpleSystemsManagement.Model;
using Amazon.Runtime;

namespace aws_connect_poc
{
    public class AWSConnection
    {
        public static void SshTunnel2(
            string accessKey,
            string secretKey,
            string target,
            string databaseHost,
            RegionEndpoint regionEndpoint)
        {
            try
            {
                // Set AWS credentials manually
                var awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

                // Create SSM client with credentials and region
                using var ssmClient = new AmazonSimpleSystemsManagementClient(awsCredentials, regionEndpoint);

                // Create request for SSH tunnel
                var startSessionRequest = new StartSessionRequest
                {
                    Target = target,  // EC2 instance ID (i-xxxx)
                    DocumentName = "AWS-StartPortForwardingSessionToRemoteHost",
                    Parameters = new Dictionary<string, List<string>>
                    {
                        { "host", new List<string> { databaseHost } },  // RDS host
                        { "portNumber", new List<string> { "3306" } }, // Remote MySQL port
                        { "localPortNumber", new List<string> { "3306" } } // Local port
                    }
                };

                // Start the session
                var response = ssmClient.StartSessionAsync(startSessionRequest).Result;

                if (!string.IsNullOrEmpty(response.SessionId))
                {
                    Console.WriteLine($"SSH session started successfully: {response.SessionId}");
                }
                else
                {
                    Console.WriteLine("Failed to start SSH session.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}

本文标签: amazon web servicesHow to start port forwarding with AWS SDK in CStack Overflow