-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudformation_template.yaml
163 lines (154 loc) · 4.81 KB
/
cloudformation_template.yaml
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
BotName:
Type: String
Default: chatgpt-bot
FunctionHandler:
Type: String
Default: 'com.anderb.chatbot.BotApplication::handleRequest'
OpenAIApiKey:
Type: String
NoEcho: true
AllowedUsers:
Type: String
Description: Comma separated whitelisted telegram user ids
BotToken:
Type: String
NoEcho: true
Description: 'Example: 1234567890:xxxxxxxxx'
BotUsername:
Type: String
Description: Must start with @
S3Bucket:
Type: String
Description: S3 bucket with function archive
S3Key:
Type: String
Description: Function archive name
Resources:
ChatHistoryTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub '${BotName}-history'
AttributeDefinitions:
- AttributeName: chat_id
AttributeType: N
KeySchema:
- AttributeName: chat_id
KeyType: HASH
BillingMode: PAY_PER_REQUEST
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: ChatBotCloudWatchDynamoDBPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
- Effect: Allow
Action:
- dynamodb:PutItem
- dynamodb:GetItem
Resource: !Sub 'arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${BotName}-history'
ChatGPTBotFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub '${BotName}-function'
Description: 'Lambda backend for Telegram Bot'
Runtime: java11
Handler: !Ref FunctionHandler
Code:
S3Bucket: !Ref S3Bucket
S3Key: !Ref S3Key
Role: !GetAtt LambdaExecutionRole.Arn
Timeout: 300
MemorySize: 512
Environment:
Variables:
AI_MODEL: gpt-3.5-turbo
OPENAI_API_KEY: !Ref OpenAIApiKey
OPENAI_API_URL: https://api.openai.com/v1/chat/completions
ALLOWED_USERS: !Ref AllowedUsers
BOT_TOKEN: !Ref BotToken
BOT_URL: !Sub 'https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/prod/${BotName}'
BOT_USERNAME: !Ref BotUsername
JAVA_TOOL_OPTIONS: -Dorg.slf4j.simpleLogger.defaultLogLevel=debug
DYNAMO_TABLE_NAME: !Sub '${BotName}-history'
HISTORY_LENGTH: 8
SESSION_MAX_LIFETIME: 120
Architectures:
- arm64
MyApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: !Sub '${BotName}-api'
Description: !Sub 'API Gateway for ${BotName}'
EndpointConfiguration:
Types:
- REGIONAL
MyFunctionResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref MyApi
ParentId: !GetAtt MyApi.RootResourceId
PathPart: !Ref BotName
MyFunctionMethod:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref MyApi
ResourceId: !Ref MyFunctionResource
HttpMethod: POST
AuthorizationType: NONE
Integration:
Type: AWS
IntegrationHttpMethod: POST
Uri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ChatGPTBotFunction.Arn}/invocations'
PassthroughBehavior: WHEN_NO_MATCH
RequestParameters:
integration.request.header.X-Amz-Invocation-Type: "'Event'"
IntegrationResponses:
- StatusCode: 200
ResponseTemplates:
application/json: ''
ContentHandling: CONVERT_TO_TEXT
MethodResponses:
- StatusCode: 200
ApiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn: MyFunctionMethod
Properties:
RestApiId: !Ref MyApi
StageName: prod
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref ChatGPTBotFunction
Action: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
SourceArn: !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${MyApi}/*/POST/${BotName}'
Outputs:
TableNameOutput:
Description: DynamoDB table name
Value: !Sub '${BotName}-history'
FunctionNameOutput:
Description: Lambda Function name
Value: !Sub '${BotName}-function'
RestApiNameOutput:
Description: ApiGateway RestApi name
Value: !Sub '${BotName}-api'
ApiEndpointUrl:
Description: The URL of the API endpoint
Value: !Sub 'https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/prod/${BotName}'