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 ......
Before we get started, make sure that you have the following prerequisites:
pip install boto3
.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.
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.
Now that we have defined our functions, let's create the Lambda function. There are several ways to do this:
lambda_handler
).
zip function.zip lambda_function.py
create-function
command:
aws lambda create-function \
--function-name MyFunction \
--zip-file fileb://function.zip \
--handler lambda_handler \
--runtime python3.8
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'
)
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:
Below are the series of messages from ChatGPT to generate Lambda Code ....
and more ....
however it did not use the json dataset i have provided, but thats ok i can also enter in test event
and more ....
I just copy pasted the same by creating a new Lambda service ...
Enter test data
Execution result from AWS Lambda...
Execution result from AWS Step Funtion...
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.