Deploy an AWS Lambda Function Using Amazon Q
Amazon Q is a generative AI-powered assistant. No browser tabs were used to get this done (except when searching for funny pics for this blog).
Join the DZone community and get the full member experience.
Join For FreeAmazon Q is a generative AI-powered assistant that you can use in the AWS Management Console, command line, and even your IDE. As a Go developer, I was quite curious to try it out for doing something in a conversational and interactive way (not just inline code suggestions). I decided to start off with something I work with on a regular basis — AWS Lambda functions using Go.
So, I asked Amazon Q to go through the process of writing an deploying a function.
Folks familiar with Lambda, but new to Go might use this as a "getting started" approach to go from nothing to something, quickly.
Hello Amazon Q
I started by setting up Amazon Q in VS Code. If you haven't done it already, I highly recommend following this excellent guide by my colleague.
I initiated the conversation by asking:
"Help me build a AWS Lambda function in Go and deploy it using CLI."
I was promptly given a set of steps along with code and CLI commands. Great!
Being a regular Go developer, my dev environment is already setup. So, steps 1 to 3 were fairly quick. Although this is a simple example, I was happy that the code compiled without any issues (no changes required):
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
type MyEvent struct {
Name string `json:"name"`
}
func HandleRequest(ctx context.Context, event MyEvent) (string, error) {
return fmt.Sprintf("Hello %s!", event.Name), nil
}
func main() {
lambda.Start(HandleRequest)
}
I moved on to step 4 to create the Lambda function using AWS CLI. Now, I imagine someone familiar with Lambda (irrespective of the programming language), might know what YOUR_LAMBDA_ROLE_ARN
in step 4 refers to.
But I played the newbie role here, so I asked for additional clarification. Amazon Q responded accurately:
Being Lazy...
Instead of using the AWS console (as per instructions), I wanted to use AWS CLI to create the role. But, I don't always remember all the AWS CLI commands (do you?):
I followed the instructions to save the trust policy in a file. Then, I created the role using the CLI command that Amazon Q had provided:
aws iam create-role --role-name q-demo-lambda-ex --assume-role-policy-document file://trust-policy.json --description "Execution role for Lambda function"
... and attached the policy to the IAM role:
aws iam attach-role-policy --role-name q-demo-lambda-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
This was cut off in the screenshot, but Amazon Q provided the CLI command for this as well!
Now I went back to step 4 again. Wait, I still need the IAM role ARN
! No guesses here — I asked Amazon Q (again):
I followed the instructions:
aws iam get-role --role-name q-demo-lambda-ex --query Role.Arn --output text
Once I had the ARN
, I used the CLI command from step 4 to create the Lambda function:
aws lambda create-function --function-name q-demo-GoFunction --zip-file fileb://function.zip --handler main --runtime provided.al2 --role <inserted the ARN here>
It worked!!
I moved on step 5 now and invoked the Lambda as per Amazon Q instructions:
aws lambda invoke --function-name q-demo-GoFunction --payload '{"name": "Amazon Q"}' output.txt
It failed with this error:
Invalid base64: "{"name": "Amazon Q"}"
Halp!
So I (obviously!) asked Amazon Q to debug it:
It offered me multiple choices - I opted for option 2 (using the --cli-binary-format
flag):
aws lambda invoke --function-name q-demo-GoFunction --cli-binary-format raw-in-base64-out --payload '{"name": "Amazon Q"}' output.txt
Now I had a different error (a sign of progress!):
{"errorType":"Runtime.InvalidEntrypoint","errorMessage":"RequestId: ccc1ffd3-e041-4dc8-a49b-7011abd6abfb Error: Couldn't find valid bootstrap(s): [/var/task/bootstrap /opt/bootstrap]"}
Back to Amazon Q — it responded with debugging steps.
Hmmm, I see. I did everything. I even confirmed my executable/handler file name.
Now I felt Amazon Q wavering a bit as it started giving me suggestions like trying SAM.
Even the Best Need Help at Times...
So, I tried to nudge it:
And there we Go (yes, Go with an upper case! pun intended!). Amazon Q was able to figure it out.
Marching Ahead
I rebuilt the Lambda function (named the binary bootstrap
, as suggested by Amazon Q), zipped it up, and updated the function using the AWS CLI command:
aws lambda update-function-code --function-name q-demo-GoFunction --zip-file fileb://function.zip
Deployment was successful! Time to invoke the Lambda function again:
aws lambda invoke --function-name q-demo-GoFunction --cli-binary-format raw-in-base64-out --payload '{"name": "Amazon Q"}' output.txt
It worked!!
Enough said ^
To Summarize
In response to my initial question, Amazon Q started off with a comprehensive set of steps. It helped me debug some issues along the way and was receptive enough when I nudged it in the right direction. Nice! Now, this was a fairly simple task but I think it was still convenient to not have to leave my IDE to research, debug, etc. But, I'm not saying that will always be the case.
Also, note that this approach is not meant as a substitute for reading the documentation (or reference material). Make sure to understand the fundamentals of what you are doing, unless you want to get caught up in a "copy-paste-debug-repeat" cycle by solely depending on specific tools. By all means, seek help when you need it. Software development in the real world has constraints — one of the most important one being time. That's where Amazon Q can help a lot.
Check out the Developer Centre resources to find more ways that Amazon Q Developer can help you.
Happy building!
Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments