aws-cloudformation - 创建 RDS 数据库用户 CloudFormation

标签 aws-cloudformation amazon-rds aws-cloudformation-custom-resource

由于 CloudFormation 本身不支持为 RDS 数据库创建数据库用户,因此我正在寻找通过 CustomResource 执行此操作的方法。但是,即使我编写由 Lambda 函数支持的 CustomResource,我也看不到允许我将用户添加到数据库实例的 RDS API 端点。

有人可以建议为 Postgres 10 数据库引擎支持的 Aurora 集群创建数据库用户的潜在方法吗?

最佳答案

I do not see an RDS API endpoint that would allow me to add a user to a database instance.

通常,您会将自定义资源设置为在创建 RDS 后触发。因此,您可以使用函数环境变量将 RDS 端点 URL 传递给 lambda。

实际上DependsOn自定义资源上的属性可用于确保自定义资源在成功创建 RDS 后触发。如果您通过环境变量传递 RDS url,则实际上不需要。

使用使用 pymysql 的示例 lambda 更新代码:

     
  MyLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.lambda_handler
      Role: !Ref ExecRoleArn
      Runtime: python3.7
      Environment:
        Variables: 
          DB_HOSTNAME: !Ref DbHostname
          DB_USER: !Ref DbMasterUsername
          DB_PASSWORD: !Ref DbMasterPassword
          DB_NAME: !Ref DbName
      VpcConfig:
        SecurityGroupIds: [!Ref SecurityGroupId]
        SubnetIds: !Ref SubnetIds
      Code:
        ZipFile: |
            import base64
            import json
            import os
            import logging
            import random
            import sys


            import pymysql
            import boto3

            rds_host  = os.environ['DB_HOSTNAME']
            rds_user = os.environ['DB_USER']
            rds_password = os.environ['DB_PASSWORD']
            rds_dbname = os.environ['DB_NAME']

            logger = logging.getLogger()
            logger.setLevel(logging.INFO)

            try:
                
                conn = pymysql.connect(rds_host, 
                                       user=rds_user,
                                       passwd=rds_password, 
                                       db=rds_dbname, 
                                       connect_timeout=5)
            except:
                logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
                sys.exit()          

            def lambda_handler(event, context):

                print(json.dumps(event))

                with conn.cursor() as cur:

                    cur.execute("create table if not exists Employee (EmpID  int NOT NULL auto_increment, Name varchar(255) NOT NULL, PRIMARY KEY (EmpID))")

                    conn.commit()

    
                return {
                    'statusCode': 200,
                    'body': ""
                }     


      Timeout: 60 # 
      MemorySize: 128
      Layers: 
        - arn:aws:lambda:us-east-1:113088814899:layer:Klayers-python37-PyMySQL:1


关于aws-cloudformation - 创建 RDS 数据库用户 CloudFormation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62865660/

相关文章:

ssh - EC2 : how to bundle an ssh private key into a private AMI?

amazon-web-services - 如何在 AWS 云形成中实现嵌套堆栈?

aws-lambda - RDS 服务触发 Lambda

amazon-web-services - AWS 免费套餐提醒

amazon-web-services - 使用 AWS CDK 创建 VPC 时如何排除路由表及其路由的默认创建

amazon-web-services - 通过 CloudFormation 从 xtrabackup 恢复 AWS RDS

sql-server - 将 SQL SERVER (RDS) 事务实时流式传输到 NoSQL

aws-cloudformation - AWS Cloud Formation 带有参数的多个路由条目

amazon-web-services - 如果存储桶存在,如何强制 CloudFormation 创建一个具有相似名称的新存储桶?

amazon-web-services - 在 EC2 路由中使用 PrefixList