Using AWS Profiles with Boto locally and in AWS
I'm not a programmer by any stretch of the imagination. I can write some code, I can build something that works, but I find writing code is one of those things that starts out nice and simple, but as you scratch the surface it gets more and more complicated until you've spent more time building support structures than you have actually implementing logic.
Which is fine for a corporate that needs control, wants proper error handling and needs something to work. A lot of the code I write is because I'm too lazy to do things manually and want things to just happen.
The following little tidbit of code has changed my life:
aws_profile='personal'
try:
boto3.setup_default_session(profile_name=aws_profile)
except:
boto3.setup_default_session()
Basically what this will do is to try and use the profile named in the first line. That's going to be the default method of operating on your machine (I use my personal profile most of the time). If that fails, as it will in AWS contexts where you've configured roles properly (you have configured roles properly haven't you?), it will fall back and use the context it's running in.
What this allows me to do is run the same code on my machine as Lambda, ECS, EC2, etc. and it's all managed by those 5 magic lines.
Simple, probably not best practice, but for my needs meets 100% of my requirements.