1. Using aws rds with appautoscaling

    TypeScript

    To use AWS RDS with Application Auto Scaling, you'll need to set up several components:

    1. An RDS Database Instance or Cluster: This is the actual database where your data will be stored and managed. RDS supports several database engines such as PostgreSQL, MySQL, MariaDB, Oracle, and SQL Server.

    2. Auto Scaling Target and Policies: The aws-native.applicationautoscaling.ScalableTarget resource allows you to define the parameters for auto-scaling for your RDS instance or cluster. It specifies the minimum and maximum capacity between which auto-scaling should operate. Alongside, you can define scaling policies, which are the rules that determine when scaling should happen. These can be target tracking scaling policies, step scaling policies, or scheduled scaling policies.

    3. IAM Role: The Application Auto Scaling service requires a role with permission to modify the RDS instance or cluster's scaling settings.

    Below is a Pulumi TypeScript program that creates an RDS instance and configures auto-scaling for it. Make sure you have Pulumi installed and configured to use with AWS.

    This code will set up a MySQL RDS instance and configure Application Auto Scaling to adjust the number of read replicas based on the CPU Utilization.

    import * as aws from "@pulumi/aws"; // Create an RDS DB instance const dbInstance = new aws.rds.Instance("my-db-instance", { allocatedStorage: 20, engine: "mysql", engineVersion: "5.7", instanceClass: "db.t2.micro", name: "mydatabase", parameterGroupName: "default.mysql5.7", username: "myadmin", password: "mysecurepassword", // Please use a secure way to inject secrets, for example aws.secretmanager.Secret or config skipFinalSnapshot: true, }); // Create an IAM role that can be used by Application Auto Scaling service const appAutoScalingRole = new aws.iam.Role("app-auto-scaling-role", { assumeRolePolicy: JSON.stringify({ Version: "2012-10-17", Statement: [{ Action: "sts:AssumeRole", Effect: "Allow", Principal: { Service: "application-autoscaling.amazonaws.com", }, }], }), }); // Attach the necessary policy to the role const appAutoScalingRolePolicyAttachment = new aws.iam.RolePolicyAttachment("app-auto-scaling-role-attachment", { role: appAutoScalingRole.name, policyArn: "arn:aws:iam::aws:policy/service-role/AmazonRDSCloudWatchLogsExportsToCloudWatchRole", }); // Define the scalable target for your RDS read replica const scalableTarget = new aws.applicationautoscaling.Target("autoscaling-target", { maxCapacity: 15, minCapacity: 1, resourceId: pulumi.interpolate`cluster:${dbInstance.id}`, roleArn: appAutoScalingRole.arn, scalableDimension: "rds:cluster:ReadReplicaCount", serviceNamespace: "rds", }); // Define the scaling policy const scalingPolicy = new aws.applicationautoscaling.Policy("autoscaling-policy", { policyType: "TargetTrackingScaling", resourceId: scalableTarget.resourceId, scalableDimension: scalableTarget.scalableDimension, serviceNamespace: scalableTarget.serviceNamespace, targetTrackingScalingPolicyConfiguration: { targetValue: 75.0, predefinedMetricSpecification: { predefinedMetricType: "RDSReaderAverageCPUUtilization", }, scaleInCooldown: 300, scaleOutCooldown: 300, }, });

    Explanation

    • aws.rds.Instance: This creates a new RDS instance. We've chosen MySQL as the engine and specified some required parameters like allocated storage, version, instance class, and credentials. Do not hard-code credentials in production code. Use a secret management system instead.

    • aws.iam.Role: We're creating an IAM role that the Application Auto Scaling service will assume to make changes to the RDS instance scaling settings.

    • aws.applicationautoscaling.Target: This sets up the scalable target with necessary parameters, including the resource ID from the dbInstance, the IAM role we created for auto-scaling, and details about what we want to scale (in this case, the read replica count).

    • aws.applicationautoscaling.Policy: Here, we define a scaling policy based on CPU utilization. We're targeting 75% CPU utilization for our RDS instance's read replicas. This policy will scale in and out (adding or removing read replicas) with the cooldown periods defined.

    Next Steps

    You should replace the placeholder values like the database name, username, and password with actual secure credentials. Furthermore, explore using AWS Secrets Manager for handling your database credentials securely when deploying with Pulumi.

    Also, before running this program with Pulumi, you need to have the AWS CLI configured with the appropriate credentials and default region.

    Once your AWS account is set up and you have Pulumi installed and configured, you can run the following commands in your terminal to deploy the code:

    pulumi up

    This will prompt you to review the changes and confirm the deployment, which on approval will provision the resources as defined in the program above.

    Please note that RDS and Application Auto Scaling have costs associated. Check AWS pricing and use the AWS free tier if you are eligible.

    Was this response helpful?