Serverless Go APIs with AWS Lambda, DynamoDB and API Gateway
Published: 2022-02-21 18:29:26

I recently created an article in my personal blog about [how to create serverless Go APIs with AWS Lambda, DynamoDB and API Gateway](https://blog.danielpinhomonteiro.com/creating-serverless-go-apis-with-aws-lambda-dynamodb-and-api-gateway).
You'll add a summary here:
You can check the [repo that contains the source code of the Go project](https://github.com/danielmonteiro/go-serverless).

After cloning the repo we need to build and generate a zip of our project in order to use it in AWS Lambda. Follow below steps:

- From the cmd folder run go build main.go and then move the builded file inside the build folder
- From the root directory of the project run zip -jrm build/main.zip build/main
- The builded build/main.zip file will be uploaded to AWS 

## AWS Lambda
Now lets create our Lambda function that will run our Go project. Go to the AWS console and select Lambda.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prm617jusufivsdxg7oa.png)
Click on "Create function" button.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hywybixdrchb6qzc3c8.png)
Add function name and select the runtime (for this project it is Go 1.x).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrh3tbx87o2mqb5cxs8n.png)
Under the "Change default execution role", fill the fields accordingly. It will grant permissions to our Lambda to call DynamoDB.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1b36324wpwq8wd1e628d.png)
Upload the zip file that we generated in Go project previously.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft0iz0xz029xjgp2apdp.png)
Click in the "Edit" button under "Runtime settings", fill the  name of our handler, in our case it is **main**.

OK, Our Lambda is ready! Let's move to configure other services.

## DynamoDB
Now let's create the DynamoDB table that will store data of our users.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/to7htg7hhm854q625mj6.png)
Select DynamoDB in AWS Console and click "Create table" button.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ay57xqwe16iuns666h1.png)
Fill the table name and the primary key field.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/goo0hy90jsg1gigbbwsc.png)
After few seconds we'll see that table was created and status is Active.

## API Gateway

Time to create the API Gateway that will be responsible to receive requests and forward to our Go project.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ayjwal8jb838o060enme.png)
Go to AWS Console and select API Gateway. Then click in the "Build" button under "REST API" panel.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swqhbjbbzf46yx8rnu3h.png)
Fill the API name.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ws8h0f3b7zk8vdmahv4j.png)
Click in the "Actions" menu and select "Create method".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4f0dp6st4h1wo2iyyle.png)
For this project we can select ANY and select the Lambda function that we created previously.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t7s9vklu0nriznbjpsaj.png)
This is how it will look like. But our project is not exposed yet, we need to deploy the API.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tetoydem11y8fmv6pepq.png)
Click in the "Actions" menu and then "Deploy API".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pxmu0jzfwm9qumjja7x.png)
Choose a name for the deployment stage.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zmi750342kpcm2m82i5.png)
We should see the "Invoke URL" that will call our Lambda function.

## Testing the APIs
Let's runcall the API's to make sure it is working. I'll use Postman for that, but you can use curl, Insomnia or any other tool.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8p1wh3gvb8p0y7gu26y.png)
First I called the POST API to create a user, and we get back the user in the response.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c15b526lo5z0rojmn2e8.png)
We can go to DynamoDB console and verify that the record was created in the table we created previously.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zp9bomibylv4wqqu5eva.png)
We can call the GET API to load the user and as expected we get his data in the response.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkgo727qm8yeuyftsm6h.png)
Let's try the PUT API to update user data.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8sgjhzizc1lrq66h026h.png)
After calling the GET API we can see that user data was updated.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7a8izdxahxfhnc8eb4gp.png)
Let's test the last API, the DELETE to remove the user from database. The response is a 200 OK meaning that user was successfully deleted.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbvr94wdk0irsax10vb7.png)
We can double check that calling the GET API.