Speak the Command, Execute the Lambda

Kasun de Silva - Dec 19 - - Dev Community

In today's world of voice assistants and smart devices, integrating your AWS Lambda functions with Amazon Alexa opens up a world of possibilities. Imagine being able to trigger a Lambda function with a simple voice command, automating tasks like checking the weather, controlling smart devices, or querying your AWS resources. In this blog post, we’ll walk you through the steps to create a voice-activated Lambda function using Amazon Alexa.

Step 1: Setting Up Your AWS Lambda Function

Before you can trigger a Lambda function with your voice, you need to create and configure the function.

1.1 Create the Lambda Function

Log in to the AWS Management Console and navigate to the Lambda service.

Create a new function:

  • Choose "Author from scratch."
  • Enter a function name (e.g., WeatherCheckerLambda).
  • Choose a runtime (e.g., Python 3.9 or Node.js 18.x).
  • Set up an execution role with basic Lambda permissions. If your function needs to access other AWS services, ensure the role has the appropriate permissions.

Write your function code: Here’s a simple Python example that returns a weather update:

import json

def lambda_handler(event, context):
    # Sample response to be returned by Alexa
    return {
        'version': '1.0',
        'response': {
            'outputSpeech': {
                'type': 'PlainText',
                'text': 'The weather today is sunny with a high of 25 degrees.',
            },
            'shouldEndSession': True
        }
    }

Enter fullscreen mode Exit fullscreen mode

1.2 Configure the Lambda Function

  • Set up environment variables and resource limits according to your needs.
  • Test the function using the AWS Lambda console's built-in test feature. This ensures that your function executes as expected before integrating with Alexa.

Step 2: Creating an Alexa Skill

Now that your Lambda function is ready, you’ll create an Alexa skill to trigger it.

2.1 Set Up the Alexa Skill

Log in to the Alexa Developer Console.

Create a new skill:

  • Choose “Create Skill” and select the “Custom” skill type.
  • Set your skill name (e.g., Weather Checker).
  • Select other for the experience type.
  • Choose a template: Start from scratch or use the provided templates for a custom experience.

Image description

  • Hit Create Skill

2.2 Designing the Interaction Model

Invocation Name:

  • Set an invocation name for your skill (e.g., weather checker).
  • This is what users will say to start the interaction (e.g., “Alexa, ask weather checker...”).
  • Hit Build Skill

Create Intents:

  • Intents represent actions users want to perform. For example, create an intent called GetWeatherIntent.
  • Under the GetWeatherIntent, add sample utterances like:
    • "What is the weather today?"
    • "Tell me the weather."
    • "Give me today's weather forecast."

Image description

Slots: If your skill needs more specific inputs, such as a city name, you can define slots. For this example, a simple weather checker might not need them.

2.3 Configuring the Endpoint

Link the Skill to Your Lambda Function:

  • In the Alexa Developer Console, navigate to the Endpoint section.
  • Select AWS Lambda ARN as the service endpoint type.
  • Paste the ARN of the Lambda function you created earlier.

Image description

Skill Permissions:

  • Ensure your Lambda function’s IAM role has the necessary permissions to execute and return data.

Add Lambda Trigger

Image description

Add “Alexa Skills kit” from the “Add triggers” sidebar in aws console. (Shown as in screenshot above)

Next you have to click on the “Alexa Skills kit”, then this will show up

Add your skill ID in the above field.

Moreover, make sure you add this lambda function ARN in your skill’s endpoint. Just paste the ARN in your skill’s endpoint.

Build and Save it.

Image description

Step 3: Coding the Lambda Function to Handle Alexa Requests

Your Lambda function needs to be able to handle requests from Alexa and respond appropriately.

3.1 Handle Alexa Requests in Your Lambda Function

Here’s an expanded version of the Lambda function that parses the input and returns a relevant response:

import json

def lambda_handler(event, context):
    # Extracting intent name from Alexa's request
    intent_name = event['request']['intent']['name']

    if intent_name == "GetWeatherIntent":
        # Build response
        return {
            'version': '1.0',
            'response': {
                'outputSpeech': {
                    'type': 'PlainText',
                    'text': 'The weather today is sunny with a high of 25 degrees.',
                },
                'shouldEndSession': True
            }
        }
    else:
        return {
            'version': '1.0',
            'response': {
                'outputSpeech': {
                    'type': 'PlainText',
                    'text': 'I am not sure how to help with that.',
                },
                'shouldEndSession': True
            }
        }

Enter fullscreen mode Exit fullscreen mode

3.2 Testing the Lambda Function

  • Use the AWS Lambda console to trigger the function manually with test events that simulate Alexa requests.
  • Enable detailed logging in CloudWatch to troubleshoot any issues and ensure your function handles requests correctly.

Step 4: Testing and Deploying the Alexa Skill

4.1 Testing with the Alexa Simulator

  • Go to the Alexa Developer Console and open your skill.
  • Use the Test tab: Here, you can simulate voice commands using the Alexa Simulator. Type or speak phrases like “Alexa, ask weather checker what the weather is today.”
  • Review responses: Ensure Alexa returns the expected responses, and debug if necessary.

4.2 Testing with a Real Device

  • Enable your skill on an Alexa-enabled device, such as an Amazon Echo.
  • Speak the invocation name and command: For example, say “Alexa, ask weather checker what the weather is today.”
  • Refine based on feedback: Adjust your Lambda function and skill interaction model based on real-world testing.

Step 5: Deploying and Monitoring Your Skill

5.1 Deploying Your Skill

  • Publish your skill: Once satisfied with the functionality, you can submit your skill for certification through the Alexa Developer Console.
  • Private or Public: Choose whether to keep the skill private for personal use or make it available to the public.

5.2 Monitoring and Logging

  • Use AWS CloudWatch: Monitor logs to track Lambda execution, identify errors, and optimize performance.
  • Skill Analytics: In the Alexa Developer Console, you can also access analytics to see how users interact with your skill.

By following these steps, you’ve successfully created a voice-activated Lambda function using Amazon Alexa. This setup opens the door to endless possibilities, from automating daily tasks to creating rich, voice-controlled applications. Whether you’re developing a personal project or a commercial application, integrating AWS Lambda with Alexa is a powerful way to bring voice capabilities to your cloud services.

Happy coding, and enjoy the convenience of voice-triggered automation!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .