Connect Amazon Lex with Lambda¶
| Category | AWS Chatbots (Amazon Lex) |
| Status | โ Completed |
| Repo | nextwork-connect-amazon-lex-with-lambda |
| Original NextWork guide | nextwork.ai |
Learning project
Practice work following a NextWork guide, separate from my production projects.
๐ NextWork learning project. This is a practice project I built while working through NextWork's Connect Amazon Lex with Lambda guide. It is part of my Learning portfolio and is fully separate from my production projects.
Category: Learning โบ AWS Chatbots (Amazon Lex)
Status: โ
Completed
Overview¶
Part 3 gives BankerBot a brain. When CheckBalance has all its slots, Lex invokes a Lambda function (the fulfillment code hook) that 'looks up' the balance (a random number, as in the guide) and closes the dialog with the answer.
Summary¶
Deployed the Python 3.12 fulfillment Lambda and wired it to the real Lex V2 bot's TestBotAlias as its code hook. After the bot collected date of birth and account type, Lex invoked the Lambda, which answered "The balance on your Savings account is $547.88 dollars". The bot, Lambda, IAM roles and log group were deleted afterwards.
The checks behind this are in docs/verification.txt.
Skills & tools¶
Amazon Lex V2, Conversational AI, Intents & utterances, NLU, Python, pytest, Terraform, AWS Lambda, Fulfillment code hooks, Serverless
What I built¶
lambda/lambda_function.py:lambda_handlerโdispatchโcheck_balance, which reads slots fromsessionState.intent.slotsand returns aClosedialog action.lex.tf: adds the Lambda (archive_file zip), its role, and alambda:InvokeFunctionpermission forlexv2.amazonaws.com.tests/test_part3.py: fulfillment through the bot, plus a direct Lex V2 event test.
How to run¶
pip install pytest
pytest -q tests # scripted conversations against the bot definition + Lambda
python3 chat.py # chat with BankerBot locally
terraform init && terraform validate # real Lex V2 resources (apply needs an AWS account)
Verification¶
I ran the verification command on 2026-12-03. The full output is in docs/verification.txt.
PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python -m pytest -q -p no:cacheprovider tests && printf 'Hello\nCheck my account balance\n1990-04-21\nsavings\n' | /workspace/nextwork-portfolio/.venv/bin/python chat.py && TF_PLUGIN_CACHE_DIR=/workspace/nextwork-portfolio/tf-plugin-cache terraform init -backend=false -input=false >/dev/null && terraform validate -no-color; rc=$?; rm -rf .terraform .terraform.lock.hcl .build; exit $rc
Real AWS deployment (2026-12-02)¶
Note:
docs/aws-real-run.txtis the log of the original real-AWS run, so its fallback reply is the bot's wording at that time. The bot definition has since been reworded in my own words.
I deployed this project to a real, dedicated AWS learning account (us-east-1, kept separate from everything else I run), checked it, captured the output, and tore it all down right after. Every resource carried the tag Project=nextwork-connect-amazon-lex-with-lambda so I could prove nothing was left behind.
How I ran it: python3 real-aws-run.py . real-conversation.txt (in this repo) reads bot/bankerbot.json and creates the real Lex V2 bot through the AWS API: IAM role, bot, en_US locale, slot types, intents, slots and contexts, plus the fulfillment Lambda where the guide has one. It builds the locale, holds the scripted conversation through the Lex runtime (RecognizeText on the TestBotAlias), and then always deletes everything, even if a step fails. (The lex.tf here is a partial Terraform sketch. I used the API script because it builds the complete bot from the same JSON the local simulator uses.)
Evidence (excerpt; the full, sanitized transcript is in docs/aws-real-run.txt, with the account ID redacted):
[21:40:09] created IAM role NW-LexRole-1b2851 (Lex runtime role)
[21:40:23] created Lambda BankingBotEnglish-1b2851 (python3.12) from lambda/lambda_function.py
[21:40:34] created Lex V2 bot BankerBot-1b2851 (LS7FTSFY7N)
slot type accountType: 3 values
intent WelcomeIntent: 7 utterances, slots=[]
intent FallbackIntent: custom closing response
intent CheckBalance: 7 utterances, slots=['dateOfBirth', 'accountType'], Lambda fulfillment
[21:40:39] building locale en_US ...
[21:41:13] locale built
[21:41:13] TestBotAlias now calls the Lambda for fulfillment
=== Conversation with the REAL bot (lexv2-runtime recognize-text, TestBotAlias) ===
--- new session
you> Hi
bot> Hey there, I'm BankerBot! I can check your balance or move money. What do you need? [intent=WelcomeIntent state=Fulfilled]
--- new session
you> Check my account balance
bot> For verification purposes, what is your date of birth? [intent=CheckBalance state=InProgress]
you> 1990-04-21
bot> What type of account would you like to check? [intent=CheckBalance state=InProgress]
you> savings
bot> Thank you. The balance on your Savings account is $547.88 dollars. [intent=CheckBalance state=Fulfilled]
--- new session
you> What is the balance in my visa account?
bot> For verification purposes, what is your date of birth? [intent=CheckBalance state=InProgress]
you> April 21 1990
bot> Thank you. The balance on your Credit account is $443.12 dollars. [intent=CheckBalance state=Fulfilled]
--- new session
you> what is the weather on Mars
bot> [fallback reply in the bot's original wording at the time; see docs/aws-real-run.txt] [intent=FallbackIntent state=Fulfilled]
Teardown¶
Resources were up from 21:40 to 21:41 CST on 2026-12-02, then destroyed and verified gone:
=== TEARDOWN 21:41:23 ===
deleted Lex bot LS7FTSFY7N
deleted Lambda BankingBotEnglish-1b2851
deleted IAM role NW-LexFnRole-1b2851
deleted IAM role NW-LexRole-1b2851
bots left in account: none
lambdas left: none
=== teardown verified 21:41:26 ===
Cost¶
Estimated cost of the real run: about $0.02 across two runs (a handful of Lex text requests and Lambda invocations; my first run failed at the locale build because Lex needs a space between a {slot} and a following ?, then everything was deleted and re-run). The account has a $50/month budget with email alerts at $5, $20 and $40, and the whole AWS series is logged in a private spend ledger.
Original project: https://nextwork.ai/projects/05a8853b-a1e0-5be3-a93f-adeecd72e61c ยท Part of my NextWork Learning portfolio (hub).
Verification output¶
From docs/verification.txt:
$ PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python -m pytest -q -p no:cacheprovider tests && printf 'Hello\nCheck my account balance\n1990-04-21\nsavings\n' | /workspace/nextwork-portfolio/.venv/bin/python chat.py && TF_PLUGIN_CACHE_DIR=/workspace/nextwork-portfolio/tf-plugin-cache terraform init -backend=false -input=false >/dev/null && terraform validate -no-color; rc=$?; rm -rf .terraform .terraform.lock.hcl .build; exit $rc
........ [100%]
8 passed in 0.01s
BankerBot (type 'quit' to exit)
you> Hello
bot> Hello! BankerBot here. Ask me about your account balance or a transfer.
you> Check my account balance
bot> For verification purposes, what is your date of birth?
you> 1990-04-21
bot> What type of account would you like to check?
you> savings
bot> Thank you. The balance on your Savings account is $629.86 dollars.
Success! The configuration is valid.
[exit code 0]