At A Cloud Guru we're a big fans of Serverless and automation. We're excited to see more people building Serverless applications. An Alexa app using Lambda is a great way to get started.

Most people are a long way from being 100% Serverless. We want to help those people become Serverless. Our project takes inspiration from Chaos Monkey. It is a skill that terminates EC2 instances with Alexa.

If you received an Amazon Echo Dot at AWS re:Invent, this is a great project to get started with.

Submitted Skill ID number

amzn1.ask.skill.69483f2c-0154-4cf1-a9d9-f7b697c65a54

Entry Video

The dashboard in the the above video can be visited here: http://alexa-chaos.acloud.guru/

The Project

The Alexa integration uses AWS Lambda to handle Alexa Skills kit requests. The Lambda function processes skills request from Alexa. The function calls Lambda functions in the Chaos Service to perform actions based on the intent.

We created a dashboard to allow people to visualise EC2 instances. The dashboard is a React app with a GraphQL backend on AWS Lambda. The Lambda function calls the same functions in the Chaos service to process requests.

Architecture Diagram

This architecture is an example of the Backend for Frontend pattern. This pattern allows us to use the same logic for the Alexa service and dashboard.

Usage and Code Samples

To terminate servers users can say:

Alexa, ask chaos monkey to kill five servers

Alexa identifies our chaos skill using the chaos monkey keyword and that our intent is to kill servers. We also configured a slot to allow the user to specify that they want to terminate five servers.

{ "intents": [ { "intent": "RandomKill", "slots": [{ "name": "Count", "type": "AMAZON.NUMBER" }] } ] }

In our intent handler checks the Count slot and terminates instances.

module.exports = (intent) => { const countSlot = intent.slots ? intent.slots.Count : null; const count = countSlot ? countSlot.value : 1; return chaosService .terminate({ count }) .then(() => ({ sessionAttributes: {}, cardTitle: "Kill", speechOutput: `Booooom`, repromptText: "", shouldEndSession: true })); };

The chaosService library invokes the terminate instances Lambda and returns a result.

In the chaosService we use the aws-sdk to terminate random instances.

const terminate = (ec2, instanceIds) => { console.log('Terminating instances', { instanceIds }); promisfy(ec2); const params = { InstanceIds: instanceIds }; return ec2.terminateInstancesAsync(params); }

We used the same approach to support starting and counting instances through the Alexa.

Running Chaos Alexa yourself

Prerequisites

1. An AWS account - sign up

2. An Amazon Developers account - get started

Deploying the Project

1. Clone the Alexa Service and Chaos Service from Github

2. Follow the instructions in each README to setup your environment.

3. Deploy Alexa service and chaos service to AWS using ./deploy.sh

4. Create a skill - (note you might not be able to use chaos monkey)

6. Add your ALEXA_APPLICATION_ID to the Alexa Service and re-deploy. You can retrieve the Application ID here:

7. Configure the Intent Schema and Sample Utterances on the Interaction model

8. Configure your skill to invoke your Lambda function

10. Enable test mode and add the skill to your Alexa

Bonus Level - Chaos Dashboard

We created the Chaos dashboard for demonstration purposes. You don't need to deploy the dashboard to run the project. But if you would like to:

1. Clone the Dashboard and Dashboard Service from Github

2. Deploy the Dashboard Service using ./deploy dev and get the API Gateway URL.

3. Update the the GRAPHQL_ENDPOINT in nwb.config.js and run nwb build .

4. Upload to S3 and configure website hosting

Our dashboard can be seen in action at http://alexa-chaos.acloud.guru/

Have fun!

You should now be able to start servers, count servers and delete servers at whim. Please be careful. We take no responsibility for any chaos this may cause.

You can find all the code on Github. If you have suggestions or improvements please go and add an issue there.