如何使用AWS AWS Systems Manager 发送RDS查询

SSM是可以向机器集群中发送命令或执行脚本的一种方式。

AWS Systems Manager (Systems Manager) was formerly known as "Amazon Simple Systems Manager (SSM)" and "Amazon EC2 Systems Manager (SSM)". The original abbreviated name of the service, "SSM", is still reflected in various AWS resources, including a few other service consoles. 

AWS系统管理的前身是“亚马逊简单系统管理(SSM)”和“亚马逊EC2系统管理(SSM)”。这个缩写“SSM”在AWS一些资源如终端(endpoint 等)以及一些控制台中还在继续使用。

下面我们举个例子看看如何执行一个sql 查询。一种做法是我们可以ssh 登陆到一个可以访问到RDS 的一台机器上,并且这台装有mysql的客户端,那么我们使用mysql cli 命令行去执行操作,但是假如我们没有权限怎么办呢?通常我们的CI/CD机器都有访问aws的权限,这样我们就可以用jenkins job 来完成这个操作。这里我们结合jenkins job 和SSM command 来看看如何完成这个操作。

首先我们在AWS VPC 里创建一个跳板机(bastion vm),用这个跳板机去访问RDS 服务如MYSQL/Postgres 等数据库系统。

Bash script

为了方便访问RDS,我们可以建立一个config 文件,这个放在能访问RDS的具有公有ip的那台机器上的,这里是跳板集。

内容如下:

==== /etc/mysql/conf/.mysql.cnf ======
[mysql]
host=rds.internal.dccompany.com
user=clouduser
password=clouduserpassword
database=test_my_db

==== /etc/mysql/conf/.mysql.cnf end ======

# 通过使用命令行参数 -e 来执行sql 语句。这里使用config文件连接是因为conf文件里包含连接数据库的用户名密码和数据库信息,可以防止用户名密码以明文方式暴露出来。

selectMySQLCommand="mysql --defaults-extra-file='/etc/mysql/conf/.mysql.cnf' -e 'select * from test_table'"

deleteMySQLCommand="mysql --defaults-extra-file='/etc/mysql/conf/.mysql.cnf' -e 'delete from test_table'"

region=$(aws configure get region --profile ${profile})

AccountId=$(aws sts get-caller-identity --output text --query Account --profile ${profile})

PublicBastionInstanceId=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=public_bastion_vm" --region ${region} --profile ${profile}| grep InstanceId | cut -d: -f2 | cut -d\" -f2)
echo -e "\nruning on profile:$profile, region:$region, account:$AccountId, PublicBastionInstanceId:$PublicBastionInstanceId"

isToDelete=$1

function executeSql {
    command="$1"

    echo "excuting sql:" $command

# 第一步,发送命令,并异步执行命令 (aws ssm send-command...)

    Result=$(aws ssm send-command --document-name "AWS-ExecuteSqlCMD" --instance-ids "${PublicBastionInstanceId}" --parameters "{\"commands\":[\"${command}\"], \"executionTimeout\":[\"1800\"]}" --timeout-seconds 600 --region ${region} --profile ${profile})


    echo -e "\nsending cmd result:" "$Result"

    cmdid=$(echo "$Result"|grep CommandId|cut -d: -f2 | cut -d\" -f2)

    echo -e "\ncmdid is :" $cmdid

    echo -e "\n\n===== waiting for command $cmdid to complete ====="

 

# 第二步,循环查询命令状态(aws ssm list-commands ...),正在执行(pending), 执行结果(Success 还是Failed)

    status=$(aws ssm list-commands --command-id ${cmdid} --region ${region} --profile ${profile}| grep Status\"|cut -d\" -f4)
    while [ "$status" -ne "Success" ]
        do
         sleep 1 # sleep one second
         status=$(aws ssm list-commands --command-id ${cmdid} --region ${region} --profile ${profile}| grep Status\"|cut -d\" -f4)
    done

    echo -e "\n\n=============== Command [${cmdid}] executed successfully ============================"

 

# 第二步,查询命令输出(aws ssm list-command-invocations ...)

    resultset=$(aws ssm list-command-invocations --command-id ${cmdid} --region $region --profile $profile --details)

    output=$(echo "$resultset" | grep Output\" | cut -d\" -f4)

    echo -e "$output"
}

executeSql "$selectMySQLCommand"

# 如果要执行删除操作,可以设置isToDelete=true

if [ "$isToDelete" = "true" ]  # shell 里,如何比较字符串可以参考 https://linuxize.com/post/how-to-compare-strings-in-bash/
then
    echo -e "\n\n=======is delete: $isdelete ======="
    executeSql "$deleteMySQLCommand"

fi

 

注:

在bash shell 中,判读字符串是否相等:

  • string1 = string2 and string1 == string2.   等号 = 用在 test ([)双等号 == 用在 [[ 
  • string1 != string2 - 字符串不相等
  • string1 =~ regex 正则表达式匹配
  • string1 > string2  按字典顺序比较
  • string1 < string2  按字典顺序比较
  • -z string - 字符串长度为0.
  • -n string - 字符串长度不等于0.

 

引用:

https://linuxize.com/post/how-to-compare-strings-in-bash/

https://docs.aws.amazon.com/systems-manager/latest/userguide/how-it-works.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值