Tag: aws

SSL HTTPS with Elastic Beanstalk Node.JS backend

If you used Elastic Beanstalk to deploy automatically your Node.JS backend on your EC2, you will have an URL to access your app deployed but you will see that by default it doesn’t have a secure certificate

The backend is not on a secure URL

Add a certificate on AWS Certificate Manager (ACM)

You will not be able to have an SSL certificate if you don’t have domains on ACM. You can have it for FREE if you already have domains. It can be on another web hosting provider.

– If you don’t have domains yet, you should have one.
You can buy one on ACM on amazon directly or buy one on another domain provider.
You might find some domains providers for free but usually the service will be bad or slow, and the address will not be simple, usually like yourdomain.freehostprovider.com

– If you have domains
if you buy it with ACM it’s automatic you don’t need to do anything, if it’s on another web host provider you will need to link it with a DNS configuration, by adding CNAME fields. Let’s see how to do this.

Request a new certificate that will be used for your backend application

It will be asked to fill a form where you will have to put the domain you want for your application. You can put something like : backend.yourdomain.com
After filling the form, few minutes after, you will be able to copy the CNAME fields (if your domain is not on AWS):

Copy the CNAME fields with the copy buttons to your web host provider
Copy the CNAME informations in the DNS configuration of your web host provider

If you have changed the DNS on your web host provider, you have to wait 20 minutes.
Also, you may need to add a “A” field on the DNS of your host provider to link the IP of your beanstalk to the domain. You can do that with a ping on the beanstalk generated domain, and copy it in the DNS configuration:

Configuration of your load balancer on Elastic Beanstalk

On Elastic Beanstalk, you can now configure your SSL domain

Go in the Configuration of your Elastic Beanstalk app

Then, go to the Load balancer configuration and add the HTTPS protocol and the port 443 on the listeners.

HTTPS is now enabled in the listeners
We upgraded the listeners
The HTTPS certificate is now working for the backend

Google Cloud Function VS AWS Lamdba (how to import)

There are some differences between Google Cloud and AWS when you want to deploy your function for your API.

My point of view is that Google Cloud function may be a bit more simple to deploy but AWS is in a way the most reliable and complete cloud environnement nowadays.

On both cloud environnement you can upload the code with a ZIP, you can so use the same way to adjust your code for AWS:

We will upgrade the code with a repository that will be in a ZIP

Differences between Google Cloud functions and AWS Lambda (Node.JS)

THE DEPENDENCIES
– In Google Cloud, the dependencies are automatically installed from the package.json file
– In AWS, you need to install the dependencies with npm or yarn, that will build the node_modules folder
If you don’t have the node_modules folder, your Lamdba function will not work with the following error:

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'MODULE'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'MODULE'",
...

THE MAIN FUNCTION

With Google Cloud, the example function looks like this:

exports.helloWorld = (req, res) => {

  res.status(200).send(message);
};
  • You use the res object with the send() method to send the response

With AWS, the example function looks like this:

exports.handler = async (event) => {

    const response = {
        statusCode: 200,
        body: JSON.stringify(jsonObj),
    };
    return response;
};
  • You return a JSON object response that should contains the body and the statusCode

So the same code will looks like this:

Using parameters

Let’s say now we want to use a parameter for our route, that will be number and the value will be 10, so we will call the route helloWorld?number=10

On GOOGLE CLOUD, to get the “number” parameter, we will use the req object :

exports.helloWorld = (req, res) => {
    let number = req.query.number;

On AWS, to get the “number” parameter, we will use the event object :

exports.handler = async (event) => {
    let number = event["params"]["number"];

Create quickly API with AWS – Lambda/API Gateway

Amazon Web Services — Wikipédia

This article is dedicated to new users to AWS – Amazon Web Services – that wants to use this cloud instead for others (Azure, Google Cloud) which seems to be the best choice at the moment. Indeed, AWS is the most complete scalable and dynamic cloud solution.

However, this is not so simple to understand all the concepts, if you look for simplicity you may be interested by Google Cloud that I used before.

In order to use API Gateway and Lamdba, you should add them in favorite in the Services:

Create your API functions in AWS Lambda

We can use AWS Lamdba to create routes that can be used for our applications.

So, you can click Create function, in order to see the form bellow.
Let’s say we want to create a route to list the products of our app, it will be a GET method and we will call it getProducts.
We will use Node.JS because I have to habit to use Javascript and Typescript but you can choose the runtime you are in ease.

Then you can click Create function, we don’t need the others settings.

You should be now able to see the function overview with the default example in the code editor:

You can try the example it will show the string “Hello from Lambda”, the response that is returned in the example.
We can now write our own function from scratch here or alternatively we can upload our code from a ZIP file.

Create our API Gateway route (HTTP)

In the overview of the Lambda function you can see the button Add trigger, you can use it to add the API event that will call our function.

Click “Add trigger”
Select “Create API”, then “HTTP API” (best for cost) and “Open” the endpoint so your app can use it

You can then click Save and so you should see your new trigger, the API Gateway trigger:

Now you can try it by going on the API endpoint link in the details.
You can use it like it in your application or you can change it by adding your own domain, for exemple api.myapp.com/getProducts
You can do that in Custom Domain Names or directly in your domain provider, in the DNS configuration, making a redirection.

Creating an API Gateway (REST API)

You could also, if you need it, use multiple methods such GET, POST, UPDATE, DELETE, … if you created a REST API Gateway :

You can then use your Lambda function for each methods you want to use.