Automate Amazon RDS for PostgreSQL horizontal scaling and system integration with Amazon EventBridge and Amazon Lambda

海外精选
海外精选的内容汇集了全球优质的亚马逊云科技相关技术内容。同时,内容中提到的“AWS” 是 “Amazon Web Services” 的缩写,在此网站不作为商标展示。
0
0
{"value":"You may have a workload where you want to automate scaling, such as a reporting application with unpredictable increases in queries, or an application with database utilization increasing at predictable times like end-of-month reporting. Scaling a database to appropriately handle workload demand is important to help manage cost, operations, performance, security, and reliability. With [Amazon Relational Database Service (Amazon RDS) for PostgreSQL](https://aws.amazon.com/rds/postgresql/), you can scale a database instance [vertically or horizontally](https://aws.amazon.com/blogs/database/scaling-your-amazon-rds-instance-vertically-and-horizontally/). You implement vertical scaling by changing the DB instance type or size (for example, from M to R or from xlarge to 2xlarge), and scale horizontally by creating [read replicas](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PostgreSQL.Replication.ReadReplicas.html).\n\nNotification of the scaling event gives you the opportunity to automate system integration. One example is notifying your cost management system about the scaling event in order to provide near real-time cost metrics. Another example is to notify your application, and dynamically adjust to use the read replica for queries. A third example is providing metrics to your data warehouse when the scaling event occurs.\n\nIn this post, we provide a solution to automate horizontal scaling, and create a mechanism to automate system integration.\n\n#### **Overview of solution**\n\nThe solution provides horizontal scaling through an event-driven architecture by monitoring your RDS for PostgreSQL database instance, and automating read replica creation based on database workload metrics. The solution creates one read replica when triggered, and the maximum number of read replicas is configurable. You can create up to five read replicas from one source DB instance. As of RDS for PostgreSQL 14.1, you can also create up to [three levels of read replica in a chain (cascade)](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PostgreSQL.Replication.ReadReplicas.html#USER_PostgreSQL.Replication.ReadReplicas.Configuration.cascading) from a source DB instance. For simplicity, this solution limits the number of read replicas to five.\n\nYour application is notified when a read replica is created. Logic built into your application offloads queries to the read replicas. In this solution, [Amazon CloudWatch](http://aws.amazon.com/cloudwatch) provides [monitoring](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/monitoring-cloudwatch.html) based on database CPU, and [Amazon EventBridge](http://aws.amazon.com/cloudwatch) watches for a CloudWatch alarm and routes the event to an [Amazon Web Services Lambda](https://aws.amazon.com/lambda/) function. The Lambda function creates a read replica and notifies your application with [Amazon Simple Notification Service](https://aws.amazon.com/sns/) ([Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail)). You can add code in the Lambda function to automate system integration. When the application is made aware of the read replica, you may offload read queries to the new instance.\n\nThe following diagram illustrates the solution architecture.\n\n![image.png](https://dev-media.amazoncloud.cn/0a576654fba24f0c86005ddf835639f2_image.png)\n\nThis solution provides the following benefits:\n\n- You can create RDS for PostgreSQL read replicas based on a configurable CPU threshold.\n- You can add code to automate system integration in the Amazon Web Services Lambda function.\n- You can notify the application code with [Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail) when a read replica is created.\n- You have a configurable maximum number of read replicas.\n- You have a configurable debug level.\n\n#### **Prerequisites**\n\nFor this solution, the following prerequisites are required:\n\n- An application architecture capable of handling and responding to events.\n- An [Amazon Web Services account](https://portal.aws.amazon.com/billing/signup).\n- [Amazon Virtual Private Cloud](https://aws.amazon.com/quickstart/architecture/vpc) ([Amazon VPC](https://aws.amazon.com/cn/vpc/?trk=cndc-detail)) with three private subnets.\n- [Amazon RDS PostgreSQL high availability (Multi-AZ) deployment version 13.6 or later](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.MultiAZ.html) .\n- An [Amazon Web Services Identity and Access Management](http://aws.amazon.com/iam) (IAM) [Lambda execution role](https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html) with basic Lambda permissions, [Amazon RDS](https://aws.amazon.com/cn/rds/?trk=cndc-detail) permissions DescribeDBInstances and CreateDBInstanceReadReplica, and the [Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail) permission Publish.\n- Familiarity with [Amazon RDS](https://aws.amazon.com/cn/rds/?trk=cndc-detail), [Amazon VPC](https://aws.amazon.com/cn/vpc/?trk=cndc-detail), [Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail), and IAM.\n\n#### **Deploy the solution**\n\nYou can deploy the solution by using the CloudFormation template provided as part of this blog post. In this solution, the resources we create in your account are:\n\n- Amazon Web Services Lambda function\n- [Amazon CloudWatch](https://aws.amazon.com/cn/cloudwatch/?trk=cndc-detail) Logs log group\n- [Amazon CloudWatch](https://aws.amazon.com/cn/cloudwatch/?trk=cndc-detail) alarm\n- [Amazon EventBridge](https://aws.amazon.com/cn/eventbridge/?trk=cndc-detail) rule\n- Amazon Web Services Key Management Service (Amazon Web Services KMS) customer managed key for SNS\n- Amazon Web Services Key Management Service (Amazon Web Services KMS) customer managed key for CloudWatch\n- [Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail) topic\n\nClick the **Launch Stack** button to deploy the CloudFormation template in the us-east-1 region.\n\n[![image.png](https://dev-media.amazoncloud.cn/07999cbce4fb4f23ba7907f41b1d0775_image.png)](https://console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/create/review?stackName=database-blog-hz-scale&templateURL=https://aws-blogs-artifacts-public.s3.amazonaws.com/artifacts/DBBLOG-1871/database_DBBLOG-1871_rds-scale-horizontal.yaml)\n\nAlternatively, you can manually create the stack:\n\n1. On the [Amazon Web Services CloudFormation console](https://console.aws.amazon.com/cloudformation/home), choose **Create Stack**.\n2. On the **Create stack** screen, in the **[Amazon S3](https://aws.amazon.com/cn/s3/?trk=cndc-detail) URL** enter:\n\n```\\nhttps://aws-blogs-artifacts-public.s3.amazonaws.com/artifacts/DBBLOG-1871/database_DBBLOG-1871_rds-scale-horizontal.yaml\\n```\n\nThe template requires application control, threshold, and Lambda configuration input parameters.\n\n#### **The application control configuration is as follows:**\n\n- **Deployment ID** – The deployment ID provides unique resource names.\n- **RDS identifier** – The RDS for PostgreSQL database identifier for the database you want to monitor.\n- **Maximum number of read replicas** – The maximum number of read replicas allowed. An exception is thrown when the limit is exceeded.\n\n![image.png](https://dev-media.amazoncloud.cn/ff901b47c4c94af7b5255ff81e907cc0_image.png)\n\n#### **The threshold configuration is as follows:**\n\n- **CPU threshold** – The solution creates a read replica when your database CPU is greater than or equal to this number, and the evaluation period number of datapoints is met. A read replica is not created when the maximum number of read replicas defined by ```MaxNumReadRelicaParameter``` is reached.\n- **CloudWatch period** – The period, in seconds, over which the CPU threshold is applied. Valid values are 10, 30, 60, and any multiple of 60.\n- **CloudWatch evaluation periods** – The number of periods over which data is compared to the CPU threshold.\n- **CloudWatch datapoints to alarm** – The number of data points that must be breaching to trigger the alarm.\n\n![image.png](https://dev-media.amazoncloud.cn/304e67c2bd0c43e3923da5284e0b8ec4_image.png)\n\n#### **The Lambda configuration is as follows:**\n\n- **Lambda execution role** – The Lambda execution role for the Lambda function we create. Use the Amazon Resource Name (ARN) of the role you created as a prerequisite.\n- **Memory size** – The amount of memory available to the function at runtime. Increasing the function memory also increases its CPU allocation. The default value is 128 MB. The value can be any multiple of 1 MB up to 10240.\n- **Lambda timeout** – The amount of time (in seconds) Lambda allows the function to run before stopping it. The maximum allowed value is 900 seconds.\n- **Security groups** – The security groups for the Lambda function.\n- **Subnets** – The subnets to deploy the Lambda function in.\n- **Python debug level** – The debug level for Python logger.\n\n![image.png](https://dev-media.amazoncloud.cn/509a994455b042ce990424d93e170787_image.png)\n\nIn the following section, we walk through the high-level components of the solution and how to monitor their progress.\n\n#### **Monitor [Amazon RDS](https://aws.amazon.com/cn/rds/?trk=cndc-detail) database CPU utilization via CloudWatch**\n\nTo view the [CloudWatch alarm](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ConsoleAlarms.html), complete the following steps:\n\n1. On the CloudWatch console, choose **All Alarms** in the navigation pane.\n2. Choose the CloudWatch alarm.\n3. Review the values for **Threshold** and **DBInstanceIdentifier** in the details pane.\n\n![image.png](https://dev-media.amazoncloud.cn/78abc660bdb44468a6d1cce600a85e38_image.png)\n\nThe alarm state changes from ```OK``` to ```ALARM``` when the threshold condition is met. For demonstration purposes, the alarm uses an evaluation period of 15 minutes. Adjust the values to meet your needs.\n\n#### **Monitor the CloudWatch alarm and invoke a Lambda function via EventBridge**\nTo view the [EventBridge rule](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/Create-CloudWatch-Events-Rule.html), complete the following steps:\n\n1. On the EventBridge console, choose **Rules** in the navigation pane.\n2. Choose the rule.\n3. Review the information under **Event pattern** and **Target**.\n\n![image.png](https://dev-media.amazoncloud.cn/b60a07f186454039a154545083c10a9a_image.png)\n\nYour EventBridge rule is configured to run the Lambda function when the CloudWatch alarm state changes.\n\n#### **Create an [Amazon RDS](https://aws.amazon.com/cn/rds/?trk=cndc-detail) read replica via Lambda**\n\nTo view how the [Lambd­a function](https://docs.aws.amazon.com/lambda/latest/dg/getting-started-create-function.html) creates a read replica, complete the following steps:\n\n1. On the Lambda console, choose **Functions** in the navigation pane.\n2. Choose the function.\n3. In the **Code source** editor, review the ```lambda_handler``` Python function.\n\n![image.png](https://dev-media.amazoncloud.cn/57cb161f723241e291ea4cac90b2f9a5_image.png)\n\n- The ```lambda_handler``` python function checks the alarm state and invokes ```create_rds_read_replica``` if the status is ALARM.\n\n4. In the **Code source** editor, review the ```create_rds_read_replica``` Python function.\n\n![image.png](https://dev-media.amazoncloud.cn/0a87b77e8e744e00980cbf4908130f8d_image.png)\n\nThe ```create_rds_read_replica``` Python function creates a read replica if the number of read replicas is less than your defined maximum number of replicas. The ```describe_db_instances``` [Amazon RDS](https://aws.amazon.com/cn/rds/?trk=cndc-detail) API function is used to determine the current number of read replicas for your database instance.\n\n#### **Send events to an SNS topic via Lambda**\n\nTo review how the function sends events to [Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail), complete the following steps:\n\n1. On the Lambda console, choose Functions in the navigation pane.\n2. Choose the function.\n3. In the Code source editor, scroll to the ```notify_application```Python function.\n\n![image.png](https://dev-media.amazoncloud.cn/32789a5cd9c54b54adcd89b32f551f38_image.png)\n\nAn event is sent to your application when the read replica is created and when the read replica is available via the SNS topic created by the CloudFormation template.\n\n#### **Subscribe to the SNS topic**\n\nYou must [write application code](https://docs.aws.amazon.com/code-samples/latest/catalog/python-sns-sns_basics.py.html) to subscribe to the SNS topic, and offload read-only queries to the read replicas. The event includes the read replica instance status and connection endpoint. Your application should check for ```DBInstanceStatus ```status ```available```. Use the connection endpoint to connect and perform read-only queries. The following is a sample event including the ```DBInstanceStatus``` status ```available```:\n\n```\\n{\\n \\"DBInstances\\": [{\\"DBInstanceIdentifier\\": \\"poc-read-replica-3\\",\\n \\"DBInstanceClass\\": \\"db.t3.small\\",\\n \\"Engine\\": \\"postgres\\",\\n \\"DBInstanceStatus\\": \\"available\\",\\n \\"MasterUsername\\": \\"admindba\\",\\n \\"DBName\\": \\"rdspoc\\",\\n \\"Endpoint\\": {\\"Address\\": \\"frbpoc-read-replica-3.XXXXXXXXX.us-east-1.rds.amazonaws.com\\", \\"Port\\": 5432, …\\n}\\n```\nYou can use this [code sample](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Connecting.Python.html) for connecting and querying a PostgreSQL database.\nThe walk-through is complete; you achieved horizontal scaling by adding read replicas when the CPU threshold exceeds the number of configured data points breached within the defined CloudWatch alarm evaluation period.\n\n#### **System Integration**\n\nModifying the solution to automate system integration requires you to add code in the Lambda function. Within the Lambda source code, the ```notify_application``` Python function is a hook where you can automate system integration. The code provided in this blog publishes an [Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail) event. You can add code to communicate with systems such as [calling an API to manage workflows](https://aws.amazon.com/blogs/architecture/managing-asynchronous-workflows-with-a-rest-api/), or communicate with a [software as a service (SaaS) solution](https://aws.amazon.com/blogs/compute/extending-saas-products-with-serverless-functions/).\n\n#### **Test scalability**\n\nYou can use a tool such as [pgbench](https://www.postgresql.org/docs/current/pgbench.html) to generate load on your database to test scalability. For an example to generate load, refer to [Automate benchmark tests for Amazon Aurora PostgreSQL](https://aws.amazon.com/blogs/database/automate-benchmark-tests-for-amazon-aurora-postgresql/).\n\n#### **Monitor your database**\n\nA variety of tools are available to [monitor](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/MonitoringOverview.html) your database, including [Amazon RDS Performance Insights](https://aws.amazon.com/rds/performance-insights/), [Amazon RDS Enhanced Monitoring](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_Monitoring.OS.html), [Amazon RDS database logs](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_LogAccess.html), and [Amazon CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/). You can use these tools to help determine the threshold when you want to trigger a scaling event.\n\n#### **Clean up**\n\nTo avoid incurring future charges, delete the resources you created as part of this post. You can clean up the Amazon Web Services resources (Lambda function, CloudWatch alarm, EventBridge rule, KMS customer managed keys, and SNS topic) by [deleting the CloudFormation stack](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-delete-stack.html).\n\n#### **Conclusion**\n\nIn this post, we provided an automated solution to horizontally scale [Amazon RDS](https://aws.amazon.com/cn/rds/?trk=cndc-detail) for PostgreSQL using an event-driven architecture. CloudWatch monitors your RDS database instance CPU. EventBridge watches for a CloudWatch alarm, and routes the event to a Lambda function. The function creates a read replica and notifies your application via [Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail). You can add code in the Lambda function to automate system integration.\n\nWhen scaling your database, it’s important to [optimize cost](https://aws.amazon.com/blogs/database/optimizing-costs-in-amazon-rds/) and follow [best practices](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_BestPractices.html#CHAP_BestPractices.PostgreSQL). As you look at options to scale your RDS database instance, I encourage you to experiment scaling vertically and horizontally. The example in this post uses CPU to determine when a read replica is created. You can [create a composite alarm](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create_Composite_Alarm.html) using multiple metrics such as CPU and memory to refine the read replica creation criteria. You can also [set alarms from Performance Insights metrics](https://aws.amazon.com/blogs/database/set-alarms-on-performance-insights-metrics-using-amazon-cloudwatch/). Another consideration is to scale down by deleting the read replicas when they’re no longer needed. Close open connections to the read replica before deleting the read replica. You can create a similar solution as described in this post with a CloudWatch alarm based on a CPU threshold to scale down.\n\n#### **About the Author**\n\n![image.png](https://dev-media.amazoncloud.cn/85220630e1f445f293c10f778ec3cc42_image.png)\n\n**Andrew Love** is a Sr. Solutions Architect in the Worldwide Public Sector at Amazon Web Services. He is passionate about helping customers build well-architected solutions to achieve their business needs. He enjoys spending time with his family, a good game of chess, home improvement projects, and writing code.\n\n","render":"<p>You may have a workload where you want to automate scaling, such as a reporting application with unpredictable increases in queries, or an application with database utilization increasing at predictable times like end-of-month reporting. Scaling a database to appropriately handle workload demand is important to help manage cost, operations, performance, security, and reliability. With <a href=\\"https://aws.amazon.com/rds/postgresql/\\" target=\\"_blank\\">Amazon Relational Database Service (Amazon RDS) for PostgreSQL</a>, you can scale a database instance <a href=\\"https://aws.amazon.com/blogs/database/scaling-your-amazon-rds-instance-vertically-and-horizontally/\\" target=\\"_blank\\">vertically or horizontally</a>. You implement vertical scaling by changing the DB instance type or size (for example, from M to R or from xlarge to 2xlarge), and scale horizontally by creating <a href=\\"https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PostgreSQL.Replication.ReadReplicas.html\\" target=\\"_blank\\">read replicas</a>.</p>\\n<p>Notification of the scaling event gives you the opportunity to automate system integration. One example is notifying your cost management system about the scaling event in order to provide near real-time cost metrics. Another example is to notify your application, and dynamically adjust to use the read replica for queries. A third example is providing metrics to your data warehouse when the scaling event occurs.</p>\n<p>In this post, we provide a solution to automate horizontal scaling, and create a mechanism to automate system integration.</p>\n<h4><a id=\\"Overview_of_solution_6\\"></a><strong>Overview of solution</strong></h4>\\n<p>The solution provides horizontal scaling through an event-driven architecture by monitoring your RDS for PostgreSQL database instance, and automating read replica creation based on database workload metrics. The solution creates one read replica when triggered, and the maximum number of read replicas is configurable. You can create up to five read replicas from one source DB instance. As of RDS for PostgreSQL 14.1, you can also create up to <a href=\\"https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PostgreSQL.Replication.ReadReplicas.html#USER_PostgreSQL.Replication.ReadReplicas.Configuration.cascading\\" target=\\"_blank\\">three levels of read replica in a chain (cascade)</a> from a source DB instance. For simplicity, this solution limits the number of read replicas to five.</p>\\n<p>Your application is notified when a read replica is created. Logic built into your application offloads queries to the read replicas. In this solution, <a href=\\"http://aws.amazon.com/cloudwatch\\" target=\\"_blank\\">Amazon CloudWatch</a> provides <a href=\\"https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/monitoring-cloudwatch.html\\" target=\\"_blank\\">monitoring</a> based on database CPU, and <a href=\\"http://aws.amazon.com/cloudwatch\\" target=\\"_blank\\">Amazon EventBridge</a> watches for a CloudWatch alarm and routes the event to an <a href=\\"https://aws.amazon.com/lambda/\\" target=\\"_blank\\">Amazon Web Services Lambda</a> function. The Lambda function creates a read replica and notifies your application with <a href=\\"https://aws.amazon.com/sns/\\" target=\\"_blank\\">Amazon Simple Notification Service</a> ([Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail)). You can add code in the Lambda function to automate system integration. When the application is made aware of the read replica, you may offload read queries to the new instance.</p>\\n<p>The following diagram illustrates the solution architecture.</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/0a576654fba24f0c86005ddf835639f2_image.png\\" alt=\\"image.png\\" /></p>\n<p>This solution provides the following benefits:</p>\n<ul>\\n<li>You can create RDS for PostgreSQL read replicas based on a configurable CPU threshold.</li>\n<li>You can add code to automate system integration in the Amazon Web Services Lambda function.</li>\n<li>You can notify the application code with Amazon SNS when a read replica is created.</li>\n<li>You have a configurable maximum number of read replicas.</li>\n<li>You have a configurable debug level.</li>\n</ul>\\n<h4><a id=\\"Prerequisites_24\\"></a><strong>Prerequisites</strong></h4>\\n<p>For this solution, the following prerequisites are required:</p>\n<ul>\\n<li>An application architecture capable of handling and responding to events.</li>\n<li>An <a href=\\"https://portal.aws.amazon.com/billing/signup\\" target=\\"_blank\\">Amazon Web Services account</a>.</li>\\n<li><a href=\\"https://aws.amazon.com/quickstart/architecture/vpc\\" target=\\"_blank\\">Amazon Virtual Private Cloud</a> ([Amazon VPC](https://aws.amazon.com/cn/vpc/?trk=cndc-detail)) with three private subnets.</li>\\n<li><a href=\\"https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.MultiAZ.html\\" target=\\"_blank\\">Amazon RDS PostgreSQL high availability (Multi-AZ) deployment version 13.6 or later</a> .</li>\\n<li>An <a href=\\"http://aws.amazon.com/iam\\" target=\\"_blank\\">Amazon Web Services Identity and Access Management</a> (IAM) <a href=\\"https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html\\" target=\\"_blank\\">Lambda execution role</a> with basic Lambda permissions, [Amazon RDS](https://aws.amazon.com/cn/rds/?trk=cndc-detail) permissions DescribeDBInstances and CreateDBInstanceReadReplica, and the [Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail) permission Publish.</li>\\n<li>Familiarity with Amazon RDS, Amazon VPC, Amazon SNS, and IAM.</li>\n</ul>\\n<h4><a id=\\"Deploy_the_solution_35\\"></a><strong>Deploy the solution</strong></h4>\\n<p>You can deploy the solution by using the CloudFormation template provided as part of this blog post. In this solution, the resources we create in your account are:</p>\n<ul>\\n<li>Amazon Web Services Lambda function</li>\n<li>Amazon CloudWatch Logs log group</li>\n<li>Amazon CloudWatch alarm</li>\n<li>Amazon EventBridge rule</li>\n<li>Amazon Web Services Key Management Service (Amazon Web Services KMS) customer managed key for SNS</li>\n<li>Amazon Web Services Key Management Service (Amazon Web Services KMS) customer managed key for CloudWatch</li>\n<li>Amazon SNS topic</li>\n</ul>\\n<p>Click the <strong>Launch Stack</strong> button to deploy the CloudFormation template in the us-east-1 region.</p>\\n<p><a href=\\"https://console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/create/review?stackName=database-blog-hz-scale&amp;templateURL=https://aws-blogs-artifacts-public.s3.amazonaws.com/artifacts/DBBLOG-1871/database_DBBLOG-1871_rds-scale-horizontal.yaml\\" target=\\"_blank\\"><img src=\\"https://dev-media.amazoncloud.cn/07999cbce4fb4f23ba7907f41b1d0775_image.png\\" alt=\\"image.png\\" /></a></p>\\n<p>Alternatively, you can manually create the stack:</p>\n<ol>\\n<li>On the <a href=\\"https://console.aws.amazon.com/cloudformation/home\\" target=\\"_blank\\">Amazon Web Services CloudFormation console</a>, choose <strong>Create Stack</strong>.</li>\\n<li>On the <strong>Create stack</strong> screen, in the <strong>Amazon S3 URL</strong> enter:</li>\\n</ol>\n<pre><code class=\\"lang-\\">https://aws-blogs-artifacts-public.s3.amazonaws.com/artifacts/DBBLOG-1871/database_DBBLOG-1871_rds-scale-horizontal.yaml\\n</code></pre>\\n<p>The template requires application control, threshold, and Lambda configuration input parameters.</p>\n<h4><a id=\\"The_application_control_configuration_is_as_follows_62\\"></a><strong>The application control configuration is as follows:</strong></h4>\\n<ul>\\n<li><strong>Deployment ID</strong> – The deployment ID provides unique resource names.</li>\\n<li><strong>RDS identifier</strong> – The RDS for PostgreSQL database identifier for the database you want to monitor.</li>\\n<li><strong>Maximum number of read replicas</strong> – The maximum number of read replicas allowed. An exception is thrown when the limit is exceeded.</li>\\n</ul>\n<p><img src=\\"https://dev-media.amazoncloud.cn/ff901b47c4c94af7b5255ff81e907cc0_image.png\\" alt=\\"image.png\\" /></p>\n<h4><a id=\\"The_threshold_configuration_is_as_follows_70\\"></a><strong>The threshold configuration is as follows:</strong></h4>\\n<ul>\\n<li><strong>CPU threshold</strong> – The solution creates a read replica when your database CPU is greater than or equal to this number, and the evaluation period number of datapoints is met. A read replica is not created when the maximum number of read replicas defined by <code>MaxNumReadRelicaParameter</code> is reached.</li>\\n<li><strong>CloudWatch period</strong> – The period, in seconds, over which the CPU threshold is applied. Valid values are 10, 30, 60, and any multiple of 60.</li>\\n<li><strong>CloudWatch evaluation periods</strong> – The number of periods over which data is compared to the CPU threshold.</li>\\n<li><strong>CloudWatch datapoints to alarm</strong> – The number of data points that must be breaching to trigger the alarm.</li>\\n</ul>\n<p><img src=\\"https://dev-media.amazoncloud.cn/304e67c2bd0c43e3923da5284e0b8ec4_image.png\\" alt=\\"image.png\\" /></p>\n<h4><a id=\\"The_Lambda_configuration_is_as_follows_79\\"></a><strong>The Lambda configuration is as follows:</strong></h4>\\n<ul>\\n<li><strong>Lambda execution role</strong> – The Lambda execution role for the Lambda function we create. Use the Amazon Resource Name (ARN) of the role you created as a prerequisite.</li>\\n<li><strong>Memory size</strong> – The amount of memory available to the function at runtime. Increasing the function memory also increases its CPU allocation. The default value is 128 MB. The value can be any multiple of 1 MB up to 10240.</li>\\n<li><strong>Lambda timeout</strong> – The amount of time (in seconds) Lambda allows the function to run before stopping it. The maximum allowed value is 900 seconds.</li>\\n<li><strong>Security groups</strong> – The security groups for the Lambda function.</li>\\n<li><strong>Subnets</strong> – The subnets to deploy the Lambda function in.</li>\\n<li><strong>Python debug level</strong> – The debug level for Python logger.</li>\\n</ul>\n<p><img src=\\"https://dev-media.amazoncloud.cn/509a994455b042ce990424d93e170787_image.png\\" alt=\\"image.png\\" /></p>\n<p>In the following section, we walk through the high-level components of the solution and how to monitor their progress.</p>\n<h4><a id=\\"Monitor_Amazon_RDS_database_CPU_utilization_via_CloudWatch_92\\"></a><strong>Monitor Amazon RDS database CPU utilization via CloudWatch</strong></h4>\\n<p>To view the <a href=\\"https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ConsoleAlarms.html\\" target=\\"_blank\\">CloudWatch alarm</a>, complete the following steps:</p>\\n<ol>\\n<li>On the CloudWatch console, choose <strong>All Alarms</strong> in the navigation pane.</li>\\n<li>Choose the CloudWatch alarm.</li>\n<li>Review the values for <strong>Threshold</strong> and <strong>DBInstanceIdentifier</strong> in the details pane.</li>\\n</ol>\n<p><img src=\\"https://dev-media.amazoncloud.cn/78abc660bdb44468a6d1cce600a85e38_image.png\\" alt=\\"image.png\\" /></p>\n<p>The alarm state changes from <code>OK</code> to <code>ALARM</code> when the threshold condition is met. For demonstration purposes, the alarm uses an evaluation period of 15 minutes. Adjust the values to meet your needs.</p>\\n<h4><a id=\\"Monitor_the_CloudWatch_alarm_and_invoke_a_Lambda_function_via_EventBridge_104\\"></a><strong>Monitor the CloudWatch alarm and invoke a Lambda function via EventBridge</strong></h4>\\n<p>To view the <a href=\\"https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/Create-CloudWatch-Events-Rule.html\\" target=\\"_blank\\">EventBridge rule</a>, complete the following steps:</p>\\n<ol>\\n<li>On the EventBridge console, choose <strong>Rules</strong> in the navigation pane.</li>\\n<li>Choose the rule.</li>\n<li>Review the information under <strong>Event pattern</strong> and <strong>Target</strong>.</li>\\n</ol>\n<p><img src=\\"https://dev-media.amazoncloud.cn/b60a07f186454039a154545083c10a9a_image.png\\" alt=\\"image.png\\" /></p>\n<p>Your EventBridge rule is configured to run the Lambda function when the CloudWatch alarm state changes.</p>\n<h4><a id=\\"Create_an_Amazon_RDS_read_replica_via_Lambda_115\\"></a><strong>Create an Amazon RDS read replica via Lambda</strong></h4>\\n<p>To view how the <a href=\\"https://docs.aws.amazon.com/lambda/latest/dg/getting-started-create-function.html\\" target=\\"_blank\\">Lambd­a function</a> creates a read replica, complete the following steps:</p>\\n<ol>\\n<li>On the Lambda console, choose <strong>Functions</strong> in the navigation pane.</li>\\n<li>Choose the function.</li>\n<li>In the <strong>Code source</strong> editor, review the <code>lambda_handler</code> Python function.</li>\\n</ol>\n<p><img src=\\"https://dev-media.amazoncloud.cn/57cb161f723241e291ea4cac90b2f9a5_image.png\\" alt=\\"image.png\\" /></p>\n<ul>\\n<li>The <code>lambda_handler</code> python function checks the alarm state and invokes <code>create_rds_read_replica</code> if the status is ALARM.</li>\\n</ul>\n<ol start=\\"4\\">\\n<li>In the <strong>Code source</strong> editor, review the <code>create_rds_read_replica</code> Python function.</li>\\n</ol>\n<p><img src=\\"https://dev-media.amazoncloud.cn/0a87b77e8e744e00980cbf4908130f8d_image.png\\" alt=\\"image.png\\" /></p>\n<p>The <code>create_rds_read_replica</code> Python function creates a read replica if the number of read replicas is less than your defined maximum number of replicas. The <code>describe_db_instances</code> [Amazon RDS](https://aws.amazon.com/cn/rds/?trk=cndc-detail) API function is used to determine the current number of read replicas for your database instance.</p>\\n<h4><a id=\\"Send_events_to_an_SNS_topic_via_Lambda_133\\"></a><strong>Send events to an SNS topic via Lambda</strong></h4>\\n<p>To review how the function sends events to Amazon SNS, complete the following steps:</p>\n<ol>\\n<li>On the Lambda console, choose Functions in the navigation pane.</li>\n<li>Choose the function.</li>\n<li>In the Code source editor, scroll to the <code>notify_application</code>Python function.</li>\\n</ol>\n<p><img src=\\"https://dev-media.amazoncloud.cn/32789a5cd9c54b54adcd89b32f551f38_image.png\\" alt=\\"image.png\\" /></p>\n<p>An event is sent to your application when the read replica is created and when the read replica is available via the SNS topic created by the CloudFormation template.</p>\n<h4><a id=\\"Subscribe_to_the_SNS_topic_145\\"></a><strong>Subscribe to the SNS topic</strong></h4>\\n<p>You must <a href=\\"https://docs.aws.amazon.com/code-samples/latest/catalog/python-sns-sns_basics.py.html\\" target=\\"_blank\\">write application code</a> to subscribe to the SNS topic, and offload read-only queries to the read replicas. The event includes the read replica instance status and connection endpoint. Your application should check for <code>DBInstanceStatus </code>status <code>available</code>. Use the connection endpoint to connect and perform read-only queries. The following is a sample event including the <code>DBInstanceStatus</code> status <code>available</code>:</p>\\n<pre><code class=\\"lang-\\">{\\n &quot;DBInstances&quot;: [{&quot;DBInstanceIdentifier&quot;: &quot;poc-read-replica-3&quot;,\\n &quot;DBInstanceClass&quot;: &quot;db.t3.small&quot;,\\n &quot;Engine&quot;: &quot;postgres&quot;,\\n &quot;DBInstanceStatus&quot;: &quot;available&quot;,\\n &quot;MasterUsername&quot;: &quot;admindba&quot;,\\n &quot;DBName&quot;: &quot;rdspoc&quot;,\\n &quot;Endpoint&quot;: {&quot;Address&quot;: &quot;frbpoc-read-replica-3.XXXXXXXXX.us-east-1.rds.amazonaws.com&quot;, &quot;Port&quot;: 5432, …\\n}\\n</code></pre>\\n<p>You can use this <a href=\\"https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Connecting.Python.html\\" target=\\"_blank\\">code sample</a> for connecting and querying a PostgreSQL database.<br />\\nThe walk-through is complete; you achieved horizontal scaling by adding read replicas when the CPU threshold exceeds the number of configured data points breached within the defined CloudWatch alarm evaluation period.</p>\n<h4><a id=\\"System_Integration_163\\"></a><strong>System Integration</strong></h4>\\n<p>Modifying the solution to automate system integration requires you to add code in the Lambda function. Within the Lambda source code, the <code>notify_application</code> Python function is a hook where you can automate system integration. The code provided in this blog publishes an [Amazon SNS](https://aws.amazon.com/cn/sns/?trk=cndc-detail) event. You can add code to communicate with systems such as <a href=\\"https://aws.amazon.com/blogs/architecture/managing-asynchronous-workflows-with-a-rest-api/\\" target=\\"_blank\\">calling an API to manage workflows</a>, or communicate with a <a href=\\"https://aws.amazon.com/blogs/compute/extending-saas-products-with-serverless-functions/\\" target=\\"_blank\\">software as a service (SaaS) solution</a>.</p>\\n<h4><a id=\\"Test_scalability_167\\"></a><strong>Test scalability</strong></h4>\\n<p>You can use a tool such as <a href=\\"https://www.postgresql.org/docs/current/pgbench.html\\" target=\\"_blank\\">pgbench</a> to generate load on your database to test scalability. For an example to generate load, refer to <a href=\\"https://aws.amazon.com/blogs/database/automate-benchmark-tests-for-amazon-aurora-postgresql/\\" target=\\"_blank\\">Automate benchmark tests for Amazon Aurora PostgreSQL</a>.</p>\\n<h4><a id=\\"Monitor_your_database_171\\"></a><strong>Monitor your database</strong></h4>\\n<p>A variety of tools are available to <a href=\\"https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/MonitoringOverview.html\\" target=\\"_blank\\">monitor</a> your database, including <a href=\\"https://aws.amazon.com/rds/performance-insights/\\" target=\\"_blank\\">Amazon RDS Performance Insights</a>, <a href=\\"https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_Monitoring.OS.html\\" target=\\"_blank\\">Amazon RDS Enhanced Monitoring</a>, <a href=\\"https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_LogAccess.html\\" target=\\"_blank\\">Amazon RDS database logs</a>, and <a href=\\"https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/\\" target=\\"_blank\\">Amazon CloudWatch Logs</a>. You can use these tools to help determine the threshold when you want to trigger a scaling event.</p>\\n<h4><a id=\\"Clean_up_175\\"></a><strong>Clean up</strong></h4>\\n<p>To avoid incurring future charges, delete the resources you created as part of this post. You can clean up the Amazon Web Services resources (Lambda function, CloudWatch alarm, EventBridge rule, KMS customer managed keys, and SNS topic) by <a href=\\"https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-delete-stack.html\\" target=\\"_blank\\">deleting the CloudFormation stack</a>.</p>\\n<h4><a id=\\"Conclusion_179\\"></a><strong>Conclusion</strong></h4>\\n<p>In this post, we provided an automated solution to horizontally scale Amazon RDS for PostgreSQL using an event-driven architecture. CloudWatch monitors your RDS database instance CPU. EventBridge watches for a CloudWatch alarm, and routes the event to a Lambda function. The function creates a read replica and notifies your application via Amazon SNS. You can add code in the Lambda function to automate system integration.</p>\n<p>When scaling your database, it’s important to <a href=\\"https://aws.amazon.com/blogs/database/optimizing-costs-in-amazon-rds/\\" target=\\"_blank\\">optimize cost</a> and follow <a href=\\"https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_BestPractices.html#CHAP_BestPractices.PostgreSQL\\" target=\\"_blank\\">best practices</a>. As you look at options to scale your RDS database instance, I encourage you to experiment scaling vertically and horizontally. The example in this post uses CPU to determine when a read replica is created. You can <a href=\\"https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create_Composite_Alarm.html\\" target=\\"_blank\\">create a composite alarm</a> using multiple metrics such as CPU and memory to refine the read replica creation criteria. You can also <a href=\\"https://aws.amazon.com/blogs/database/set-alarms-on-performance-insights-metrics-using-amazon-cloudwatch/\\" target=\\"_blank\\">set alarms from Performance Insights metrics</a>. Another consideration is to scale down by deleting the read replicas when they’re no longer needed. Close open connections to the read replica before deleting the read replica. You can create a similar solution as described in this post with a CloudWatch alarm based on a CPU threshold to scale down.</p>\\n<h4><a id=\\"About_the_Author_185\\"></a><strong>About the Author</strong></h4>\\n<p><img src=\\"https://dev-media.amazoncloud.cn/85220630e1f445f293c10f778ec3cc42_image.png\\" alt=\\"image.png\\" /></p>\n<p><strong>Andrew Love</strong> is a Sr. Solutions Architect in the Worldwide Public Sector at Amazon Web Services. He is passionate about helping customers build well-architected solutions to achieve their business needs. He enjoys spending time with his family, a good game of chess, home improvement projects, and writing code.</p>\n"}
目录
亚马逊云科技解决方案 基于行业客户应用场景及技术领域的解决方案
联系亚马逊云科技专家
亚马逊云科技解决方案
基于行业客户应用场景及技术领域的解决方案
联系专家
0
目录
关闭