-1

I want to understand the idiomatic Bash (ie the Bash equivalent of 'Pythonic') way with the AWS CLI to create an AWS resource (say an AWS Cloud9 EC2 environment) and then 'poll'/'wait'/'sleep' for that resource to reach a certain state; when the AWS resources reaches the given state, the command completes and the user is able to enter commands back into the shell.

EG after the user hits Enter

[cloudshell-user@ip-10-10-10-10 ~]$ #an idiomatic Bash one-liner that combines 'aws cloud9 create-environment-ec2 --name this-resource-will-take-a-moment-to-create --instance-type t2.micro' and 'aws cloud9 describe-environments --environment-ids tHiSiDwAsThEoUtPuToFtHePrEvIoUsCoMmAnD'

Then after some time the user will see

[cloudshell-user@ip-10-10-10-10 ~]$ #an idiomatic Bash one-liner that combines 'aws cloud9 create-environment-ec2 --name this-resource-will-take-a-moment-to-create --instance-type t2.micro' and 'aws cloud9 describe-environments --environment-ids tHiSiDwAsThEoUtPuToFtHePrEvIoUsCoMmAnD'
[cloudshell-user@ip-10-10-10-10 ~]$

at which point the AWS Cloud9 EC2 environment has reached a given state.

As the create(create-,launch-, etc)/wait(get-/describe-) pattern is common among AWS services API calls and then corresponding AWS CLI commands the one-liner should be re-usable given minor changes for other services.

The Bash one-liner will be used in the CloudShell but should be portable to be used in other installations of Bash.

I have read about && and sleep but would like some direction as to how to integrate these commands (or any alternative ones) in a idiomatic Bash one-liner. Also I am concerned about not violating any throttling or request limiting AWS APIs have.

  • You might consider writing a Python script using boto3 Waiters (see [here](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/clients.html)). – jarmod Dec 16 '22 at 15:03
  • I'll investigate Boto3 waiters (not only for this project but for others), thanks. But I'd like to focus on the AWS CLI as I plan on instructing a non-technical user/customer to issue these commands in a AWS CloudShell (the Cloud9 EC2 environment is where I could ask the user to write and execute Python code), possibly over the phone or email. There is also the issue of Boto/PyPi version, where the script will be created from and saved, how many characters the Python script will be compared to a one-liner, etc. With the AWS CloudShell the only requirements would be Bash and the AWS CLI. – George DeGennaro Dec 16 '22 at 15:10
  • One generic pattern in bash (awscli aside) is: `until some-command-here; do sleep 5; done`. Other ideas [here](https://stackoverflow.com/questions/12321469/retry-a-bash-command-with-timeout). Example for testing with: `until test -f /tmp/fred; do sleep 5; done` and then in another shell run `echo > /tmp/fred`. Maybe you can combine this pattern with aws list/describe commands. – jarmod Dec 16 '22 at 15:27

1 Answers1

0

As jarmod commented with 'boto3 Waiters', I looked more into the AWS CLI's Github Issues and found this Issue

The underlying library that powers the CLI, botocore, has this capability already via a concept it calls "waiters", which is a data driven way to describe waiting for a resource to reach a certain state... We just need to come up with a suitable API to expose this in the CLI.

The capability was implemented in aws-cli but each service has to then use that capability for their own service.

It looks at the time of this writing the Cloud9 part of the aws-cli does not support wait.