Deploying Serverless Apps with Chalice: A Beginner’s Guide

Deploying Serverless Apps with Chalice: A Beginner’s Guide

Introduction

Hello, World! This blog covers the basics of using AWS Chalice, with a primary focus on deploying it to AWS Lambda. First, let's know about Chalice.

What is Chalice?

Chalice is an open-source framework by AWS that simplifies building serverless applications on AWS Lambda using Python. It allows you to create REST APIs, schedule recurring jobs, and process events such as S3 object uploads or API Gateway requests.

Where can Chalice be used?

Chalice can be used to build the following, but is not limited to:

  1. REST APIs

  2. ETL pipelines

  3. Scheduled events

  4. IoT applications

Prerequisites

  • Python >= 3.8.

  • AWS account.

  • AWS CLI installed.

  • Basic understanding of Python.

Setting Up Chalice

We will install and set up a simple hello-world Chalice application. Follow the steps below:

  1. Install Chalice using pip:-

     pip install chalice
    
  2. Create a Chalice project:-

     chalice new-project hello-world
    
  3. Run the app locally:-

     chalice local
    

    Now, you can visit http://127.0.0.1:8000/ to see the output. Voila🎉! You've successfully deployed your first Chalice application to AWS Lambda. If you get stuck on any step, feel free to leave a comment on this post.

Setting Up AWS User for Chalice (Best Practice)

For best security practices, we will create a new AWS user and assign it the necessary permissions to deploy our Chalice application to AWS Lambda. Please follow the steps below:-

  1. Create a new policy.

    1. Go to AWS Console → IAM → Policies.

    2. Choose the JSON tab and enter the following JSON in the Policy editor field:-

       {
           "Version": "2012-10-17",
           "Statement": [
               {
                   "Effect": "Allow",
                   "Action": [
                       "iam:GetRole",
                       "iam:PassRole",
                       "iam:CreateRole",
                       "iam:AttachRolePolicy",
                       "iam:PutRolePolicy"
                   ],
                   "Resource": "arn:aws:iam::*:role/hello-world-dev"
               },
               {
                   "Effect": "Allow",
                   "Action": [
                       "lambda:*"
                   ],
                   "Resource": "*"
               },
               {
                   "Effect": "Allow",
                   "Action": [
                       "apigateway:POST",
                       "apigateway:PUT",
                       "apigateway:DELETE",
                       "apigateway:GET",
                       "apigateway:PATCH"
                   ],
                   "Resource": "*"
               },
               {
                   "Effect": "Allow",
                   "Action": [
                       "logs:CreateLogGroup",
                       "logs:CreateLogStream",
                       "logs:PutLogEvents"
                   ],
                   "Resource": "*"
               },
               {
                   "Effect": "Allow",
                   "Action": [
                       "cloudformation:CreateStack",
                       "cloudformation:UpdateStack",
                       "cloudformation:DeleteStack",
                       "cloudformation:DescribeStacks"
                   ],
                   "Resource": "*"
               },
               {
                   "Effect": "Allow",
                   "Action": [
                       "s3:CreateBucket",
                       "s3:PutObject",
                       "s3:GetObject",
                       "s3:DeleteObject",
                       "s3:ListBucket"
                   ],
                   "Resource": "*"
               }
           ]
       }
      
    3. Click Next and enter the policy name - ChaliceDeploymentPolicy

    4. Click on Create policy and your policy will be created. You can search your policy by name ChaliceDeploymentPolicy to confirm.

  2. Create a new user:

    1. Go to AWS ConsoleIAMUsers.

    2. Click on "Create user" button and enter the user name - chalice-deployer. Click Next.

    3. Choose Attach policies directly. Search for the policy - ChaliceDeploymentPolicy and attach it. Click Next.

    4. Click on Next and in the review page, click on Create User button.

Configure AWS CLI

  1. Getting Access Key & Secret Key:-

    1. Go to AWS Console → IAM → Users → chalice-deployer home page.

    2. Go to Security credentials tab and under Access keys section, click on Create access key button.

    3. Choose Command Line Interface (CLI) option and click Next.

    4. Click on Create access key button. Copy both the access key ID and secret key.

    5. Copy the access key ID and secret key and save them in a secure location. You will need them later when deploying the application to AWS Lambda.

  2. Configuring AWS CLI:-

    1. Run the command:

       aws configure --profile chalice-deployer
      
    2. You will be prompted to enter the access key ID and secret key. Enter the values that you copied earlier.

    3. Choose the region where you want to deploy the application. In this case, we will use us-east-1.

Deploy the Chalice app

We've made it to the final step! The following command will use the profile chalice-deployer to deploy the application:-

AWS_PROFILE=chalice-deployer chalice deploy

When you run this command, Chalice will package your application and create the Lambda function hello-world-dev. After a successful deployment, you will see the Lambda ARN and REST API URL in the output.

To verify the deployment, visit the AWS Management Console for your region (e.g., us-east-1) and navigate to the Lambda service. You should see that the function has been successfully deployed.

Conclusion

So, what have we learned? Chalice makes it easy to build serverless applications with just a few lines of code. Now that we've successfully deployed our first Chalice application to AWS Lambda, stay tuned for the next post, where we'll explore a more exciting use case of Chalice.