我是靠谱客的博主 机智火车,最近开发中收集的这篇文章主要介绍如何使用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 "nn===== 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 "nn=============== 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 "nn=======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

 

最后

以上就是机智火车为你收集整理的如何使用AWS AWS Systems Manager 发送RDS查询的全部内容,希望文章能够帮你解决如何使用AWS AWS Systems Manager 发送RDS查询所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部