# Deploy Rest API via Cloud9 & Chalice

Welcome!

In this tutorial, we will configure settings in Cloud9 and then the AWS CLI to deploy a basic REST API via Chalice.

What is Chalice?

> AWS Chalice is **a framework for writing serverless applications in Python that provides familiar, declarative APIs to help you write your application**. From the first version of this framework, AWS has provided a deployment packager that handles the details of how to package your application for AWS Lambda.

AWS Chalice allows you to quickly create and deploy applications that use Amazon API Gateway and AWS Lambda.  Chalice provides:

1. A command-line tool for creating, deploying, and managing your app
    
2. A familiar and easy-to-use API for declaring views in Python code
    
3. Automatic IAM policy generation
    

Let's get started!

### Create Cloud9 Environment:

> AWS Cloud9 is a cloud-based integrated development environment (IDE) that lets you write, run, and debug your code with just a browser. It includes a code editor, debugger, and terminal.

* Navigate to the AWS Console and type Cloud9 in the services search bar:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437000380/eb86b773-72b5-4a53-9ebc-71fe971580aa.png align="center")

* Click Create Environment
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437044192/c898dd17-e105-4886-b28e-49098f94d94e.png align="center")

* In the Create environment screen:
    
* Click the "Name" field.
    
    * Type Name of Project
        
* Click "VPC settings" Click Custom VPC or Default VPC
    
* Select the appropriate subnet or default "No preference"
    
* Click Create
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437079051/37b3e0f6-769a-4c9d-9aab-397f69a304a2.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437088892/c94789cf-8575-421a-8805-48c2ad35639a.png align="center")

---

### Create IAM Role for Cloud9 EC2 Instance

> Ec2 Instance IAM roles allow applications to securely make API requests from your instances, without requiring you to manage the security credentials that the applications use. Instead of creating and distributing your AWS credentials, you can delegate permission to make API requests using IAM roles.  For this example, we will utilize an Admin IAM role instead of the default Cloud9 permissions for our Ec2 instance.

* After the Cloud9 environment is created, let's create an IAM role for the EC2 Instance:
    
* Head to the Console services search bar and type: IAM
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437113624/79ebbbc7-d0df-4fe3-baa0-a0791be92980.png align="center")

* Click Roles &gt; Create Role
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437143847/f6459678-3c63-42d5-9f50-1221b416b225.png align="center")

* For this example, we will be adding AdministratorAccess to our role.
    
* Select AdministratorAccess policy and Create a Role with Role Name: Cloud9Ec2InstanceAdmin:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437161918/eebfc36d-f5e5-481d-b867-c5eabd47a19d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437184950/768173d7-c5c1-42fe-b5a4-0c77ed32414f.png align="center")

* Navigate to the EC2 Dashboard via the Services search bar in the AWS Console:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437579574/a13a30da-c97d-4d09-9e75-e23f6622a91b.png align="center")

* Click instances(running)
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437558550/f1e502b7-c42b-41ed-b805-0f3d33a73ec9.png align="center")

* Select the cloud9 Ec2 Instance
    
    * Actions &gt; Security &gt; Modify IAM role:
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437226217/b8618a16-1a41-4405-943c-e74ebabfba36.png align="center")

* Attach the Cloud9Ec2InstanceAdmin role:
    
    * Select the Cloud9EC2InstanceAdmin
        
    * Click Update IAM Role
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437261535/c3a95568-b155-4a13-85bf-bb22ee5d1ac7.png align="center")

* Navigate back to Cloud 9 and Select the radio button for our newly created environment
    
* Click the radio button for the Cloud9 Environment we created:
    
* Click Open in Cloud9
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437542573/89d7673c-f1ff-4811-9ed2-756ae0f79424.png align="center")

* This will take you to the Cloud9 IDE screen:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437531414/392705ef-c76b-40c7-a640-3c32864e349f.png align="center")

---

### Configure Cloud9 Credential Management

> For an AWS Cloud9 EC2 development environment, AWS Cloud9 makes temporary AWS access credentials available to you in the environment. AWS calls these *AWS-managed temporary credentials*.  AWS Cloud9 puts additional restrictions on how its temporary credentials can be used to access AWS actions and resources from the environment (security best practice).  To avoid any issues, we will disable *managed temporary credentials* in favor of our Admin IAM Role.

* In the Cloud9 Screen:
    
    * Click the Settings icon in the top right:
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437522309/ad30fc39-de0b-4470-9b87-80bfc58ea769.png align="center")

* Click AWS Settings:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437508344/e1c7e17a-d4cc-440e-934b-325bc7aa97c7.png align="center")

* De-Select AWS managed temporary credentials:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437474618/16a7414c-f313-49f4-9765-155786f063f2.png align="center")

* Exit out of the settings window
    
    * Select the Window drop-down:
        
        * Click New Terminal
            

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437494492/c9566fb8-adbd-4a9f-b900-8e7483fec890.png align="center")

---

### Configure the CLI, Install Chalice & Deploy

> The `config` and `credentials` files are files that the AWS Command Line Interface (AWS CLI) uses to interact with AWS. These include your security credentials, the default output format, and the default AWS Region.  The AWS CLI stores this information in a *profile* (a collection of settings) named `default` in the `credentials` file. By default, the information in this profile is used when you run an AWS CLI command that doesn't explicitly specify a profile to use.  For this example, we will be utilizing instance metadata and environment variables to configure the options and credentials.

* Type the following commands in the terminal window of Cloud9 to configure the CLI and download Chalice:
    

* Install text utilities:
    

```bash
sudo yum -y install jq gettext
```

* Configure CLI w/ Instance Metadata
    

```bash
export AWS_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)

echo "export AWS_REGION=${AWS_REGION}" >> ~/.bash_profile

aws configure set default.region ${AWS_REGION}

aws configure get default.region
```

* Setup environment variables for account ID
    

```bash
export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)

echo "export AWS_ACCOUNT_ID=${AWS_ACCOUNT_ID}" >> ~/.bash_profile
```

* Install Chalice:
    

```bash
pip install chalice
```

* Create New-Project "Name"
    
    * In this example, we will use "rolldice" as the project name:
        

```bash
chalice create new-project rolldice
```

* CD into "rolldice" directory
    

```bash
cd rolldice
```

* Notice that Chalice has created a directory with [app.py](http://app.py) and requirements.txt
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437450562/700bb3b1-2423-4a7d-b4d2-77e150a74e25.png align="center")

* Deploy the endpoint
    

```bash
chalice deploy
```

* Copy API endpoint and paste into a new browser or use the Curl command to check our endpoint:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437284099/5812d04f-267f-4e00-be13-dd74fc49663f.png align="center")

```bash
curl https://endpoint/api
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437309254/2bd6c975-67a9-448b-acf9-a6522e662bc6.png align="center")

You should have received the hello world message!  Chalice makes it easy to deploy Rest API as it automates the Lambda Function, API and I AM Role creation.

---

### Cleanup Infrastructure

* To Delete the infrastructure associated with Chalice:
    
    * Type in the following command:
        

```bash
chalice delete
```

* You can now head back over to the Clou9 Dashboard in the AWS Console and delete the Environment
    

---

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691437339429/3341fd3e-7333-4f3f-8cda-784beb748bad.png align="center")

Great job! We successfully launched our REST API via Chalice.

We've configured our Cloud9 environment, the AWS CLI, and deployed our API.  You could utilize other IDE's such as Visual Studio Code..etc as long as you configure the CLI for programmatic access.  

Till next time!
