OpenAI ChatGPT for AWS

Using OpenAI ChatGPT to generate AWS Lambda code which starts a Step Function - state machine

Introduction to OpenAI ChatGPT


ChatGPT-3 (Generative Pre-trained Transformer 3) is a state-of-the-art autoregressive language model developed by OpenAI. It is capable of generating human-like text and completing tasks such as translation, summarization, question answering and in our case generate AWS services.

One way to interact with GPT-3 is through ChatGPT, a chatbot that uses GPT-3 to understand and respond to user input. ChatGPT is trained on a large dataset of conversational text and can carry on a natural and coherent conversation with a user.

In this blog post, we will explore how to use ChatGPT and GPT-3 to generate a Lambda code to start a Step Functions state machine using the AWS SDK for Python (Boto3). This can be useful in a variety of scenarios, such as triggering a state machine as part of a serverless workflow or as a response to an event.

This entire blog text and code was also generated by ChatGPT, i have did very few code enhancements to meet the json data and existing arn's...
Even HTML tags for this blog with content was generated by ChatGPT

Let ChatGPT start ......

Prerequisites

Before we get started, make sure that you have the following prerequisites:

  • The AWS SDK for Python (Boto3) installed on your computer. You can install Boto3 using pip install boto3.
  • Alternatively you can use AWS console
  • A Step Functions state machine created in your AWS account.

Define a function to start the Step Functions state machine

First, let's define a function that starts the Step Functions state machine. This function will use the start_execution operation of the Step Functions service to start the state machine. Here's the code for the function:

  
  def start_step_function(event, context):
      step_functions = boto3.client('stepfunctions')

      # Replace STATE_MACHINE_ARN with the Amazon Resource Name (ARN) of your state machine
      response = step_functions.start_execution(
          stateMachineArn='STATE_MACHINE_ARN',
          input='{ "input": "your input here" }'
      )
      execution_arn = response['executionArn']
      print(f'Started execution with ARN: {execution_arn}')
  
  

In this function, we use the start_execution operation of the Step Functions service to start the state machine. We pass the Amazon Resource Name (ARN) of the state machine as the stateMachineArn parameter, and any input that we want to pass to the state machine as a JSON string in the input parameter. Make sure to replace STATE_MACHINE_ARN with the actual ARN of your state machine.

Define a Lambda handler function

Next, let's define a Lambda handler function that calls the function from the previous step. This function will be the entry point for our Lambda function and will be called by Lambda when the function is triggered. Here's the code for the handler function:

    
    def lambda_handler(event, context):
        start_step_function(event, context)
    
    

In this function, we simply call the start_step_function function that we defined in the previous step. This function will be the entry point for our Lambda function and will be called by Lambda when the function is triggered.

Create the Lambda function

Now that we have defined our functions, let's create the Lambda function. There are several ways to do this:

  • Using the AWS Management Console:
    1. Go to the Lambda console.
    2. Click the "Create function" button.
    3. Select "Author from scratch" and enter a name and runtime for your function.
    4. Under "Code entry type", select "Upload a .zip file" and upload the ZIP file containing your code.
    5. Under "Handler", enter the name of your Lambda handler function (e.g. lambda_handler).
    6. Click the "Create function" button to create the function.
  • Using the AWS CLI:
    1. Package your code and dependencies into a ZIP file:
                
                zip function.zip lambda_function.py
                
                
    2. Create the Lambda function using the create-function command:
                
                aws lambda create-function \
                  --function-name MyFunction \
                  --zip-file fileb://function.zip \
                  --handler lambda_handler \
                  --runtime python3.8
                
                
  • Using the AWS SDKs:

    You can use the AWS SDKs for various programming languages to create the Lambda function programmatically. For example, here is some sample code using the AWS SDK for Python (Boto3):

            
            import boto3
            client = boto3.client('lambda')
          
            response = client.create_function(
                FunctionName='MyFunction',
                Runtime='python3.8',
                Code={
                    'ZipFile': open('function.zip', 'rb').read()
                },
                Handler='lambda_handler'    
            )
            
            

Trigger the Lambda function

Now that we have created the Lambda function, let's trigger it to start the Step Functions state machine. There are several ways to do this:

  • Using an Amazon CloudWatch Events rule
  • Using an AWS Console
  • other Event based

OpenAI ChatGPT AWS Lambda code generation

Below are the series of messages from ChatGPT to generate Lambda Code ....

openai-chatgpt-aws-lambda-code-generate

and more ....


openai-chatgpt-aws-lambda-code-generate

however it did not use the json dataset i have provided, but thats ok i can also enter in test event


openai-chatgpt-aws-lambda-code-generate

and more ....


openai-chatgpt-aws-lambda-code-generate

I just copy pasted the same by creating a new Lambda service ...


openai-chatgpt-aws-lambda-code-generate

Enter test data


openai-chatgpt-aws-lambda-code-generate

Execution result from AWS Lambda...


openai-chatgpt-aws-lambda-code-generate

Execution result from AWS Step Funtion...


openai-chatgpt-aws-lambda-code-generate

Conclusion

ChatGPT worked like charm and after generating the above code, I intentionally made code errors and asked it to debug it.
The result is amazing ... it did it and responded with the correct code.
I have never seen a Bot like this before, it responds with full history and remembers all our previous chat. This looks like a serious disruptor in the AI world and obviously in software development.
I can see it as a tough competitor to google and Stackoverflow as this could evolve in the coming years.
Go on explore it yourself, its from different world ... ChatGPT OpenAI.