Deploy an AWS Lambda function using Amazon Q
Tags: go, programming, ai, productivity
Published: 2024-05-07 09:01:10

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

[Amazon Q](https://aws.amazon.com/developer/generative-ai/amazon-q/?trk=01b23196-71a4-403b-909c-65ddc571caa0&sc_channel=el) 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](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/q-in-IDE-setup.html). If you haven't done it already, I highly recommend following this [excellent guide by my colleague](https://community.aws/content/2fVw1hN4VeTF3qtVSZHfQiQUS16/getting-started-with-amazon-q-developer-in-visual-studio-code).

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgefzodj6274zmybwao4.png)

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

```go
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. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93ssnjc4vemkz24mqh4h.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/godx1r3gps8uxp2ch0ze.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mc65dtpj8janeoqb0mp.png)

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:

```shell
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:

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yd00vthnamxbsqx9pkx6.png)

I followed the instructions:

```shell
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:

```shell
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:

```shell
aws lambda invoke --function-name q-demo-GoFunction --payload '{"name": "Amazon Q"}' output.txt
```

It failed with this error:

```shell
Invalid base64: "{"name": "Amazon Q"}"
```

## Halp! 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovvl9niqven2lhm24ts9.png)

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



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iy7b5jdh34zx94jkpfbk.png)

It offered me multiple choices - I opted for option **2** (using the `--cli-binary-format` flag):

```shell
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!):

```shell
{"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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/58mg729r16t0ai3e0gyu.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lzahkifot84et1rhy2ob.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ul38n80lsh9akbysa6ri.png)

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:

```shell
aws lambda update-function-code --function-name q-demo-GoFunction --zip-file fileb://function.zip
```

Deployment successful! Time to invoke the Lambda function again:

```shell
aws lambda invoke --function-name q-demo-GoFunction --cli-binary-format raw-in-base64-out --payload '{"name": "Amazon Q"}' output.txt
```

It worked!!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oz8x5tbfisk58k2rt1ka.png)

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](https://aws.amazon.com/developer/generative-ai/amazon-q/?trk=01b23196-71a4-403b-909c-65ddc571caa0&sc_channel=el) to find more ways that Amazon Q Developer can help you. 

Happy building!