Search code examples
amazon-web-servicesaws-lambdaaws-cliamazon-lex

What's the fastest way to update the lambda function being hit by lex testing?


I have a lex bot and intent, and a lambda function handling the fulfillment and verification for the bot. I'm working on building an effective development workflow for development.

What is the most effective way to make sure that the Lambda function I'm hitting is on the latest version?

Currently I'm updating the function code in my IDE, and updating it with this script:

#!/bin/bash -e
rm lambda.zip || echo "Cleaning zip"
zip lambda.zip *.js -r node_modules/
aws lambda update-function-code \
    --profile myprofile \
    --function-name my-lambda-function \
    --zip-file fileb://lambda.zip

I then use the test interface in the lex console to update it, and read the log output with this script:

#!/bin/bash
LOGSTREAM=$(aws logs describe-log-streams --profile myprofile --log-group-name /aws/lambda/my-lambda-function | jq -r '.logStreams | .[-1].logStreamName')
aws logs get-log-events --profile myprofile --log-group-name /aws/lambda/my-lambda-function --log-stream-name "$LOGSTREAM" --limit 5

Does this use of the AWS cli produce any major synchronization issues? Is there a better way to have a quick development cycle?

Edit:

Thanks to Marcin, I've updated this to use aliases, and here's the resulting deployment code:

#!/bin/bash -e
rm lambda.zip || echo "Cleaning zip"
zip lambda.zip *.js node_modules/
VERSION=$(aws lambda update-function-code \
    --profile myprofile \
    --publish \
    --function-name my-lambda-function \
    --zip-file fileb://lambda.zip \
    | jq -r .Version)
aws lambda update-alias \
    --profile myprofile \
    --function-name my-lambda-function \
    --name dev \
    --function-version $VERSION

Solution

  • Generally you would use lambda aliases. With that you would provide arn of the alias to your lex's verification and fulfillment options.

    Then when you update your function, you publish a new version of it, and point the alias to the version.

    This process ensures that you are fully decoupling lex from your lambda code. This is because the alias is static and does not require any changes in the lex configurations. The only thing that changes is that the alias points to new versions of your lambda function. This is transparent process from the lex's perspective.