Repository navigation
Expand file tree
/
Copy pathtemplate.yaml
More file actions
182 lines (177 loc) · 7.16 KB
/
Copy pathtemplate.yaml
File metadata and controls
182 lines (177 loc) · 7.16 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: FAQ assistant worker Lambda - HTTP endpoint with shared-secret auth.
Parameters:
OpenAIApiKey:
Type: String
NoEcho: true
Description: OpenAI API key, exposed to the function as OPENAI_API_KEY.
SharedSecret:
Type: String
NoEcho: true
Default: ""
Description: >-
Shared secret callers send in the x-faq-assistant-secret header.
Empty leaves the endpoint open (not recommended).
OpikApiKey:
Type: String
NoEcho: true
Default: ""
Description: Opik Cloud API key for trace export, exposed as OPIK_API_KEY. Empty keeps tracing inert.
GitHubRepo:
Type: String
Default: "DataTalksClub/faq-assistant"
Description: owner/repo allowed to assume the deploy role via GitHub OIDC.
Globals:
Function:
Timeout: 30
MemorySize: 256
Architectures:
- arm64
Resources:
FaqWorkerFunction:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: makefile
Properties:
FunctionName: faq-assistant
CodeUri: .
Handler: faq_assistant.handler.lambda_handler
Runtime: python3.14
Environment:
Variables:
OPENAI_API_KEY: !Ref OpenAIApiKey
FAQ_ASSISTANT_SHARED_SECRET: !Ref SharedSecret
SEARCH_INDEX_PATH: search-index.zsx
OPIK_ENABLED: "true"
OPIK_URL_OVERRIDE: https://www.comet.com/opik/api
OPIK_WORKSPACE: default
OPIK_PROJECT_NAME: faq-assistant-lambda
OPIK_API_KEY: !Ref OpikApiKey
FunctionUrlConfig:
AuthType: NONE
# Role GitHub Actions assumes (via OIDC) to deploy this stack. Bootstrap: the
# first deploy is done with other credentials; afterwards CI uses this role.
# The OIDC provider (token.actions.githubusercontent.com) must already exist.
GitHubDeployRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Federated: !Sub "arn:aws:iam::${AWS::AccountId}:oidc-provider/token.actions.githubusercontent.com"
Action: sts:AssumeRoleWithWebIdentity
Condition:
StringEquals:
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
StringLike:
"token.actions.githubusercontent.com:sub": !Sub "repo:${GitHubRepo}:*"
# Least privilege: exactly what `sam deploy` of this stack needs. No
# AdministratorAccess - this role is identical in dev and production.
Policies:
- PolicyName: faq-assistant-sam-deploy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: CloudFormationStack
Effect: Allow
Action:
- cloudformation:CreateStack
- cloudformation:UpdateStack
- cloudformation:DeleteStack
- cloudformation:DescribeStacks
- cloudformation:DescribeStackEvents
- cloudformation:DescribeStackResource
- cloudformation:DescribeStackResources
- cloudformation:GetTemplate
- cloudformation:ListStackResources
- cloudformation:CreateChangeSet
- cloudformation:DescribeChangeSet
- cloudformation:ExecuteChangeSet
- cloudformation:DeleteChangeSet
Resource:
- !Sub "arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/faq-assistant/*"
# SAM's --resolve-s3 manages this stack to provision the deploy bucket.
- !Sub "arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/aws-sam-cli-managed-default/*"
- Sid: CloudFormationServerlessTransform
Effect: Allow
Action: cloudformation:CreateChangeSet
Resource: !Sub "arn:aws:cloudformation:${AWS::Region}:aws:transform/Serverless-2016-10-31"
- Sid: CloudFormationTemplateInspection
Effect: Allow
Action:
- cloudformation:GetTemplateSummary
- cloudformation:ValidateTemplate
Resource: "*"
- Sid: SamDeployBucket
Effect: Allow
Action:
- s3:CreateBucket
- s3:GetBucketPolicy
- s3:PutBucketPolicy
- s3:PutBucketTagging
- s3:PutBucketVersioning
- s3:PutBucketPublicAccessBlock
- s3:PutEncryptionConfiguration
- s3:GetObject
- s3:PutObject
- s3:GetBucketLocation
- s3:ListBucket
Resource:
- arn:aws:s3:::aws-sam-cli-managed-default-*
- arn:aws:s3:::aws-sam-cli-managed-default-*/*
- Sid: LambdaFunction
Effect: Allow
Action:
- lambda:CreateFunction
- lambda:DeleteFunction
- lambda:GetFunction
- lambda:GetFunctionConfiguration
- lambda:UpdateFunctionCode
- lambda:UpdateFunctionConfiguration
- lambda:ListVersionsByFunction
- lambda:PublishVersion
- lambda:TagResource
- lambda:UntagResource
- lambda:ListTags
- lambda:AddPermission
- lambda:RemovePermission
- lambda:GetPolicy
- lambda:CreateFunctionUrlConfig
- lambda:UpdateFunctionUrlConfig
- lambda:GetFunctionUrlConfig
- lambda:DeleteFunctionUrlConfig
Resource: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:faq-assistant"
- Sid: ManageStackRoles
Effect: Allow
Action:
- iam:CreateRole
- iam:DeleteRole
- iam:GetRole
- iam:TagRole
- iam:UntagRole
- iam:AttachRolePolicy
- iam:DetachRolePolicy
- iam:PutRolePolicy
- iam:DeleteRolePolicy
- iam:GetRolePolicy
- iam:ListRolePolicies
- iam:ListAttachedRolePolicies
- iam:UpdateAssumeRolePolicy
Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/faq-assistant-*"
- Sid: PassFunctionRoleToLambda
Effect: Allow
Action: iam:PassRole
Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/faq-assistant-*"
Condition:
StringEquals:
iam:PassedToService: lambda.amazonaws.com
Outputs:
FunctionUrl:
Description: Public HTTPS endpoint. Auth is the x-faq-assistant-secret header.
Value: !GetAtt FaqWorkerFunctionUrl.FunctionUrl
DeployRoleArn:
Description: Set this as the AWS_DEPLOY_ROLE_ARN GitHub secret.
Value: !GetAtt GitHubDeployRole.Arn