admin管理员组文章数量:1246601
We use the following testing program to start a port forwarding session by AWS SDK in C#, and the program finishes running without any issue. However, after the program finishes running, the session terminates in less than 30 seconds.
More information:
It looks like, the sessions started by the SDK do not really work. We are still looking for why.
We developed unit tests to start a session and immediately followed by a login request to connect to the database. And the result is:
If the session was started by C# calling commands of AWS CLI, the test passes.
If the session was started by C# calling APIs in SDK, the test fails with the following error message:
MySQL login failed.
ex.Message: Unable to connect to any of the specified MySQL hosts.
If we use AWS CLI instead of SDK, the following command can start a session and persist. The command will keep running and the session keeps working and accepting requests. Until we press Control + C, the process gets killed and the session terminates.
Therefore, we believe our credentials do not have any issue.
aws ssm start-session \
--target "i-xxx" --region "xxxxxxx" --profile "profile_name" \
--document-name "AWS-StartPortForwardingSessionToRemoteHost" \
--parameters host="xxxx-database-cluster.xxx.rds.amazonaws",portNumber="3306",localPortNumber="3306"
We prefer to use SDK because it provides better functionality for programmatical usage, so please point out if we missed anything in the program.
Details
Note: The following testing source code automatically refers to the default location in ~/.aws/
for its config
and credentials
files.
public static async Task CallSDKAsync()
{
// Load AWS credentials from the specified profile
var credentials = new Amazon.Runtime.StoredProfileAWSCredentials(SensitiveData.AwsProfile);
// Create the SSM client using the credentials
var client = new AmazonSimpleSystemsManagementClient(credentials, SensitiveData.AwsRegionEndpoint);
// Define the port forwarding parameters
var startSessionRequest = new StartSessionRequest
{
DocumentName = "AWS-StartPortForwardingSessionToRemoteHost",
Parameters = new Dictionary<string, List<string>>()
{
{ "host", new List<string> { SensitiveData.DatabaseHost } },
{ "portNumber", new List<string> { "3306" } },
{ "localPortNumber", new List<string> { "3306" } }
},
Target = SensitiveData.AwsInstanceId
};
try
{
var response = await client.StartSessionAsync(startSessionRequest);
Console.WriteLine(response.ToString());
Console.WriteLine("Session started successfully.");
}
catch (AmazonServiceException amazonEx)
{
Console.WriteLine($"AWS Service error: {amazonEx.Message}");
Console.WriteLine($"Status Code: {amazonEx.StatusCode}");
Console.WriteLine($"AWS Error Code: {amazonEx.ErrorCode}");
Console.WriteLine($"Request ID: {amazonEx.RequestId}");
Console.WriteLine($"AWS Error Type: {amazonEx.ErrorType}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine(ex.InnerException?.Message);
}
}
本文标签:
版权声明:本文标题:c# - Port forwarding sessions started by AWS SDK do not work and terminate in less than 30 seconds - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740264260a2250858.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论