Search code examples
amazon-web-servicesalexa-skills-kit

Setting lambda environmental variable using ask CLI?


How can I use the Ask CLI to set lambda function environmental variables? I tried setting them using the AWS console, but after I do that I get this error when I try to run ask deploy:

[Error]: Lambda update failed. Lambda ARN: arn:aws:lambda:us-east-1:608870357221:function:ask-custom-talk_stem-default
The Revision Id provided does not match the latest Revision Id. Call the GetFunction/GetAlias API to retrieve the latest Revision Id

Solution

  • The only solution I've found is to update the variables through the AWS console and manually fetch the function's info using the AWS CLI and update the local revision id to match the revision id that's live on AWS. Here is my script:

    const path = require('path');
    const { readFileSync, writeFileSync } = require('fs');
    const execa = require('execa');
    const skillRoot = path.join(__dirname, '..');
    const functionRoot = path.join(skillRoot, 'lambda', 'custom');
    const askConfigPath = path.join(skillRoot, '.ask', 'config');
    const askConfig = JSON.parse(readFileSync(askConfigPath, 'utf8'));
    const { functionName } = askConfig.deploy_settings.default.resources.lambda[0];
    
    async function main() {
      console.log('Downloading function info from AWS');
      const result = await execa('aws', ['lambda', 'get-function', '--function-name', functionName]);
      const functionInfo = JSON.parse(result.stdout);
      const revisionId = functionInfo.Configuration.RevisionId;
    
      console.log('Downloading function contents from AWS');
      await execa('ask', ['lambda', 'download', '--function', functionName], { cwd: functionRoot, stdio: 'inherit' });
    
      console.log('Updating skill\'s revisionId');
      askConfig.deploy_settings.default.resources.lambda[0].revisionId = revisionId;
      writeFileSync(askConfigPath, JSON.stringify(askConfig, null, 2));
    
      console.log('Done');
    }
    
    main();