Share #10 Lập Lịch Tự Động Tắt Amazon RDS Cluster với AWS Lambda và Amazon EventBridge

minhthu472

New member
## Cách tạo cụm Amazon RDS với AWS Lambda và Amazon EventBridge

Dịch vụ cơ sở dữ liệu quan hệ Amazon (RDS) là một dịch vụ được quản lý, cung cấp một nền tảng an toàn và đáng tin cậy, có thể mở rộng và bảo mật để lưu trữ cơ sở dữ liệu quan hệ trên đám mây.AWS Lambda là một dịch vụ tính toán không có máy chủ cho phép bạn chạy mã mà không cần phải cung cấp hoặc quản lý máy chủ.Amazon EventBridge là một bus sự kiện không có máy chủ cho phép bạn kết nối các dịch vụ AWS khác nhau và các sự kiện kích hoạt dựa trên các điều kiện nhất định.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách tạo cụm Amazon RDS với AWS Lambda và Amazon Eventbridge.Chúng tôi sẽ sử dụng AWS Lambda để tạo trình kích hoạt sẽ được gọi khi một cơ sở dữ liệu mới được tạo trong cụm RDS.Trình kích hoạt sau đó sẽ gửi một sự kiện đến Amazon EventBridge, sẽ được sử dụng để tạo thông báo trong Dịch vụ Thông báo Simple (SNS) của Amazon.

### Điều kiện tiên quyết

Để hoàn thành hướng dẫn này, bạn sẽ cần những điều sau đây:

* Tài khoản AWS
* Một cụm Amazon RDS
* AWS CLI
* AWS Sam Cli

### Bước 1: Tạo cụm Amazon RDS

Để tạo cụm Amazon RDS, hãy làm theo các bước dưới đây:

1. Chuyển đến Bảng điều khiển quản lý AWS và mở bảng điều khiển Amazon RDS.
2. Nhấp vào ** Tạo cơ sở dữ liệu **.
3. Chọn tùy chọn ** cơ sở dữ liệu quan hệ **.
4. Chọn công cụ cơ sở dữ liệu mà bạn muốn sử dụng.Trong hướng dẫn này, chúng tôi sẽ sử dụng MySQL.
5. Nhập tên của thể hiện cơ sở dữ liệu.
6. Chọn kích thước thể hiện mà bạn muốn sử dụng.
7. Chọn số lượng phiên bản cơ sở dữ liệu mà bạn muốn tạo.
8. Chọn loại lưu trữ mà bạn muốn sử dụng.
9. Nhập mật khẩu cho quản trị viên cơ sở dữ liệu.
10. Nhấp vào ** Tạo cơ sở dữ liệu **.

### Bước 2: Tạo chức năng AWS Lambda

Để tạo chức năng AWS Lambda, hãy làm theo các bước dưới đây:

1. Đi đến Bảng điều khiển quản lý AWS và mở bảng điều khiển AWS Lambda.
2. Nhấp vào ** Tạo chức năng **.
3. Chọn tùy chọn ** Tác giả từ đầu **.
4. Nhập tên cho chức năng.
5. Chọn thời gian chạy ** Python 3.8 **.
6. Nhấp vào ** Tạo chức năng **.

### Bước 3: Viết mã chức năng AWS Lambda

Mã sau đây sẽ được sử dụng để tạo trình kích hoạt sẽ được gọi khi cơ sở dữ liệu mới được tạo trong cụm RDS.Trình kích hoạt sau đó sẽ gửi một sự kiện đến Amazon EventBridge, sẽ được sử dụng để tạo thông báo trong Amazon SNS.

`` `Python
Nhập boto3

Def Lambda_Handler (Sự kiện, Bối cảnh):
# Nhận ID cụm RDS từ sự kiện.
Cluster_id = event ['dbclusteridentifier']]

# Tạo một khách hàng cho Amazon RDS.
rds_client = boto3.client ('rds')

# Tạo một kích hoạt cho cụm RDS.
Trigger = rds_client.create_trigger (
Sourcetype = 'db_cluster_event',
Sourceidentifier = Cluster_id,
Categories = [
'db_cluster_created',
'db_cluster_deleted',
'db_cluster_lyamed',
'db_cluster_restored',
],
TriggerName = 'RDSClusterTrigger',
Enable = true,
)

# Nhận arn kích hoạt.
Trigger_arn = Trigger ['Triggerarn']

# Tạo một khách hàng cho Amazon EventBridge.
EventBridge_Client = boto3.Client ('EventBridge')

# Tạo quy tắc sự kiện cho kích hoạt.
event_rule = eventbridge_client.put_rule (
Name = 'rdsclustereventrule',
EventPottern = {
'Nguồn': ['aws.rds'],
'Kiểu chi tiết': ['Sự kiện cụm RDS'],
'chi tiết': {
'SourceIdentifier': [Cluster_id],
},
},
State = 'đã kích hoạt',
)

# Tạo một hành động cho quy tắc sự kiện.
hành động = {
'Loại': 'SNS',
'Targetarn': 'ARN: AWS: SNS: US-EAST-1: 123456789012: Mytopic',
}

# Thêm hành động vào quy tắc sự kiện.
EventBridge_Client.put_targets (
Rulename = 'rdsclustereventrule',
Mục tiêu = [hành động],
)

# Trả lại một phản hồi thành công.
trở lại {
'StatusCode': 200,
'thân hình'
=======================================
## How to Create an Amazon RDS Cluster with AWS Lambda and Amazon EventBridge

Amazon Relational Database Service (RDS) is a managed service that provides a reliable, scalable, and secure platform for hosting relational databases in the cloud. AWS Lambda is a serverless compute service that allows you to run code without having to provision or manage servers. Amazon EventBridge is a serverless event bus that allows you to connect different AWS services and trigger events based on certain conditions.

In this tutorial, we will show you how to create an Amazon RDS cluster with AWS Lambda and Amazon EventBridge. We will use AWS Lambda to create a trigger that will be invoked when a new database is created in the RDS cluster. The trigger will then send an event to Amazon EventBridge, which will be used to create a notification in Amazon Simple Notification Service (SNS).

### Prerequisites

To complete this tutorial, you will need the following:

* An AWS account
* An Amazon RDS cluster
* The AWS CLI
* The AWS SAM CLI

### Step 1: Create an Amazon RDS Cluster

To create an Amazon RDS cluster, follow the steps below:

1. Go to the AWS Management Console and open the Amazon RDS console.
2. Click **Create database**.
3. Select the **Relational database** option.
4. Choose the database engine that you want to use. In this tutorial, we will use MySQL.
5. Enter the name of the database instance.
6. Select the instance size that you want to use.
7. Choose the number of database instances that you want to create.
8. Select the storage type that you want to use.
9. Enter the password for the database administrator.
10. Click **Create database**.

### Step 2: Create an AWS Lambda Function

To create an AWS Lambda function, follow the steps below:

1. Go to the AWS Management Console and open the AWS Lambda console.
2. Click **Create function**.
3. Select the **Author from scratch** option.
4. Enter a name for the function.
5. Select the **Python 3.8** runtime.
6. Click **Create function**.

### Step 3: Write the AWS Lambda Function Code

The following code will be used to create a trigger that will be invoked when a new database is created in the RDS cluster. The trigger will then send an event to Amazon EventBridge, which will be used to create a notification in Amazon SNS.

```python
import boto3

def lambda_handler(event, context):
# Get the RDS cluster ID from the event.
cluster_id = event['DBClusterIdentifier']

# Create a client for Amazon RDS.
rds_client = boto3.client('rds')

# Create a trigger for the RDS cluster.
trigger = rds_client.create_trigger(
SourceType='db_cluster_event',
SourceIdentifier=cluster_id,
EventCategories=[
'db_cluster_created',
'db_cluster_deleted',
'db_cluster_renamed',
'db_cluster_restored',
],
TriggerName='RDSClusterTrigger',
Enabled=True,
)

# Get the trigger ARN.
trigger_arn = trigger['TriggerArn']

# Create a client for Amazon EventBridge.
eventbridge_client = boto3.client('eventbridge')

# Create an event rule for the trigger.
event_rule = eventbridge_client.put_rule(
Name='RDSClusterEventRule',
EventPattern={
'source': ['aws.rds'],
'detail-type': ['RDS Cluster Event'],
'detail': {
'SourceIdentifier': [cluster_id],
},
},
State='ENABLED',
)

# Create an action for the event rule.
action = {
'type': 'SNS',
'targetArn': 'arn:aws:sns:us-east-1:123456789012:MyTopic',
}

# Add the action to the event rule.
eventbridge_client.put_targets(
RuleName='RDSClusterEventRule',
Targets=[action],
)

# Return a success response.
return {
'statusCode': 200,
'body'
 
Đưa ra kịch bản sau:

- Một cụm Amazon RDS được tạo bằng cơ sở dữ liệu PostgreSQL.
- Một quy tắc của Amazon EventBridge được tạo ra để tạo các sự kiện trên cơ sở dữ liệu PostgreSQL.
- Hàm AWS Lambda được tạo ra được kích hoạt bởi Quy tắc EventBridge của Amazon.

Một vấn đề tiềm năng với kịch bản này là gì?
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock