DEV Community

Cover image for Deploy an AWS Lambda function using Amazon Q
Abhishek Gupta for AWS

Posted on • Originally published at community.aws

Deploy an AWS Lambda function using Amazon Q

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

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 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!

Image description

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)
}
Enter fullscreen mode Exit fullscreen mode

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.

Image description

But I played the newbie role here, so I asked for additional clarification. Amazon Q responded accurately:

Image description

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?):

Image description

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"
Enter fullscreen mode Exit fullscreen mode

... 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
Enter fullscreen mode Exit fullscreen mode

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):

Image description

I followed the instructions:

aws iam get-role --role-name q-demo-lambda-ex --query Role.Arn --output text
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

It failed with this error:

Invalid base64: "{"name": "Amazon Q"}"
Enter fullscreen mode Exit fullscreen mode

Halp!

Image description

So I (obviously!) asked Amazon Q to debug it:

Image description

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
Enter fullscreen mode Exit fullscreen mode

Now I had a different error (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]"}
Enter fullscreen mode Exit fullscreen mode

Back to Amazon Q - it responded with debugging steps.

Image description

Hmmm, I see. I did everything. I even confirmed my executable/handler file name.

Image description

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:

Image description

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:

aws lambda update-function-code --function-name q-demo-GoFunction --zip-file fileb://function.zip
Enter fullscreen mode Exit fullscreen mode

Deployment 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
Enter fullscreen mode Exit fullscreen mode

It worked!!

Image description

Enough said ^

To summarise..

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 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!

Top comments (0)