Node.js Archívum - Road to AWS https://roadtoaws.com/tag/node-js/ This is my cloud journey Thu, 13 Jun 2024 21:24:00 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.2 https://roadtoaws.com/wp-content/uploads/2021/03/cropped-avatar-32x32.png Node.js Archívum - Road to AWS https://roadtoaws.com/tag/node-js/ 32 32 Creating a Serverless Mastodon Bot https://roadtoaws.com/2023/08/29/creating-a-serverless-mastodon-bot/ https://roadtoaws.com/2023/08/29/creating-a-serverless-mastodon-bot/#respond Tue, 29 Aug 2023 12:10:02 +0000 https://roadtoaws.com/?p=760 With the growing popularity of the Fediverse, I decided to take a look at what this decentralized social network has to offer for developers. I…

A Creating a Serverless Mastodon Bot bejegyzés először Road to AWS-én jelent meg.

]]>
With the growing popularity of the Fediverse, I decided to take a look at what this decentralized social network has to offer for developers.

I chose Mastodon as my primary platform because it is the most popular of all. You may choose otherwise, as these networks can communicate seamlessly with each other no matter what server you are running.

Twitter (now: X) as a commercial company has the right to restrict or commercialize its API, which can be a struggle for startups or small developers. Mastodon is not only free and open source, but also much more developer friendly. One such feature is the support of bot accounts. You are not limited at these accounts, in fact you are encouraged to use them. In Mastodon, you can specifically mark if an account is a bot, making it more transparent to everyone. 🫶

The first step is always the hardest, choosing your Mastodon server. There are many to choose from, some are for specific communities, some are geographically restricted. If you are unsure, just stick with the oldest: mastodon.social.

Create an account here and check the This is an automated account box under your profile. This will let others know that this is a bot account. Under Development, create a new application and select the appropriate permissions. Since my bot will only publish, I only selected write:statuses.

In a previous blog post I created a website for Hungarian tech conferences. I will use this as my input source. Currently this site doesn’t offer an easy way to export information, so I modified the Jekyll code to generate a CSV file for the upcoming events. This way I can parse the data more easily.

The Serverless Approach

From the title of this post, you have probably guessed that I am going to take a serverless approach. I don’t want to deal with security updates and patches. I just want this bot to work with very little maintenance.

💡 Tip: Choose arm64 as your Lambda architecture because it is cheaper to run.

There are a handful of API clients for Mastodon to choose from. Since I will be using Node.js 18.x for the runtime, I wanted to find one that was compatible with it. My choice was Masto.js, which is maintained quite frequently and supports most of the Mastodon API features.

To download CSV data from techconf.hu, I will use Axios as in my previous projects. As for parsing CSV data my choice was csv-parse (watch out there are multiple CSV parsers out there, some names may only be different with a hyphen). I then created separate Layers for each function and attached it to my Lambda function.

Making it all work

The code is pretty simple. First I download the CSV file and parse it with csv-parse. Then I set up the Toot (Mastodon’s phrase for Tweet) and publish it with Masto.js.

One problem I faced is that in Mastodon every Toot has a language variable. If you don’t set it specifically, it defaults to the one set in your Mastodon account.

💡 Tip: Since the Fediverse is so decentralized, it is a good idea to tag all your posts.

import { parse } from 'csv-parse';
import { login } from 'masto';
import axios from 'axios';

export const handler = async(event) => {
    var tweet = "Upcoming Hungarian Tech Conferences 🇭🇺\n\n";
    var conferencesThisWeek = false;
    const currentDate = new Date();
    const endOfWeek = new Date(new Date().setDate(new Date().getDate() + 7));
    currentDate.setHours(0,0,0,0);
    endOfWeek.setHours(0,0,0,0);
    var conferenceDate;
    var csv;
    
    await axios({
        url: 'https://techconf.hu/conferences.csv',
        method: 'GET',
        responseType: 'blob'
    }).then((response) => {
        csv = response.data;
    });
    
    const parser = parse(csv, {
        delimiter: ",",
        from_line: 2
    });
    
    for await (const record of parser) {
        conferenceDate = new Date(record[3]);
        if (currentDate <= conferenceDate && conferenceDate <= endOfWeek) {
            tweet += '👉 ' +record[0] + ' (' + record[2] + ')\n📅 ' + record[3] + ' - ' + record[4] + '\n🔗 ' + record[1] + '\n\n';
            conferencesThisWeek = true;
        }
    }
    
    if (conferencesThisWeek) {
        tweet += '#Hungary #Technology #Conference';
        
        const masto = await login({
            url: 'https://mastodon.social/api/v1/',
            accessToken: ''
        });
    
        await masto.v1.statuses.create({
            status: tweet,
            visibility: 'public',
            language: 'en'
        });
    }
    
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

Scheduling

The easiest way to schedule a Lambda function is to use the Amazon EventBridge Scheduler. Simply select your schedule pattern and the Lambda function as the target, and it will execute your code at the given time.

Final Thoughts

Did I mention the best part? This is all free. The services I used are all covered by the AWS Free Tier (as of this writing).

Feel free to create similar bots or improve my code or just follow my bot at: https://mastodon.social/@techconf

A Creating a Serverless Mastodon Bot bejegyzés először Road to AWS-én jelent meg.

]]>
https://roadtoaws.com/2023/08/29/creating-a-serverless-mastodon-bot/feed/ 0
Controlling API Gateway access with Cognito https://roadtoaws.com/2021/04/14/controlling-api-gateway-access-with-cognito/ https://roadtoaws.com/2021/04/14/controlling-api-gateway-access-with-cognito/#respond Wed, 14 Apr 2021 18:47:00 +0000 http://roadtoaws.com/?p=345 During the API Gateway series, we already created an API Gateway and a new Lambda function. We named that function simple-api-auth for reason. Can you…

A Controlling API Gateway access with Cognito bejegyzés először Road to AWS-én jelent meg.

]]>
During the API Gateway series, we already created an API Gateway and a new Lambda function. We named that function simple-api-auth for reason. Can you guess why? 🤔

Cognito User Pools

Amazon Cognito is a simple and Secure User Sign-Up, Sign-In, and Access Control tool. It can manage User Pools and Identity Pools. User pools are user directories that provide sign-up and sign-in options for your app users. Identity pools provide AWS credentials to grant your users access to other AWS services.

For our API Gateway, we will create a Cognito User Pool that will handle all of our authorization tasks, including managing usernames, passwords, and access tokens.

Let’s start with Cognito and selecting Manage User Pools. Here we Create a user pool. We name our pool simple-api-AUTH and review the Step through settings as we customize our pool. ❗Remember that we cannot change these attributes after we have created the pool. Policies and other pool settings can be changed later but attributes cannot. When we are at the “App client” settings we create a new app client for our API Gateway.

Here we set up our App client. For simplicity, we will uncheck the Generate client secret option and enable the ALLOW_ADMIN_USER_PASSWORD_AUTH that we will need for our Lambda function to access.

Our User Pool is now ready. It’s that easy. 😀

Adding a user to a Cognito User Pool

We have several options to create users in our user pool. The default settings allow users to sign themselves up. We can create a simple UI or enable other identity providers like Facebook or “Sign in with Apple”. For simplicity, we will create the user manually under Users and groups.

After we have created the user the user will receive an Email with the following information:

Your username is misi and temporary password is 00eEhtI;.

It looks like everything is ready in Cognito but if we look closely we see that the user is not yet activated. The account status is: FORCE_CHANGE_PASSWORD 😡

We cannot change this in the Cognito UI so we will do this in Lambda instead.

Connecting our API Gateway to Cognito

We now head back to our API Gateway and select Authorizers. Here we Create New Authorizer.

We select the type to be Cognito and select our Cognito User Pool that we have created earlier. You can name your token source whatever you like but for following standards, we name it Authorization.

Securing an API method with Cognito

Let’s start securing our methods with Cognito authorization. I will select the GET method inside the hello resource that we have created earlier. We have set up API Keys before for this method so I will remove the API Key required option and select Cognito for our Authorization.

If we check out our method we now see that Cognito is the Authorizer.

Preparing our auth function for authentication

When we added a new Lambda function to our API Gateway we have created an auth method for our gateway. We will use this for authentication. It’s a good idea to rely on the features that Amazon API Gateway already has, including request validations. The API Gateway can validate the query string, the headers, and the body. The latter we will discuss in a later post because it requires creating a model. Setting up query string parameters is much more simple.

Let’s supply username and password as URL Query String Parameters and mark them Required. Under the Request Validator select Validate query string parameters and headers.

The AWS API Gateway will now check for these parameters and if they don’t exist the gateway will throw an error to the user.

Don’t forget to Deploy the API.

Setting up the necessary permission for Lambda

Our Lambda function needs to access our Cognito user pool. Yes, you guessed right we are going to IAM. ✨

There is no default policy for the permissions we would like to issue so we will create a new policy for it. We need AdminInitiateAuth and AdminSetUserPassword permissions for our Lambda function to manage our Cognito user pool.

Under Policies we “Create policy” and at services, we select Cognito User Pools. Under Action we select the two permissions and under Resources we add the ARN of the Cognito User Pool.

We then create this policy and attach it to our simple-api-Role as we learned in the previous post.

Confirming the user

Let’s go back to Lambda and get rid of that pesky “FORCE_CHANGE_PASSWORD” status. For this, we will write a simple Lambda function that will change the password of our user.

This is the code I used to verify the user:

const params = {
    Password: 'password',
    UserPoolId: 'Pool Id',
    Username: 'username',
    Permanent: true
};
    
await cognito.adminSetUserPassword(params).promise();

Run the code and if we set up everything correctly Cognito will show that the account status is now CONFIRMED.

Final touches

We are almost finished! We just have to write a small code that will call Cognito for authorization. Luckily we already have a sample Lambda function that we can modify: simple-api-auth

Replace the code we had earlier with this sample code:

const aws = require('aws-sdk');
const cognito = new aws.CognitoIdentityServiceProvider();

exports.handler = async (event) => {
    const params = {
        AuthFlow: 'ADMIN_NO_SRP_AUTH',
        ClientId: 'App client id',
        UserPoolId: 'Pool Id',
        AuthParameters: {
            USERNAME: event.queryStringParameters.username,
            PASSWORD: event.queryStringParameters.password
        }
    };
    
    var authResponse = await cognito.adminInitiateAuth(params).promise();
    
    const response = {
        statusCode: 200,
        body: JSON.stringify(authResponse),
    };
    return response;
};

Deploy and we are done!

Testing our API Gateway authentication

Let’s go to Postman and see if everything is working as expected.

If we call our /hello method we will receive the following error:

“message”: “Unauthorized”

Great! We need an IdToken to access this method. Let’s call our auth method to get the token. API Gateway will check if we have the username and password params. If not, we will receive an error.

We received our token. 🥳 Now if we go back to our /hello method and set the Authorization header we will have access to our function. Be sure to use the IdToken for Authorization.

And voila! Our API Gateway is now using Cognito for authentication.

A Controlling API Gateway access with Cognito bejegyzés először Road to AWS-én jelent meg.

]]>
https://roadtoaws.com/2021/04/14/controlling-api-gateway-access-with-cognito/feed/ 0
Creating a simple API Gateway on AWS https://roadtoaws.com/2021/03/30/creating-a-simple-api-gateway-on-aws/ https://roadtoaws.com/2021/03/30/creating-a-simple-api-gateway-on-aws/#comments Tue, 30 Mar 2021 10:20:53 +0000 http://roadtoaws.com/?p=36 API’s are a fundamental part of AWS, all actions are handled through API calls whether you call them on the AWS Management Console or CLI.…

A Creating a simple API Gateway on AWS bejegyzés először Road to AWS-én jelent meg.

]]>
API’s are a fundamental part of AWS, all actions are handled through API calls whether you call them on the AWS Management Console or CLI. This is why it’s a good idea to get familiar with APIs in the first place and know how to create them.

AWS recommends using an API for most of your tasks because it would be easier later to scale your cloud infrastructure. In this example, we will create a simple API that calls a Lambda function.

I recommend starting with a simple blueprint, in this way all the necessary resources will be created automatically and linked together. Later you can modify these settings.

Starting with a blueprint

Let’s start with the AWS Management Console. Select Lambda from the services and click on Create function.

Under Create function select Use a blueprint and filter for the word “api” under Blueprints. Select the microservice-http-endpoint blueprint and click Configure.

On the next screen, we configure the Lambda and API Gateway. Name your Function under Function name and set a role name under Role name. Under the API Gateway trigger select Create an API and select REST API as the API type. AWS recommends using the HTTP API because of its performance, but currently, the REST API has more features. You can see a full comparison between HTTP and REST API on this page: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-vs-rest.html
For security reasons, we select API key under Security. At a later stage, we will change this to Cognito but for now, API keys are the way to go because they are really easy to manage and provide basic security.

The Lambda and the API Gateway are now created and linked together. Great job! 🎈

Configuring the API Gateway

Let’s start configuring the API Gateway first. Select API Gateway from the services in the AWS Management Console and select the newly created API. The API is now active and open: it will accept any connection and won’t require an API key. Since we don’t need it first we delete the GET method under our sample API.

Let’s create a resource called “hello”. Select your API root (in our case simple-api) and under Actions select Create Resource. Name your resource “hello”.

Under this resource we then create a GET method. Be sure to check “Use Lambda Proxy Integration” because that is how we will read the resource name from Lambda. Also, select the Lambda Function that has been created by the blueprint automatically.

Now let’s apply some security to our API. 🔒 Select our newly created GET method and click on Method Request. Here we change API Key Required to true.

We are almost done with our API Gateway configuration! 🤸 But here comes the most important part. We should deploy our API. Under Action select Deploy API.
Before leaving our API service we should write down some important information: our Invoke URL and API key. Under Stages select your default stage, where you can find the invoke URL. Under API Keys you find the simple-api-Key which is your API key. Now we can go back to Lambda. 💾

Configuring Lambda

The blueprint created a Dynamo DB example. We won’t use Dynamo DB for now. Replace the default code with this one. And click Deploy. Now we can test our API!

const AWS = require('aws-sdk');

exports.handler = async (event, context) => {
    
    var response_status, response_body; 
    
    if (event.resource.endsWith('hello')) {
        response_status = 200;
        response_body = "Hello from Lambda";
    }
    else {
        response_status = 200;
        response_body = "Unsupported resource";
    }

    const response = {
        statusCode: response_status,
        body: JSON.stringify(response_body),
    };
    return response;
};

The best tool to test APIs is Postman. You can create an account for free.

In postman test your newly baked API. Be sure to set your API key. 😀

Well done! You now have a fully working API with basic security. 🥳🎉

A Creating a simple API Gateway on AWS bejegyzés először Road to AWS-én jelent meg.

]]>
https://roadtoaws.com/2021/03/30/creating-a-simple-api-gateway-on-aws/feed/ 2