Guest User

Untitled

a guest
Dec 13th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. # AWS Helpers functions
  2. #
  3. # prerequisites:
  4. # - awscli
  5. # - jq
  6.  
  7. # parse aws arn
  8. parse_arn() {
  9. echo $1 | sed 's/\"//g;s/^.*\///'
  10. }
  11.  
  12. # get hostname from instance tag name
  13. hostname_from_instance() {
  14. echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PublicDnsName' | tr -d '"')
  15. }
  16.  
  17. # get ip from instance tag name
  18. ip_from_instance() {
  19. echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PublicIpAddress' | tr -d '"')
  20. }
  21.  
  22. # ssh to instance using tag name
  23. ssh_aws() {
  24. ssh $(hostname_from_instance $1)
  25. }
  26.  
  27. # get cloudformation stack status
  28. stack_status() {
  29. aws cloudformation describe-stacks --stack-name $1 | jq '.Stacks[0].StackStatus'
  30. }
  31.  
  32. # get cloudformation stacks and status
  33. stacks() {
  34. stacks=$(aws cloudformation list-stacks --stack-status-filter CREATE_IN_PROGRESS CREATE_COMPLETE ROLLBACK_IN_PROGRESS ROLLBACK_FAILED ROLLBACK_COMPLETE DELETE_IN_PROGRESS DELETE_FAILED UPDATE_IN_PROGRESS UPDATE_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_COMPLETE UPDATE_ROLLBACK_IN_PROGRESS UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_ROLLBACK_COMPLETE REVIEW_IN_PROGRESS --query 'StackSummaries[*].[StackStatus,StackName]' --output text | sort -k 2 | column -t)
  35. if [[ $# -ge 1 ]]
  36. then
  37. echo $stacks | grep -i $1
  38. else
  39. echo $stacks | less -FS
  40. fi
  41. }
  42.  
  43. # get cloudformation stack resources and status
  44. stack_resources() {
  45. aws cloudformation describe-stack-resources --stack-name $1 --query 'StackResources[*].[ResourceStatus,ResourceType,LogicalResourceId,PhysicalResourceId]' --output text | sort -k2 | column -t | less -FS
  46. }
  47.  
  48. # get cloudformation stack parameters
  49. stack_params() {
  50. aws cloudformation describe-stacks --stack-name $1 --query 'Stacks[*].Parameters[*].[ParameterKey,ParameterValue]' #--output text | sort | column -t
  51. }
  52.  
  53. # get cloudformation stack outputs
  54. stack_outputs() {
  55. aws cloudformation describe-stacks --stack-name $1 --query 'Stacks[*].Outputs[*].[OutputKey,OutputValue]' --output text | sort | column -t
  56. }
  57.  
  58. # get ecs clusters
  59. ecs_clusters() {
  60. clusters="
  61. for c in $(aws ecs list-clusters | jq .clusterArns[])
  62. do
  63. parse_arn $c
  64. done)"
  65. if [[ $# -ge 1 ]]
  66. then
  67. echo $clusters | sed 's/ /\n/g' | grep -i $1
  68. else
  69. echo $clusters | sed 's/ /\n/g'
  70. fi
  71. }
  72.  
  73. # get ecs services from cluster
  74. ecs_services() {
  75. services="
  76. for s in $(aws ecs list-services --cluster $1 | jq .serviceArns[])
  77. do
  78. parse_arn $s
  79. done)"
  80. if [[ $# -ge 2 ]]
  81. then
  82. echo $services | sed 's/ /\n/g;s/\"//' | grep -i $2
  83. else
  84. echo $services | sed 's/ /\n/g;s/\"//'
  85. fi
  86. }
  87.  
  88. # get ecs tasks from cluster and service name
  89. ecs_service_tasks() {
  90. aws ecs list-tasks --cluster $1 --service-name $2
  91. }
  92.  
  93. # stop ecs task from cluster and service name
  94. ecs_stop_task() {
  95. aws ecs stop-task --cluster $1 --task $2
  96. }
Add Comment
Please, Sign In to add comment