I wrote an AWS Lambda function to teach me Spanish

I wrote an AWS Lambda function to teach me Spanish

ยท

2 min read

This is Day#1 of the #ServerlessChallenge, where I'm creating/building serverless apps using different AWS services.

The short description: an app sends me a WhatsApp message every day to teach me a random Spanish word with its meaning.

The long/tech description

Using the serverless framework, I've built a simple lambda function that is being triggered with an EventBridge rule:

events: [
  {
    schedule: {
      rate: ["rate(1 day)"]
    }
  }
]

EventBridge rules are so simple and interesting to write, as example if you want to write a rule that triggers an event on only the work days at 9 am, it will be like this: cron(0 9 ? MON-FRI )

The lambda function is so small, what it does:

        // src/functions/teach/index.ts

        const word = RANDOM.word();
        const translation = await translate(word, 
            'process.env.LANGUAGE_TO',
            'process.env.LANGUAGE_FROM'
        );
        const message = {
            from: `whatsapp:${process.env.TWILIO_SENDER}`,
            body: `Hola! ๐Ÿ™‹ \n
                   Today's word is: *${translation.text}* \n
                   Which means: *${translation.translation}*`,
            to: `whatsapp:${process.env.TWILIO_RECEIVER}`
        };

        await client.messages.create(message);

And as my favorite song says: โ™ซ Don't place the API key/secrets & configs in the code directly, my friend โ™ซ

You can safely store any keys/secrets/configs inside the serverless configuration provider.environment and they will be added as env variables for the lambda function

// serverless.ts
const serverlessConfiguration: AWS = {
  //..
  provider: {
    environment: {
      TWILIO_ACCOUNT_SID: 'xx',
      TWILIO_AUTH_TOKEN: 'xx',
      TWILIO_SENDER: 'xx',
      TWILIO_RECEIVER: 'xx',
      LANGUAGE_TO: 'es',
      LANGUAGE_FROM: 'en'
    },
  },
}

the github repo: https://github.com/Rochdy/teachMeSpanish

That was the app of Day#1! looking forward to posting soon about the next app of the challenge.

ย