3 min read

Learn how to write Python in an AWS Cloud9 for free!

Learn how to write Python in an AWS Cloud9 for free!

There probably isn't a better place to learn than on Cloud9. You won't have to worry about setting up your machine, installing dependencies, updates, etc. Just spin up an instance in the free tier and away you go!


  1. AWS Account
  2. There is no second prereq. All you need is an account that works.

Now let's deploy an environment!

Open up AWS and select or search for Cloud9


And click Create environment. Pick whatever you want for the name and description and leave the rest default. Click Create.


It will take a few minutes to create the environment so go grab a coffee. Next, we'll open it up and start writing python!

Now open the environment and create a new hello.py file and paste the contents of this code:

import sys

print('Hello, World!')

print('The sum of 2 and 3 is 5.')

sum = int(sys.argv[1]) + int(sys.argv[2])

print('The sum of {0} and {1} is {2}.'.format(sys.argv[1], sys.argv[2], sum))

Go ahead and run it!

If you're running python without arguments you can use the green Run button, but we want to feed two numbers into the script: 5, and 9.

Click Run > Run Configurations > New run configurations and in the command box, paste this:

hello.py 5 9

And hit Run


Congratulations! You've run a Python script in the cloud!

Now let's make your code actually do something

To add functionality to Python we can install modules such as boto3. Boto3 is an SDK written by AWS so Python can interface with the AWS API. This lets us use Python to make AWS do things for us like... Create S3 buckets!

In the bottom half of the IDE, switch over to the bash tab and check that pip is installed(which it is), and then install boto3 with:

sudo python3 -m pip install boto3  # Install boto3 for Python 3.

python -m pip show boto3            # Verify boto3 is installed for the current version of Python.

Now create a new file called s3.py and paste the following into it

import sys
import boto3
from botocore.exceptions import ClientError

def list_my_buckets(s3_resource):
    print('Buckets:\n\t', *[b.name for b in s3_resource.buckets.all()], sep="\n\t")

def create_and_delete_my_bucket(s3_resource, bucket_name, keep_bucket):

        print('\nCreating new bucket:', bucket_name)
        bucket = s3_resource.create_bucket(
                'LocationConstraint': s3_resource.meta.client.meta.region_name
    except ClientError as e:
        print(f"Couldn't create a bucket for the demo. Here's why: "


    if not keep_bucket:
        print('\nDeleting bucket:', bucket.name)

        print('\nKeeping bucket:', bucket.name)

def main():
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('bucket_name', help='The name of the bucket to create.')
    parser.add_argument('region', help='The region in which to create your bucket.')
    parser.add_argument('--keep_bucket', help='Keeps the created bucket. When not '
                                              'specified, the bucket is deleted '
                                              'at the end of the demo.',

    args = parser.parse_args()
    s3_resource = (
        boto3.resource('s3', region_name=args.region) if args.region
        else boto3.resource('s3'))
        create_and_delete_my_bucket(s3_resource, args.bucket_name, args.keep_bucket)
    except ClientError:
        print('Exiting the demo.')

if __name__ == '__main__':

Now go ahead and create a new Run Configuration like we did before and use this as the command, but make sure to replace the bucket name with something completely unique. Just throw some jibberish in there, it's fine.

s3.py my-new-bucket-as8ifne6y3cj us-east-2

Go ahead and run the code. A few notes:

Boto3 has a strange behavior where it will create a bucket in your default region when you don't specify locationconstraint. But our code does specify the constraint so if your default region is us-east-1, then set us-east-2 as your s3 region. Here is some documentation where they account for this.

Here is what the code is supposed to do:

  1. Lists all the existing buckets before it creating anything
  2. Creates the bucket and shows you the new one
  3. It will delete the S3 bucket immediately before exiting - This is expected

Congrats! You've now completed the bonus exercise of having your python code actually do something useful! Pat yourself on the back, cause next up we'll do something even better. Maybe a lambda?