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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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:

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

Deploying the Service

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

1
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/