Skip to content

Instantly share code, notes, and snippets.

@david-sanabria
Last active March 30, 2017 20:20
Show Gist options
  • Save david-sanabria/a26a33fe2f85dc0f40d6496536e83d84 to your computer and use it in GitHub Desktop.
Save david-sanabria/a26a33fe2f85dc0f40d6496536e83d84 to your computer and use it in GitHub Desktop.
AWS CLI - Test for S3 Bucket Existence
#!/bin/sh
if [[ $(aws s3api list-buckets --query 'Buckets[?starts_with(Name,`my-bucket`)].[Name]' --output text) = 'my-bucket' ]]; then
echo "You're a genius. Now do something useful";
fi
## Alternate Syntax
if [[ $(aws s3api list-buckets --query 'Buckets[?Name == `my-bucket`].[Name]' --output text) = 'my-bucket' ]]; then
echo "You're a specific genius. Now do something useful";
fi
@david-sanabria
Copy link
Author

This is a very simple example of a very powerful capability of Amazon's command line utility. Not only can I create stuff, I but i can use JMESPath queries to validate that I created it. This is useful for doing conditional evaluation (test if its there before using it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment