AWS API Gateway Websocket & Lambda Terraform

In this blog post, we will explore the seamless integration of AWS API Gateway Websockets with Lambda functions using Terraform. Dive into the world of serverless communication as we guide you through the setup, configuration, and deployment process, unlocking the power of real-time, bidirectional communication for your applications. Whether you’re a seasoned developer or just starting with AWS infrastructure as code, this post will provide valuable insights and a step-by-step walkthrough to harness the potential of AWS API Gateway Websockets and Lambda functions with the efficiency and control offered by Terraform.

Source Code: https://github.com/NadunOvitigala/aws-apiwebsocket_and_lambda_with_terraform

Clone the terraform code and run it

terraform init
terraform plan
terraform apply

Go to API gateway and select websocket-api

Then find the connection URL on stage section

Copy and please update on Sendmessage , Broadcast Lambda functions python codes and deploy it

Note : please remove /@connections parth from the URL

After the these changes go to the apigateway and find the websocket URL on stage section

Go https://www.piesocket.com/websocket-tester to test our websocket api

Paste the copied websocket URL and connect

Lets send a test message

{ "action": "sendmessages", "message":"hello" }

You should see the “waiting for response message….

Then go to the AWS Cloudwatch and select the SendMessage log group

select the latest log stream and find the connectionID

in my case it is “POSPDfYqBcwCEgQ=

Go to the Broadcast Lambda function and create a test event like bellow json

Note : please update your connectionID

{
  "connectionId": "POSPDfYqBcwCEgQ=",
  "message": "testing"
}

Save & Test

Go the https://www.piesocket.com/websocket-tester

Now you can see the “testing” message that we send

to destroy the environment please type bellow command in your terminal

terraform destroy

Thank You!!

Leave a Comment

Your email address will not be published. Required fields are marked *