# I wrote an AWS Lambda function to teach me Spanish

> *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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698010595799/5c6dd7da-7708-4c7f-aaec-1d182e6740f6.png align="center")

### The long/tech description

Using the [serverless framework,](https://www.serverless.com/) I've built a simple lambda function that is being triggered with an EventBridge rule:

```typescript
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:

* Getting a random Spanish word using [paunovic/random-words](https://www.npmjs.com/package/@paunovic/random-words)
    
* Translate this word using [bing-translate-api](https://www.npmjs.com/package/bing-translate-api)
    
* Send a good-formatted WhatsApp every day with the word and its translation using [Twilio](https://www.twilio.com/en-us)
    

```typescript
        // 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<s><br></s>

```typescript
// 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](https://github.com/Rochdy/teachMeSpanish)

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