Save User Info with a Lex Chatbot¶
| Category | AWS Chatbots (Amazon Lex) |
| Status | โ Completed |
| Repo | nextwork-save-user-info-with-a-lex-chatbot |
| 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 Save User Info with a Lex Chatbot 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 4 makes BankerBot remember. After a balance check, an output context (contextCheckBalance) stores the verified date of birth for a few turns. A new FollowupCheckBalance intent ('How about my savings account?') only activates while that context is live, and fills dateOfBirth from it, so the user isn't asked again.
Summary¶
On real Lex V2 + Lambda, the Lambda saved the verified date of birth in the contextCheckBalance output context. Follow-ups like "How about my savings account?" and "What about credit?" were answered by FollowupCheckBalance without asking for the date of birth again, because its slot default is #contextCheckBalance.dateOfBirth. Deleted afterwards.
The checks behind this are in docs/verification.txt.
Skills & tools¶
Amazon Lex V2, Conversational AI, Intents & utterances, NLU, Python, pytest, Terraform, Contexts, Session state, Dialog management
What I built¶
bot/bankerbot.json: CheckBalanceoutputContexts, plus FollowupCheckBalance withinputContextsand a default value from the context.lambda/lambda_function.py: returnsactiveContextswithcontextAttributes.dateOfBirth, and adds afollowup_check_balancehandler.tests/test_part4.py: the follow-up skips the DOB question, and without an active context the bot must re-verify.
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-save-user-info-with-a-lex-chatbot 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:42:57] created IAM role NW-LexRole-d4f00d (Lex runtime role)
[21:43:11] created Lambda BankingBotEnglish-d4f00d (python3.12) from lambda/lambda_function.py
[21:43:22] created Lex V2 bot BankerBot-d4f00d (H0RV3JMPOL)
slot type accountType: 3 values
intent WelcomeIntent: 7 utterances, slots=[]
intent FallbackIntent: custom closing response
intent CheckBalance: 7 utterances, slots=['dateOfBirth', 'accountType'], Lambda fulfillment, contexts in=None out=['contextCheckBalance']
intent FollowupCheckBalance: 3 utterances, slots=['accountType', 'dateOfBirth'], Lambda fulfillment, contexts in=['contextCheckBalance'] out=[]
[21:43:28] building locale en_US ...
[21:44:08] locale built
[21:44:09] TestBotAlias now calls the Lambda for fulfillment
=== Conversation with the REAL bot (lexv2-runtime recognize-text, TestBotAlias) ===
--- new session
you> Hello
bot> Hello! BankerBot here. Ask me about your account balance or a transfer. [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> Which account: checking, savings or credit? [intent=CheckBalance state=InProgress]
you> checking
bot> Thank you. The balance on your Checking account is $3,507.33 dollars. [intent=CheckBalance state=Fulfilled]
you> How about my savings account?
bot> Thank you. The balance on your Savings account is $2,747.42 dollars. [intent=FollowupCheckBalance state=Fulfilled]
you> What about credit?
bot> Thank you. The balance on your Credit account is $2,452.94 dollars. [intent=FollowupCheckBalance 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:42 to 21:44 CST on 2026-12-02, then destroyed and verified gone:
=== TEARDOWN 21:44:17 ===
deleted Lex bot H0RV3JMPOL
deleted Lambda BankingBotEnglish-d4f00d
deleted IAM role NW-LexFnRole-d4f00d
deleted IAM role NW-LexRole-d4f00d
bots left in account: none
lambdas left: none
=== teardown verified 21:44:21 ===
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/19e304aa-a759-5670-97ff-e3e1c45436eb ยท 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%]
10 passed in 0.02s
BankerBot (type 'quit' to exit)
you> Hello
bot> Hi! I'm BB, the Banking Bot. How can I help you today?
you> Check my account balance
bot> For verification purposes, what is your date of birth?
you> 1990-04-21
bot> Which account: checking, savings or credit?
you> savings
bot> Thank you. The balance on your Savings account is $2,762.38 dollars.
Success! The configuration is valid.
[exit code 0]