How to create RDS instance using AWS SDK

 avatar
user_0649422
csharp
2 years ago
2.6 kB
9
Indexable
using Amazon;
using Amazon.Lambda.Core;
using Amazon.RDS;
using Amazon.RDS.Model;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace CreateRDSInstance;

public class Function
{

    /// <summary>
    /// A simple function that takes a string and does a ToUpper
    /// </summary>
    /// <param name="input"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public async Task<string> FunctionHandler(string input, ILambdaContext context)
    {
        // Configure the AWS region and credentials
        var region = RegionEndpoint.USEast1;
        var credentials = new Amazon.Runtime.BasicAWSCredentials("ACCESS_KEY", "SECRET_KEY");

        // Create an RDS client with the configured region and credentials
        var rdsClient = new AmazonRDSClient(credentials, region);

        // Configure the parameters for the RDS instance
        var dbInstanceIdentifier = "my-rds-instance";
        var dbInstanceClass = "db.t3.small";
        var engine = "sqlserver-ex";
        var engineVersion = "15.00.4236.7.v1";
        var masterUsername = "admin";
        var masterPassword = "mypassword";
        //var dbName = "nursdatabase";
        //var vpcSecurityGroupId = "vpc-0fcde65ba8e70a9f1";
        var subnetGroupName = "dev-my-rds-subnet-group"; //TODO create firstly in aws console
        var allocatedStorage = 20;

        // Create the request to create an RDS instance
        var createRequest = new CreateDBInstanceRequest
        {
            DBInstanceIdentifier = dbInstanceIdentifier,
            DBInstanceClass = dbInstanceClass,
            Engine = engine,
            EngineVersion = engineVersion,
            MasterUsername = masterUsername,
            MasterUserPassword = masterPassword,
            //DBName = dbName,
            //VpcSecurityGroupIds = new List<string> { vpcSecurityGroupId },
            DBSubnetGroupName = subnetGroupName,
            LicenseModel = "license-included",
            AllocatedStorage = allocatedStorage
        };

        // Create the RDS instance
        var response = await rdsClient.CreateDBInstanceAsync(createRequest);

        // Return the endpoint of the created RDS instance
        return response.DBInstance.Endpoint.Address; //TODO returns null exception, however, rds instance has status Creating
    }
}
Editor is loading...