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
版权声明:本文标题:amazon web services - How to start port forwarding with AWS SDK in C#? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741634230a2389547.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论