Getting Started with AWS Cloud9: A Beginner’s Guide with Python Code
In the fast-paced world of cloud computing, developers need tools that allow them to collaborate, develop, and deploy code in a seamless environment. One such tool from Amazon Web Services (AWS) is AWS Cloud9. It’s a cloud-based integrated development environment (IDE) that allows you to write, run, and debug your code from any location using just a browser.
In this blog, we’ll explore what AWS Cloud9 is, why it’s useful, and walk through an example of setting up a Python project with AWS Cloud9.
What is AWS Cloud9?
AWS Cloud9 is a browser-based IDE that supports multiple programming languages including Python, JavaScript, Ruby, and many more. It offers a seamless integration with AWS services, making it an excellent choice for cloud-native development.
Key Features of AWS Cloud9:
- Real-time collaboration: Multiple developers can edit and run code simultaneously.
- Preconfigured environment: AWS Cloud9 comes pre-installed with many popular software development packages.
- Terminal Access: You get direct access to an AWS-managed EC2 instance, which serves as the environment to develop, test, and run your applications.
- Seamless AWS integration: Since it’s an AWS service, you can easily connect to your other AWS resources like S3, Lambda, or DynamoDB.
Why Use AWS Cloud9?
- Remote Development: You don’t need to install development tools on your local machine. All the code and the environment are hosted on AWS, making it possible to work from any machine with an internet connection.
- Cost-Effective: You only pay for the EC2 instance running behind the IDE, and you can stop it whenever you’re not working.
- Collaboration: It allows real-time collaboration, making it easy to work on team projects.
Setting Up AWS Cloud9
Let’s walk through how you can set up AWS Cloud9 and use it to develop a simple Python project.
Step 1: Create an AWS Cloud9 Environment
- Log in to your AWS Console: Go to the AWS Management Console and search for “Cloud9”.
2. Create a new environment:
- In the AWS Cloud9 dashboard, click on “Create Environment”.
- Give your environment a name and description.
3. Select the instance type:
- Choose the EC2 instance type. If you are experimenting, the free tier t2.micro instance is sufficient.
4. Configure the environment settings:
- Specify the environment settings such as network, VPC, and IAM roles. You can keep the default options for simplicity.
5. Create the environment:
- AWS will launch a new EC2 instance and attach the Cloud9 IDE to it. This may take a couple of minutes.
Step 2: Setting Up Your Python Environment
Once your environment is up and running, you will be presented with a development interface that includes a code editor, a terminal, and an environment pane.
Check Python Installation:
AWS Cloud9 comes pre-installed with Python. You can check the version of Python by typing the following command in the terminal:
python3 --version
You should see something like:
Python 3.x.x
Install Dependencies:
For this example, let’s install the requests
library, a popular library for making HTTP requests in Python.
pip3 install requests
Step 3: Write a Simple Python Program
Now that the environment is set up, let’s create a simple Python script that fetches data from a public API.
- Create a new Python file:
- In the left-hand file explorer, right-click on the workspace directory and select “New File”.
- Name the file
fetch_data.py
.
2. Write the Python Code:
import requests
def fetch_data():
url = 'https://api.coindesk.com/v1/bpi/currentprice/BTC.json'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(f"Bitcoin Price (USD): {data['bpi']['USD']['rate']}")
else:
print("Failed to fetch data.")
if __name__ == "__main__":
fetch_data()
This script fetches the current Bitcoin price in USD from the Coindesk API and prints it to the terminal.
Step 4: Running Your Code
To run the Python script, follow these steps:
- Open the terminal within the Cloud9 IDE.
- Run the following command:
python3 fetch_data.py
You should see output similar to this:
Bitcoin Price (USD): 42,000.00
Step 5: Collaboration and Debugging
Real-Time Collaboration
One of the cool features of AWS Cloud9 is real-time collaboration. You can share your environment with other developers by:
- Clicking the “Share” button on the top-right corner.
- Adding team members and specifying their permissions.
Multiple developers can now collaborate on the same project in real-time.
Debugging in AWS Cloud9
AWS Cloud9 also provides a built-in debugger, which can be very handy for tracking down issues in your code. To set a breakpoint and debug your code:
- Click in the margin next to the line of code where you want to add a breakpoint.
- Hit the “Run” button, and the debugger will stop at the breakpoint, allowing you to inspect variables and step through the code.
Conclusion
AWS Cloud9 is a powerful tool for cloud-based development, providing a robust environment for writing, debugging, and running code. Its seamless integration with AWS services, coupled with its real-time collaboration features, makes it an ideal choice for teams working on cloud-native applications.
For more information → https://www.youtube.com/watch?v=T8p6EB6Zqus&ab_channel=choobtorials
So, go ahead, set up your AWS Cloud9 environment, and start building!