logo
Menu
Deploy an AWS Lambda function using Amazon Q in your  IDE

Deploy an AWS Lambda function using Amazon Q in your IDE

No browser tabs were used to get this done (except when searching for funny pics)

Abhishek Gupta
Amazon Employee
Published May 6, 2024
Last Modified May 7, 2024
Amazon 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 and 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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:
1
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:
1
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:
1
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:
1
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:
1
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):
1
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 (sign of progress!)

1
{"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 a 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:
1
aws lambda update-function-code --function-name q-demo-GoFunction --zip-file fileb://function.zip
Deployment successful! Time to invoke the Lambda function again:
1
aws lambda invoke --function-name q-demo-GoFunction --cli-binary-format raw-in-base64-out --payload '{"name": "Amazon Q"}' output.txt
Wohoo, it worked!! (check the output.txt file)
Enough said ^

To summarise..

In response to my initial question, Amazon Q started off with a comprehensive set of steps. It answered a few queries, helped me debug 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, that may not always be the case. Note that this approach is not meant as a substitute for reading the documentation (or reference material). Make sure to know 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, and stay Q-rious !
 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

4 Comments