Skip to content
blog.saurav.io
Go back

ChatGPT + Serverless Framework: Streamlining Cloud App Development

ChatGPT + Serverless Framework: Streamlining Cloud App Development

August 11, 2023

My go-to method for initiating a new personal project is an innovative combination of the Serverless Framework[1] and ChatGPT. This powerful blend enables me to describe my project requirements using natural language and quickly receive a stack of resources necessary for my web application.

For example, all I had to say was:

“Give me a Serverless Framework YAML file to create an API endpoint to get/put an item in a DynamoDB using a Python-based Lambda.”

This wasn’t just a command; it was a powerful instruction that resulted in the following output:

service: my-dynamodb-service

provider:
  name: aws
  runtime: python3.8
  region: us-east-1
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:GetItem
        - dynamodb:PutItem
      Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/MyTable"

functions:
  getItem:
    handler: handler.getItem
    events:
      - http:
          path: item/{id}
          method: get
          cors: true

  putItem:
    handler: handler.putItem
    events:
      - http:
          path: item
          method: put
          cors: true

resources:
  Resources:
    MyTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: MyTable
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5

Thrilled with the result, I asked again:

“Give me the Python handler file and instructions to deploy this.”

The response was equally prompt and effective:

Create Python Handlers

Create two Python functions in a file named handler.py. These functions will correspond to the getItem and putItem Lambda functions defined in the YAML file.

import boto3
import json

# DynamoDB client
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('MyTable')

def getItem(event, context):
    item_id = event['pathParameters']['id']
    response = table.get_item(Key={'id': item_id})
    return {
        'statusCode': 200,
        'body': json.dumps(response['Item'])
    }

def putItem(event, context):
    item = json.loads(event['body'])
    table.put_item(Item=item)
    return {
        'statusCode': 200,
        'body': json.dumps({'message': 'Item inserted successfully'})
    }

Directory Structure

Ensure that your directory structure looks like this:

project-root/
|-- handler.py
|-- serverless.yml (your YAML configuration)

Deploying the Service

Navigate to your project root directory and run the following command:

serverless deploy

This command packages and deploys your service, including the Lambda functions and the DynamoDB table.

The collaboration between the Serverless Framework and ChatGPT simplifies the development process, converting ideas into ready-to-deploy code with ease. It’s a practical change in how I tackle personal projects, aligning innovative thinking with contemporary cloud tools.

[1]: Serverless Framework: https://www.serverless.com/


Share this post on:


Previous Post
AI Agents: The Trillion-Agent Future Is Coming
Next Post
The AI Tools I'm Actually Using in 2023