Use Amazon Cloud9 with the Amazon SDK for PHP
Amazon Cloud9 is a web-based integrated development environment (IDE) that contains a collection of tools that you use to code, build, run, test, debug, and release software in the cloud. You can use Amazon Cloud9 with the Amazon SDK for PHP to write and run your PHP code by using a browser. Amazon Cloud9 includes tools such as a code editor and terminal. Because the Amazon Cloud9 IDE is cloud based, you can work on your projects from your office, home, or anywhere by using an internet-connected machine. For general information about Amazon Cloud9, see the Amazon Cloud9 User Guide.
Follow these instructions to set up Amazon Cloud9 with the Amazon SDK for PHP:
Step 1: Set up your Amazon Web Services account to use Amazon Cloud9
To use Amazon Cloud9, sign in to the Amazon Cloud9 console from the Amazon Web Services Management Console.
Note
If you are using Amazon IAM Identity Center to authenticate, you might need to add the required
permission of iam:ListInstanceProfilesForRole
to the user-attached policy
in the IAM console.
To set up an IAM entity in your Amazon account to access Amazon Cloud9 and sign in to the Amazon Cloud9 console, see Team Setup for Amazon Cloud9 in the Amazon Cloud9 User Guide.
Step 2: Set up your Amazon Cloud9 development environment
After you sign in to the Amazon Cloud9 console, use the console to create an Amazon Cloud9 development environment. After you create the environment, Amazon Cloud9 opens the IDE for that environment.
For details, see Creating an Environment in Amazon Cloud9 in the Amazon Cloud9 User Guide.
Note
As you create your environment in the console for the first time, we recommend that you choose the option to Create a new instance for environment (EC2). This option tells Amazon Cloud9 to create an environment, launch an Amazon EC2 instance, and then connect the new instance to the new environment. This is the fastest way to begin using Amazon Cloud9.
If the terminal isn’t already open in the IDE, open it. On the menu bar in the IDE, choose Window, New Terminal. You can use the terminal window to install tools and build your applications.
Step 3: Set up the Amazon SDK for PHP
After Amazon Cloud9 opens the IDE for your development environment, use the terminal window to set up the Amazon SDK for PHP in your environment.
Composer is the recommended way to install the Amazon SDK for PHP. Composer is a tool for PHP that manages and installs the dependencies of your project.
For more information on how to install Composer, configure autoloading, and follow other
best practices for defining dependencies, see getcomposer.org
Install Composer
If Composer is not already in your project, download and install Composer on the
Download Composer page
-
For Windows, follow the Windows Installer instructions.
-
For Linux, follow the Command-line installation instructions.
Add Amazon SDK for PHP as a dependency via Composer
If Composer is already
installed globally
$
composer require aws/aws-sdk-php
Otherwise, type this Composer command to install the latest version of the Amazon SDK for PHP as a dependency.
$
php -d memory_limit=-1 composer.phar require aws/aws-sdk-php
Add autoloader to your php scripts
Installing Composer creates several folders and files in your environment. The primary
file you will use is autoload.php
, which is in the
vendor
folder in your environment.
To utilize the Amazon SDK for PHP in your scripts, include the autoloader in your scripts, as follows.
<?php require '/path/to/vendor/autoload.php'; ?>
Step 4: Download example code
Use the terminal window to download example code for the Amazon SDK for PHP into the Amazon Cloud9 development environment.
To download a copy of all the code examples used in the official Amazon SDK documentation into your environment’s root directory, run the following command:
$
git clone https://github.com/awsdocs/aws-doc-sdk-examples.git
The code examples for the Amazon SDK for PHP are located at
ENVIRONMENT_NAME/aws-doc-sdk-examples/php
directory, where
ENVIRONMENT_NAME
is the name of your development
environment.
To follow along using an Amazon S3 example, we recommend starting with code example
ENVIRONMENT_NAME/aws-doc-sdk-examples/php/example_code/s3/ListBuckets.php
.
This example will list your Amazon S3 buckets. Use the terminal window to navigate to
the s3
directory and list the files.
$
cd aws-doc-sdk-examples/php/example_code/s3
$
ls
To open the file in Amazon Cloud9, you can click on the ListBuckets.php
directly in the terminal window.
For more support in understanding code examples, see Amazon SDK for PHP Code Examples.
Step 5: Run example code
To run code in your Amazon Cloud9 development environment, choose the Run
button in the top menu bar. Amazon Cloud9 automatically detects the .php
file
extension and uses the PHP (built-in web server) runner to run
the code. However, for this example we actually want the PHP
(cli
) option. For more information about running code in
Amazon Cloud9, see Run Your
Code in the Amazon Cloud9 User Guide.
In the following screenshot, note these basic areas:
-
1: Run. The Run button is located on the top menu bar. This opens a new tab for your results.
Note
You can also manually create new run configurations. On the menu bar, choose Run, Run Configurations, New Run Configuration.
-
2: Command. Amazon Cloud9 populates the Command text box with the path and file name to the file you run. If your code expects any command line parameters to be passed in, these can be added to the command line in the same way that you would when running the code through a terminal window.
-
3: Runner. Amazon Cloud9 detects that your file extension is
.php
and selects the PHP (built-in web server) Runner to run your code. Select PHP (cli
) to run this example instead.
Any output generated from the running code displays in the tab.