bash – 如果存在特定的rds实例,如何通过aws cli检查?

前端之家收集整理的这篇文章主要介绍了bash – 如果存在特定的rds实例,如何通过aws cli检查?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果已存在具有特定名称的AWS RDS实例,我希望我的bash脚本检测到.

这是我试过的:

  1. #!/usr/bin/env bash
  2.  
  3. DBINSTANCEIDENTIFIER=greatdb
  4.  
  5. EXISTINGINSTANCE=$(aws rds describe-db-instances \
  6. --db-instance-identifier="$DBINSTANCEIDENTIFIER" \
  7. --output text\
  8. )

我希望有一个空列表,或者结果为零 – 但是我收到一条错误消息:

  1. An error occurred (DBInstanceNotFound) when calling the DescribeDBInstances operation: DBInstance greatdb not found.

有没有人知道如何正确地找出实例是否存在,没有错误信息?

使用–db-instance-indentifier时请注意文档中的约束:
  1. --db-instance-identifier (string)
  2.  
  3. The user-supplied instance identifier. If this parameter is specified,information from only the specific DB instance is returned. This parameter
  4. isn't case-sensitive.
  5.  
  6. Constraints:
  7. - If supplied,must match the identifier of an existing DBInstance

因此,如果您知道DB实际存在,则只能使用此选项.

使用查询

搜索可能存在或不存在的数据库,您必须使用–query选项:

  1. $aws rds describe-db-instances \
  2. --query 'DBInstances[*].[DBName,DBInstanceIdentifier]' --output text

可以在awscli帮助中访问DBINstances JSON结构:

  1. $aws rds describe-db-instances help --output text
  2. ...
  3. ...
  4. {
  5. "DBInstances": [
  6. {
  7. "PubliclyAccessible": false,"MasterUsername": "mymasteruser","MonitoringInterval": 0,"LicenseModel": "general-public-license",...
  8. ...
  9. "DBName": "sample",...
  10. ...
  11. "DBInstanceStatus": "stopped","EngineVersion": "5.6.27","AvailabilityZone": "us-east-1e","StorageType": "standard","StorageEncrypted": false,"DBInstanceClass": "db.t2.micro","DbInstancePort": 0,"DBInstanceIdentifier": "mydbinstance-1"
  12. }
  13. ]
  14. }
  15. ...
  16. ...

使用过滤器

初始问题的另一个简单解决方案是使用–filters参数.查询将返回实例标识符(如果实例存在)或空字符串(如果它不存在):

  1. #!/usr/bin/env bash
  2.  
  3. DBINSTANCEIDENTIFIER="greatdb"
  4. EXISTINGINSTANCE=$(aws rds describe-db-instances \
  5. --query 'DBInstances[*].[DBInstanceIdentifier]' \
  6. --filters Name=db-instance-id,Values=$DBINSTANCEIDENTIFIER \
  7. --output text \
  8. )
  9.  
  10. if [ -z $EXISTINGINSTANCE ]
  11. then
  12. echo "instance $DBINSTANCEIDENTIFIER does not exist!"
  13. else
  14. echo "instance $DBINSTANCEIDENTIFIER exists!"
  15. fi

参考

> https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-instances.html
> https://stackoverflow.com/questions/45449174/how-do-i-use-query-parameter-in-aws-rds-describe-db-instances-command
> https://stackoverflow.com/questions/46051538/syntax-for-filters-for-aws-rds-describe-db-instances

猜你在找的Bash相关文章