{"id":1176,"date":"2022-08-31T14:52:59","date_gmt":"2022-08-31T14:52:59","guid":{"rendered":"https:\/\/www.geekmungus.co.uk\/?p=1176"},"modified":"2022-11-05T10:53:18","modified_gmt":"2022-11-05T10:53:18","slug":"aws-with-python3-using-boto3-quickstart-linux","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=1176","title":{"rendered":"AWS with Python3 using Boto3 Quickstart (Linux)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Following on from the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.geekmungus.co.uk\/aws-cli-quick-start-linux\" target=\"_blank\">https:\/\/www.geekmungus.co.uk\/aws-cli-quick-start-linux<\/a> guide, let&#8217;s see about how you can connect to your AWS from Python3 using Boto3. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;ve not already done it, and if you&#8217;re not using the &#8220;default&#8221; credentials, you can specify a particular profile to use by adding it to an environment variable for example the below; where the &#8220;londonaws&#8221; is the profile name as written in the <strong>~\/.aws\/config<\/strong> file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export AWS_PROFILE=londonaws<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In my example I&#8217;m assuming Python3 is already installed, so we&#8217;ll create a virtual environment first with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install python3-virtualenv\n\nmkdir venv\ncd venv\n\nvirtualenv aws1\nsource \/home\/&lt;username&gt;\/venv\/aws1\/bin\/activate<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As we&#8217;ve switched into our virtualenv with that last command (source) we can then install boto3 using pip.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install boto3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Right now we&#8217;re ready to rumble, assuming you have an active session open with AWS, the script using boto3 can make use of the existing AWS session. If you get any access errors see the previous guide (link at the top of this post) to ensure you have an active session.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s create a cheeky script to just access AWS and pull out a list of buckets, create the following python script file, ensure you mark it as executable when finished with: <strong>chmod u+x test.py<\/strong>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n\nimport boto3\n\ns3 = boto3.resource('s3')\n\nfor bucket in s3.buckets.all():\n    print(bucket.name)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If we then run this script we should see an output that displays the names of any buckets within our S3 storage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Again the boto3 documentation is pretty good to help you get started see the link:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/boto3.amazonaws.com\/v1\/documentation\/api\/latest\/guide\/quickstart.html\">https:\/\/boto3.amazonaws.com\/v1\/documentation\/api\/latest\/guide\/quickstart.html<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/unsplash.com\/@pericakalimerica\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/unsplash.com\/@pericakalimerica<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following on from the https:\/\/www.geekmungus.co.uk\/aws-cli-quick-start-linux guide, let&#8217;s see about how you can connect to your AWS from Python3 using Boto3. If you&#8217;ve not already done it, and if you&#8217;re not using the &#8220;default&#8221; credentials, you can specify a particular profile to use by adding it to an environment variable for example the below; where the &#8230; <a title=\"AWS with Python3 using Boto3 Quickstart (Linux)\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=1176\" aria-label=\"Read more about AWS with Python3 using Boto3 Quickstart (Linux)\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":1171,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,3],"tags":[],"class_list":["post-1176","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","category-cloud"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1176"}],"version-history":[{"count":1,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1176\/revisions"}],"predecessor-version":[{"id":1304,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1176\/revisions\/1304"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}