Hello, is it possible to add an Alexa trigger to m...
# help
c
Hello, is it possible to add an Alexa trigger to my lambda function created using sst? In normal Serverless it's one line of yaml: https://www.serverless.com/framework/docs/providers/aws/events/alexa-skill
f
Hey @Chris Laplante, SST doesn’t have a high level construct for Alexa yet, but you can use CDK’s Alexa Ask construct directly. Alternatively, I found this helper construct from AWS. Save it to ur local and use it like this:
Copy code
import { Skill } from "./Skill";

const fn = new sst.Function(this, 'SkillFn', { ... });

const skill = new Skill(this, 'Skill', {
  endpointLambdaFunction: fn,
  skillPackagePath: 'src/skill-package',
  alexaVendorId,
  lwaClientId,
  lwaClientSecret,
  lwaRefreshToken,
});
Here’s the full example from AWS blog. It’s similar to the above code. Instead of the PythonFunction, we are using
sst.Function
to use Live Lambda Dev.
Let me know if that makes sense.
c
Hi @Frank, thanks for the quick response! I did see that blog post, but I think it's not quite what I'm looking for. I don't want to create the skill itself using CDK or SST, I just want to create a lambda handler for an existing skill and give it the proper permissions / event sources so that ASK can invoke it.
I took a look at what Serverless Framework does. I'm not 100% certain, but I think all it's doing is creating a permissions object? https://github.com/serverless/serverless/blob/ed111e021b286eb4a68f9b2fce7fb641e03340bd/lib/plugins/aws/package/compile/events/alexa-skill.js#L63-L76
f
I think the easiest approach would be to create a SLS project (ie. serverless.yml) with just 1 function and triggered by Alexa. Run
sls package
, and look at the AWS resources in the CFN template.
There should just be 1 or 2 AWS resources glueing the Lambda function together w/ the existing Skill. We just have to create them in SST.
That was the output of this serverless.yml:
Copy code
service: aws-node-project

frameworkVersion: '3' 

provider:
  name: aws 
  runtime: nodejs14.x

functions:
  hello:
    handler: handler.hello
    events:
      - alexaSkill: amzn1.ask.skill.xx-xx-xx-xx-xx
The only Alexa-ish thing I see is:
Copy code
"HelloLambdaPermissionAlexaSkill1": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": {
          "Fn::GetAtt": [
            "HelloLambdaFunction",
            "Arn"
          ]
        },
        "Action": "lambda:InvokeFunction",
        "Principal": "<http://alexa-appkit.amazon.com|alexa-appkit.amazon.com>",
        "EventSourceToken": "amzn1.ask.skill.xx-xx-xx-xx-xx"
      }   
    }
f
Ah it seems u can do:
Copy code
import * as iam from "aws-cdk-lib/aws-iam";

const fn = new sst.Function(stack, "SkillFn", { ... });

fn.addPermission('Skill Invocation', {
  principal: new iam.ServicePrincipal('<http://alexa-appkit.amazon.com|alexa-appkit.amazon.com>'),
  eventSourceToken: "amzn1.ask.skill.xx-xx-xx-xx-xx",
});
c
Sweet, I'll give it a try. Thanks!
f
which version of SST r u using?
c
Sorry, 'import * as iam from "aws-cdk-lib/aws-iam";' worked for me. I missed that line when I initially copy-pastad it.
FWIW, Framework Core: 3.4.0 Plugin: 6.1.2 SDK: 4.3.1
One other question if you don't mind - I'm finding that I need to add deps into the top-level package.json for them to work, even when it's something used by stuff in backend. But adding them to backend/package.json doesn't seem to work. Is that expected?
f
which dep is this?
c
There are a few that the backend lambda needs, but the first I tried adding are uuid and axios. When I tried to add it to backend/package.json I got for example:
Copy code
Error: There was a problem transpiling the Lambda handler: ✘ [ERROR] Could not resolve "axios"
    backend/functions/lib/lwa.ts:4:18:
      4 │ import axios from "axios";
        ╵                   ~~~~~~~
  You can mark the path "axios" as external to exclude it from the bundle, which will remove this error.
Also I can confirm the code you posted works to add Alexa as a trigger for the lambda 🙂
f
hmm that’s weird.. adding to
backend/package.json
should work.
Btw, do u have
srcPath
set to
backend
in
index.ts
or for individual functions?
c
Yes I've got it set in index.ts
I'll dig in a bit more when I'm less sleepy. Thanks again for all your help @Frank! It's seriously crazy how I get better and faster support on SST than I do for most commercial products I pay for 🙂 Much appreciated.
f
lol I’d be even happier if you said SST instead of SLS 😂
c
Damn too many acronyms! Sorry lol. Thanks again
f
haha all good 👍