It's the 7th day of Advent Calendar.
This time, I created a reminder BOT in the AWS environment. Cloudformation templates will also be posted, so please have a look if you are interested.
Click here for an article on implementation in Lambda (https://qiita.com/peyryo/items/ff5a2a693d47ce13fa18)
The exchange of BOT is like this.
It has the following functions.
--Event registration function --You can register the event you want to remind by entering the event title and time. --Registration will start by pressing the "Event Registration" button.
--Event reference function --You can refer to the registered event. --You can refer to the event by clicking the "Browse for event" button.
--Event notification function --BOT will notify you when the date and time of the registered event is approaching.
This time, I placed a button on the BOT using a fixed talk menu.
AWS was used to build the BOT backend. This time, I tried to build it without a server in order to make it as easy as possible.
API Gateway and Lambda are responsible for interacting with the BOT, and DynamoDB is used for talk state management.
Messages to LINEWORKS-> AWS are processed by lambda via API Gateway. Therefore, the URL of API Gateway is set in the callback URL of BOT of LINE WORKS.
Message notification to AWS-> LINEWORKS is done by Lambda for sending via SQS. The access token and authentication key required to send a message are managed by S3.
Remind notifications take advantage of CloudWatch Events to launch Lambda on a regular basis. This is achieved by polling the events in DynamoDB.
Cloudformation is used to create AWS resources. This time, I used AWS SAM, which is easy to write because I am using Lambda and API Gateway.
The names and setting values of various resources are set appropriately, so if you want to use them, change them accordingly.
template.yaml
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
#Set template parameters to pass the required credentials to the LINEWORKS BOT
Parameters:
BotNo:
Description: LINEWORKS bot number
Type: String
ApiId:
Description: LINEWORKS api id
Type: String
ServerListId:
Description: LINEWORKS server list id
Type: String
ServerApiConsumerKey:
Description: LINEWORKS server api consumer key
Type: String
#Properties that apply to all Lambda functions
Globals:
Function:
AutoPublishAlias: live
Timeout: 10
#Properties applied to Lambda functions
Environment:
Variables:
BOT_NO: !Ref BotNo
API_ID: !Ref ApiId
SERVER_LIST_ID: !Ref ServerListId
SERVER_API_CONSUMER_KEY: !Ref ServerApiConsumerKey
Resources:
# AWS ->Lambda function responsible for notifying LINE WORKS
SendLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: 'send-lineworks-message'
Handler: index.handler
Runtime: python3.7
CodeUri: lambda/send-message
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Events:
#Define SQS triggers here
SQS1:
Type: SQS
Properties:
Queue:
Fn::GetAtt:
- MessageQueue
- Arn
BatchSize: 10
# LINEWORKS ->Lambda function responsible for AWS reception processing
RecieveMessage:
Type: AWS::Serverless::Function
Properties:
FunctionName: 'recieve-lineworks-message'
Handler: index.handler
Runtime: python3.7
CodeUri: lambda/recieve-message
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Events:
PostEvent:
Type: Api
Properties:
Path: /callback
Method: post
#Lambda function to get the event registered from DynamoDB
GetEvents:
Type: AWS::Serverless::Function
Properties:
FunctionName: 'get-events'
Handler: index.handler
Runtime: python3.7
CodeUri: lambda/get-events
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Events:
#Define CloudWatch Event here
Schedule:
Type: Schedule
Properties:
#Polling interval is 5 minutes
Schedule: rate(5 minutes)
#Lambda function permissions (sweet)
#For now, apply to all Lambda functions
LambdaExecutionRole:
Description: Creating service role in IAM for AWS Lambda
Type: AWS::IAM::Role
Properties:
RoleName: !Sub 'LineWorksLambdaExecution'
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [lambda.amazonaws.com]
Action: sts:AssumeRole
Path: /
ManagedPolicyArns:
- !Sub 'arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
Policies:
-
PolicyName: lineworks-lambda-execution-role
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action: "sqs:*"
Resource: "*"
-
Effect: "Allow"
Action: "dynamodb:*"
Resource: "*"
#DynamoDB definition
LineWorksDB:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
-
AttributeName: "Hash"
AttributeType: "S"
-
AttributeName: "Range"
AttributeType: "S"
KeySchema:
-
AttributeName: "Hash"
KeyType: "HASH"
-
AttributeName: "Range"
KeyType: "RANGE"
ProvisionedThroughput:
ReadCapacityUnits: "1"
WriteCapacityUnits: "1"
TableName: lineworks-sample-table
#Set "Expire Time" to TTL
#Items can be deleted automatically by setting TTL
TimeToLiveSpecification:
AttributeName: ExpireTime
Enabled: true
Tags:
- Key: key
Value: value
#Definition of SQS
MessageQueue:
Type: 'AWS::SQS::Queue'
Properties:
QueueName: lineworks-message-queue
When deploying, I used the following script. The stack name is also appropriate.
build.sh
###Change here according to each environment
BOT_NO="xxx"
API_ID="yyy"
SERVER_LIST_ID="zzz"
SERVER_API_CONSUMER_KEY="aaa"
DEPLOY_S3_BUCKET = "bbb"
###
aws cloudformation package --template template.yml --s3-bucket ${DEPLOY_S3_BUCKET} --output-template template-export.yml
aws cloudformation deploy \
--template-file template-export.yml \
--stack-name lineworks-sample-stack \
--capabilities CAPABILITY_NAMED_IAM \
--parameter-overrides BotNo=${BOT_NO} ApiId=${API_ID} ServerListId=${SERVER_LIST_ID} ServerApiConsumerKey=${SERVER_API_CONSUMER_KEY}
I tried to create a reminder BOT in a serverless environment on AWS.
Next time, I'd like to introduce you to the implementation of Lambda functions. Implementation link
Recommended Posts