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:
REST APIs
ETL pipelines
Scheduled events
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:
Install Chalice using pip:-
pip install chalice
Create a Chalice project:-
chalice new-project hello-world
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:-
Create a new policy.
Go to AWS Console → IAM → Policies.
Choose the
JSON
tab and enter the following JSON in thePolicy 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": "*" } ] }
Click Next and enter the policy name -
ChaliceDeploymentPolicy
Click on
Create policy
and your policy will be created. You can search your policy by nameChaliceDeploymentPolicy
to confirm.
Create a new user:
Go to AWS Console → IAM → Users.
Click on "Create user" button and enter the user name -
chalice-deployer
. Click Next.Choose
Attach policies directly
. Search for the policy -ChaliceDeploymentPolicy
and attach it. Click Next.Click on Next and in the review page, click on Create User button.
Configure AWS CLI
Getting Access Key & Secret Key:-
Go to AWS Console → IAM → Users →
chalice-deployer
home page.Go to Security credentials tab and under Access keys section, click on Create access key button.
Choose
Command Line Interface (CLI)
option and click Next.Click on Create access key button. Copy both the access key ID and secret key.
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.
Configuring AWS CLI:-
Run the command:
aws configure --profile chalice-deployer
You will be prompted to enter the access key ID and secret key. Enter the values that you copied earlier.
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.