Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How do I run existing Go applications on AWS Lambda — Part 2

Sign upSign InSign upSign InAbhishek GuptaFollowITNEXT--ListenShareThe first part of this series introduced you to the AWS Lambda Go API Proxy, and how its framework/package specific adapter implementations (for gorilla/mux, echo and net/http) allows you to run existing Go applications as AWS Lambda functions fronted by Amazon API Gateway. If you haven't already, I encourage you to take a look at it in order to get a basic understanding of the AWS Lambda Go API Proxy.abhishek1987.medium.comThe AWS Lambda Go API Proxy also supports Gin, is one of the most popular Go web frameworks! This follow-up blog post will demonstrate how to take an existing URL shortener service written using the Gin framework, and run it as a serverless AWS Lambda function. Instead of using AWS SAM, we will change things up a bit and use the AWS CDK to deploy the solution.The code is available on GitHubThe aws-lambda-go-api-proxy package makes it possible to run Go APIs written using Gin, thanks to the framework specific adapter implementations.To understand this, let’s look into the main.go file of the Lambda function:The sample application presented in this blog is a trimmed down version of bit.ly or other solutions you may have used. It uses Amazon DynamoDB for persistence and exposes HTTP endpoints to access the application (basic CRUD operations).Although we will not be discussing the application code in detail, it’s important to understand the basic structure. Here is the package structure for the URL shortener application (in the function directory):It’s time to deploy the URL shortener application and give it a go!Before you proceed, make sure you have the Go programming language (v1.18 or higher) and AWS CDK installed.Clone the GitHub repository and change to the right directory:AWS CDK is a framework that lets you define your cloud infrastructure as code in one of its supported programming and provision it through AWS CloudFormation. In this case, we will be using the Go bindings for AWS CDKYou can refer to the code in the cdk directory.To start the deployment, invoke the cdk deploy command. You will see a list of resources that will be created and will need to provide your confirmation to proceed.Enter y to start creating the AWS resources required for the application. This will start creating the AWS resources required for our application.If you want to see the AWS CloudFormation template which will be used behind the scenes, run cdk synth and check the cdk.out folderYou can keep track of the progress in the terminal or navigate to AWS console: CloudFormation > Stacks > LambdaGolangProxyAPIDemoStackOnce all the resources are created, you can try out the application. You should have:You will need the API Gateway endpoint to invoke with the application — it’s available as part of the stack output (in the terminal or the Outputs tab in the AWS CloudFormation console for your stack). It should look something like this - https://foobarbazl.execute-api.us-east-1.amazonaws.com/prod/To generate a short code, you need to pass the original URL in the payload body as part of a HTTP POST request (for e.g. https://abhirockzz.github.io/)If all goes well, you should get a HTTP 201 along with the short code in the HTTP response (as a JSON payload).Check the DynamoDB table to verify records with the short code with the corresponding URL.The access link will have the following format — /app/ for e.g. https://8zbqx074rl.execute-api.us-east-1.amazonaws.com/prod/app/4b824fadOnce you navigate to the link using a browser, you will be automatically redirected to the original URL that you had specified. To better understand what’s going on behind the scenes, try accessing the same URL with curlYou should get a HTTP 302 response (Found) and the URL redirection happens due to the Location HTTP header which has the original URL.You can disable (and enable) the short codes. The original URL will only be accessible if the association is in active state.To disable a short code:This is a HTTP PUT request with a JSON payload that specifies the status (false in this case refers to disable action) along with the short code which is a path parameter to the API Gateway endpoint. If all works well, you should see an HTTP 204 (No Content) response.Check the corresponding record in DynamoDB — the active attribute should have switched to false.Just like in the case of update, you should get a HTTP 204 response. But this time, the respective DynamoDB record will be deleted.We covered the basic operations for the URL shortener application. As an exercise, try out the following scenarios and check the response(s):Once you’re done, to delete all the services, simply use:In this blog post you took a URL shortener application built using the Gin framework and deployed it as a Lambda function. Although this was a simple example, the same approach can be used to deploy more complex applications. As long as you have reasonable separation of concerns and your application is modular, most of the work will involve refactoring parts of the logic (main.go in this case) to wire up the Gin router (gin.Engine) to a Lambda function handler (entry point) by using the adapter implementation for Gin.Happy Building!----ITNEXTPrincipal Developer Advocate at AWS | I ❤️ Databases, Go, KubernetesAbhishek Gupta--2Carlos ArguellesinITNEXT--30Mohammad Hoseini RadinITNEXT--5Abhishek GuptainBetter Programming--3Midnight FiresaleinStackademic--1Alvaro Martinez Muñoz ✅--Aaron Dinin, PhDinEntrepreneur's Handbook--21Allen HeltoninBetter Programming--3Andy Walker--7TonyinGeek Culture--1HelpStatusWritersBlogCareersPrivacyTermsAboutText to speechTeams



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

How do I run existing Go applications on AWS Lambda — Part 2

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×