DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • Building Powerful AI Applications With Amazon Bedrock: Enhanced Chatbots and Image Generation Use Cases
  • Enterprise AI Platform With Amazon Bedrock
  • Mastering Distributed Caching on AWS: Strategies, Services, and Best Practices
  • Data Migration With AWS DMS and Terraform IaC

Trending

  • AWS CDK: Infrastructure as Abstract Data Types
  • Implementing Real-Time Credit Card Fraud Detection With Apache Flink on AWS
  • Node.js Walkthrough: Build a Simple Event-Driven Application With Kafka
  • You Can Shape Trend Reports: Participate in DZone Research Surveys + Enter the Prize Drawings!
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Deploy an AWS Lambda Function Using Amazon Q

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

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
May. 07, 24 · Tutorial
Like (1)
Save
Tweet
Share
911 Views

Join the DZone community and get the full member experience.

Join For Free

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!

A set of steps along with code and CLI commands

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.

YOUR_LAMBDA_ROLE_ARN

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

Amazon Q response

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

CLI command

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

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!

Halp!

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

debug

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.

debugging response

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

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:

nudge

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

celebrate!

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!

AWS AWS Lambda Go (programming language) AI

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building Powerful AI Applications With Amazon Bedrock: Enhanced Chatbots and Image Generation Use Cases
  • Enterprise AI Platform With Amazon Bedrock
  • Mastering Distributed Caching on AWS: Strategies, Services, and Best Practices
  • Data Migration With AWS DMS and Terraform IaC

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: