top of page
  • Writer's pictureParag

Python SDK in AWS CLoud9 IDE

Updated: Jul 29, 2021

Learn to run Python code in an AWS Cloud9 IDE


Objectives:

  1. Learn to install Python SDK.

  2. Learn to create an S3 bucket using Python SDK.

Step 1: Cloud9 operates in some specific regions for now. So we have to switch to one of those operative Regions.

In AWS Console, top blue bar, from Region drop down, select US East (Ohio) us-east-2.


Step 2: Go to Cloud9 Service. Click on Create Environment.

Provide the following configuration:

  • Environment Name: MyCloud9

  • Description: Cloud9 IDE for CloudPlusPlus Tutorial

Go to Next Step. Confirm the following list of default selected choices:

  • Environment type: Create a new EC2 instance for environment (direct access)

  • Instance type: t2.micro (1 GiB RAM + 1 vCPU)

  • Platform: Amazon Linux 2 (recommended)

Proceed to Next Step. Review and click on Create environment.


Step 3: You will have the Cloud9 IDE ready in some time. A window as below is visible.

In the bottom part of the screen, a Terminal Window is visible. If not go to the Window option in Menu Bar of Cloud9 IDE and click on New Terminal.


Step 4: Here we will install SDK.

Type the following command to check if Python is installed.

python --version

It will give the python installed version.

Type the following command to check if pip is installed.

python -m pip --version

It will give the pip installed version.

pip is a Python tool for installing packages.

Type the following command to install AWS SDK for Python (Boto3).

sudo python3 -m pip install boto3

It will give the python installed version.

Type the following command to check if Boto3 is installed.

python3 -m pip show boto3

It will give the Boto3 installed version.

SDKs simplify AWS services in your applications with an API tailored to your programming language or platform. AWS SDK for Python is also known as Boto.


Step 5: To test the working of following code, we create a bucket manually through S3 service of AWS Console.

Go to S3. Create a bucket.

Bucket name: my-cloudpp-bucket1

AWS Region: Ohio (Make sure it’s the same as Cloud9 IDE Region)

After the bucket is created, it will be visible in S3 Console.


Step 6: Go back to your Cloud9 Environment.

Go to File -> New File in the Menu bar at the top of screen.

Type in the following code:

import sys

import boto3

from botocore.exceptions import ClientError


def get_s3(region=None):

return boto3.resource('s3', region_name=region) if region else boto3.resource('s3')


def list_my_buckets(s3):

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


def create_and_delete_my_bucket(bucket_name, region, keep_bucket):

s3 = get_s3(region)


list_my_buckets(s3)


try:

print('\nCreating new bucket in S3:', bucket_name)

bucket = s3.create_bucket(

Bucket=bucket_name,

CreateBucketConfiguration={

'LocationConstraint': region

}

)

except ClientError as e:

print(e)

sys.exit('Bucket creation failed.')


bucket.wait_until_exists()

list_my_buckets(s3)


if not keep_bucket:

print('\nDeleting bucket in S3:', bucket.name)

bucket.delete()


bucket.wait_until_not_exists()

list_my_buckets(s3)

else:

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


def main():

import argparse


parser = argparse.ArgumentParser()

parser.add_argument('bucket_name', help='Name of Bucket')

parser.add_argument('region', help='Region of Bucket.')

parser.add_argument('--keep_bucket', help='Keeps the Bucket.',

action='store_true')


args = parser.parse_args()


create_and_delete_my_bucket(args.bucket_name, args.region, args.keep_bucket)


if __name__ == '__main__':

main()

Save this file (Ctrl+S) b name of cloudpps3.py.

Step 7: In Menu Bar click on

Run -> Run Configuration -> New Run Configuration.

In the [New] – Stopped window Run the following command. You can also press Enter to run the command.:

cloudpps3.py my-cloudpp-bucket2 us-east-2

Where my-cloudpp-bucket2 is the name of bucket to be created.

The output will be visible in the window.

We observe in the output that the manually created bucket is not affected as it is not mentioned in the code. The name of newly created bucket is the only argument that is passed.

Thus we have successfully created bucket in S3 using python SDK from Cloud9.


Step 8: If you no longer need the IDE, you may delete it.

In the top side of screen, click on the Cloud9 logo.

Further Click on Go To Your Dashboard.

In the Cloud9 Dashboard, select your IDE and click on Delete.

Type “Delete” in the next screen to confirm deletion.


Note: Delete the manually created S3 bucket from S3 service console. Also, switch back to your usual operating Region.



Was this document helpful? How can we make this document better. Please provide your insights. You can download PDF version for reference.


Cloud9 code for S3 with Python SDK
.pdf
Download PDF • 474KB

396 views23 comments

23 Comments


easy to follow steps,

well defined

Like

Easy to understand

very helpful

Kindly indent the code before running.

Like

Very helpful

Like

Atchaya B
Atchaya B
Feb 12

Useful sir.

Like

very useful

Like
bottom of page