forked from aws-samples/amazon-lex-bi-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-bot.sh
89 lines (75 loc) · 3.05 KB
/
build-bot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
#
# Builds the bot, intents, and custom slot types
#
#
# Environment variables to be set in the CodeBuild project
#
# $BOT Name of the Lex bot
# $INTENTS List of intent names for the bot
# $SLOTS List of slot type names for the bot
# $LAMBDA Name of the Lambda fulfillment function for the bot
# $LAMBDA_ROLE_ARN ARN for the Lambda execution role
# $ATHENA_DB Name of the Athena database
# $ATHENA_OUTPUT_LOCATION Name of the S3 bucket for Athena output
#
# Load the Lambda functions for each Intent
echo "Lambda execution role = $LAMBDA_ROLE_ARN"
for i in $INTENTS
do
module_name=`echo $i | tr '[:upper:]' '[:lower:]'`
echo "Creating Lambda handler function: ${LAMBDA}_${i} from ${module_name}.py"
# echo aws lambda create-function \
# --function-name ${LAMBDA}_${i} \
# --description "${LAMBDA} ${i} Intent Handler" \
# --timeout 300 \
# --zip-file fileb://${i}.zip \
# --role $LAMBDA_ROLE_ARN \
# --handler ${module_name}.lambda_handler \
# --runtime python3.6 \
# --environment "Variables={ATHENA_DB=$ATHENA_DB,ATHENA_OUTPUT_LOCATION=$ATHENA_OUTPUT_LOCATION}"
aws lambda create-function \
--function-name ${LAMBDA}_${i} \
--description "${LAMBDA} ${i} Intent Handler" \
--timeout 300 \
--zip-file fileb://${i}.zip \
--role $LAMBDA_ROLE_ARN \
--handler ${module_name}.lambda_handler \
--runtime python3.6 \
--environment "Variables={ATHENA_DB=$ATHENA_DB,ATHENA_OUTPUT_LOCATION=$ATHENA_OUTPUT_LOCATION}" \
>/dev/null
# echo "Adding permission to invoke Lambda handler function ${LAMBDA}_${i} from Amazon Lex"
# echo aws lambda add-permission \
# --function-name ${LAMBDA}_${i} \
# --statement-id chatbot-fulfillment \
# --action "lambda:InvokeFunction" \
# --principal "lex.amazonaws.com"
aws lambda add-permission \
--function-name ${LAMBDA}_${i} \
--statement-id chatbot-fulfillment \
--action "lambda:InvokeFunction" \
--principal "lex.amazonaws.com" \
>/dev/null
echo; echo; echo
done
for i in $SLOTS
do
echo "Creating slot type: $i"
aws lex-models put-slot-type --name $i --cli-input-json file://slots/$i.json >/dev/null
done
# build the intents
for i in $INTENTS
do
echo "Creating intent: $i"
# substitute the ARN for the Lambda intent handler function
LAMBDA_ARN=`aws lambda get-function --function-name ${LAMBDA}_${i} | grep 'FunctionArn' | sed 's/.*FunctionArn": "\(.*\)".*/\1/'`
sed "s/{{lambda-arn}}/$LAMBDA_ARN/" intents/$i.json >intents/$i-updated.json
# echo "ARN for $i = `grep -i arn intents/$i-updated.json`"
aws lex-models put-intent --name $i --cli-input-json file://intents/$i-updated.json >/dev/null
done
# build the bot
echo "Creating bot: $BOT"
if aws lex-models put-bot --name $BOT --cli-input-json file://bots/$BOT.json >/dev/null
then echo "Success: $BOT bot build complete."; exit 0
else echo "Error: $BOT bot build failed, check the log for errors"; exit 1
fi