How to Deploy a WebSocket App to Amazon Lightsail | by Manato Kuroda | May, 2022

Deploying a WebSocket software to the cloud

Picture by Maxime VALCARCE on Unsplash.

Amazon Lightsail supplies light-weight digital servers which can be simple to make use of at a really cost-effective worth and routinely configure networking and safety environments.

The use instances might embrace:

  • A easy net functions
  • A check atmosphere
  • A private mission like a portfolio or weblog

Amazon Lightsail is appropriate for WebSocket functions as properly. Different cloud servers additionally assist WebSocket equivalent to Heroku and Google App Engine for a versatile atmosphere (not Customary). However Heroku for a free plan will not be sufficient by way of efficiency. From a price standpoint, Google App Engine may be costly for small initiatives. Amazon Lightsail affords plans beginning at $7 for containers, and the server specs are sufficient. I constructed an application utilizing Golang and GraphQL and deployed it in a micro container. This makes use of WebSocket for real-time communication, and the efficiency is nice sufficient.

This submit will information you thru the deployment of the Lightsail with AWS CLI.

Obtain the WebSocket app to deployment to AWS Lightsail.

$ git clone https://github.com/manakuro/golang-websockets-example

If you wish to understand how WebSocket works, check out the previous post.

Right here is the ultimate codebase on GitHub

  • Configure AWS CLI
  • Create Lightsail service
  • Create Docker picture
  • Push Container
  • Deploying

First, set up the AWS CLI.

$ brew set up awscli

You too can set up it by following the AWS instructions.

Create coverage

To entry Lightsail from awscli, create a coverage.

Go to the IAM console and the Insurance policies within the left navigation. Click on the Create Coverage button.

Within the create coverage, click on the JSON tab and previous it:


"Model": "2012-10-17",
"Assertion": [

"Effect": "Allow",
"Action": [
"lightsail:*"
],
"Useful resource": "*"

]

This coverage grants entry to all Lightsail actions and sources.

Add consumer

Subsequent, go to the Customers within the left navigation, and click on the Add Customers.

Customers

And fill within the consumer identify and test the Entry key like so:

Add consumer

Subsequent, connect the Lightsail coverage like this:

Add consumer

Lastly, create the consumer and make a remark of the Entry Key ID and Secret entry key. We are going to use this later.

Add consumer

Configure awscli

Arrange the consumer above to have the ability to entry your AWS account from the command line.

$ aws configure --profile deploy-lightsail
AWS Entry Key ID [None]: xxxxxAWS Secret Entry Key [None]: xxxxxDefault area identify [None]: ap-northeast-1Default output format [None]:

After the configuration, the credential file ought to appear to be this:

cat ~/.aws/credentials                                                                                                                                                                       [default]aws_access_key_id = xxxaws_secret_access_key = xxx[deploy-lightsail]aws_access_key_id = xxxaws_secret_access_key = xxx

Set up the Lightsail plugin

Subsequent, set up the Lightsail plugin to have the ability to push container pictures of Docker.

$ brew set up aws/faucet/lightsailctl

Now we’re prepared for accessing the AWS Lightsail from the command line.

On this submit, we are going to create a micro container service that prices $10 a month however contains free for 3 months.

Create a micro container from the command. The service identify is websocket-lightsail right here.

$ aws lightsail create-container-service --service-name project- websocket-lightsail --power micro --scale 1

We are able to now push a container picture to Lightsail. So subsequent, create a Dockerfile to deploy our software.

Create Dockerfile within the root listing.

Then, construct the picture named app:

$ docker construct . -t app

And the Docker pictures ought to appear to be this:

$ docker pictures -a 

REPOSITORY TAG IMAGE ID CREATED SIZE
app newest 87961c0073de 5 seconds in the past 14.5MB

We’ve created the docker picture app, push the picture to the container.

$ aws lightsail push-container-image --service-name websocket-lightsail --label app --image app

Since a container service can have a number of containers, we label it by including --label app and specify our picture to push by including --image app ultimately.

After the creation, the response exhibits one thing like this. The picture identify might be used for pulling containers within the Lightsail.

Discuss with this picture as ":websocket-lightsail.app.1" in deployments.

To deploy our software, inform the Lightsail which container needs to be working by a configuration.

To ship a configuration choice, we create a json file.

So first, set up the jq. The jq is a versatile command-line JSON processor.

$ brew set up jq

Then, create a template file conatiner.tmpl.json like so:


"containers":
"app":
"picture": "",
"ports":
"8080": "HTTP"


,
"publicEndpoint":
"containerName": "app",
"containerPort": 8080,
"healthCheck":
"healthyThreshold": 2,
"unhealthyThreshold": 2,
"timeoutSeconds": 5,
"intervalSeconds": 10,
"path": "/",
"successCodes": "200"


The app.picture needs to be the identify of a pushed container picture. To get the identify dynamically, we are able to use the get-container-image command like so.

$ aws lightsail get-container-images --service-name websocket-lightsail | jq -r '.containerImages[0].picture'
:websocket-lightsail.app.1

Then create a container.json working by this:

$ jq --arg picture `aws lightsail get-container-images --service-name websocket-lightsail| jq -r '.containerImages[0].picture'` '.containers.app.picture = $picture' container.tmpl.json > ./container.json

The generated file ought to appear to be this:


"containers":
"app":
"picture": ":websocket-lightsail.app.1",
"ports":
"8080": "HTTP"


,
"publicEndpoint":
"containerName": "app",
"containerPort": 8080,
"healthCheck":
"healthyThreshold": 2,
"unhealthyThreshold": 2,
"timeoutSeconds": 5,
"intervalSeconds": 10,
"path": "/",
"successCodes": "200"


In the long run, deploy our software by working the create-container-service-deployment like so:

$ aws lightsail create-container-service-deployment --service-name websocket-lightsail --cli-input-json file://$(pwd)/container.json

As soon as working the command, the response prints to the console like this. Now you can entry the URL.


"containerService":
"containerServiceName": "websocket-lightsail",
"url": "https://xxxx.xxxx.ap-northeast-1.cs.amazonlightsail.com/"

To deploy the latest picture routinely, create a Makefile and put some script like so:

We are able to now deploy a brand new model of our app by working:

$ make construct push deploy

With a view to hook up with the server via WebSocket, use wss protocol like so:

We’ve coated methods to deploy and hook up with Amazon Lightsail on this submit. The takeaway is as follows:

  • Construct Docker picture
  • Push container picture
  • Deploy it

That’s all there to it. Quite simple and straightforward to make use of.

More Posts