-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
153 lines (139 loc) · 4.44 KB
/
serverless.yml
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
service: metadocs
provider:
name: aws
runtime: python3.11
region: eu-west-3
iamRoleStatements:
- Effect: Allow
Action:
- rds-data:ExecuteStatement
- rds-data:BatchExecuteStatement
Resource:
- arn:aws:rds:${self:provider.region}:*:db:${self:provider.stage}-postgres
plugins:
- serverless-python-requirements
- serverless-dotenv-plugin
custom:
dotenv:
path: .env
pythonRequirements:
dockerizePip: true
usePoetry: true
frontendBucketName: ${self:provider.stage}-metadocs-frontend
domainName: ${self:custom.domainNames.${self:provider.stage}, '${self:provider.stage}.app.metadocs.co'}
domainNames:
prod: app.metadocs.co
cloudFrontDistributionIds:
dev: "E3CIV0VN5IVHON"
prod: "YOUR_PROD_CLOUDFRONT_DISTRIBUTION_ID"
package:
exclude:
- frontend/**
- node_modules/**
- .git/**
- .vscode/**
- tests/**
- README.md
- .env
include:
- backend/**
functions:
backend:
handler: backend/src/main.handler
environment:
JWT_SECRET: ${env:JWT_SECRET}
BUCKET_NAME: ${self:custom.frontendBucketName}
RDS_HOSTNAME: !GetAtt MyPostgresDB.Endpoint.Address
RDS_PORT: '5432'
RDS_USERNAME: admin
RDS_PASSWORD: ${env:RDS_PASSWORD}
RDS_DB_NAME: mydatabase
resources:
Resources:
BackendLambdaPermission:
Type: AWS::Lambda::Url
Properties:
TargetFunctionArn: !GetAtt BackendLambdaFunction.Arn
AuthType: NONE # No authentication (public URL)
S3BucketFrontend:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.frontendBucketName}
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
CloudFrontOriginAccessControl:
Type: AWS::CloudFront::OriginAccessControl
Properties:
OriginAccessControlConfig:
Name: "${self:provider.stage}-metadocs-OAC"
OriginAccessControlOriginType: s3
SigningBehavior: always
SigningProtocol: sigv4
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: !GetAtt S3BucketFrontend.DomainName
Id: S3Origin
OriginAccessControlId: !Ref CloudFrontOriginAccessControl # Correct property for OAC
S3OriginConfig: {} # Keep this empty when using OAC
Enabled: true
DefaultRootObject: index.html
DefaultCacheBehavior:
TargetOriginId: S3Origin
ViewerProtocolPolicy: redirect-to-https
ForwardedValues:
QueryString: false
Cookies:
Forward: none
ViewerCertificate:
AcmCertificateArn: arn:aws:acm:us-east-1:514170698941:certificate/62e81c17-bebd-414e-bc9a-9e88828bd697
SslSupportMethod: sni-only
Aliases:
- ${self:custom.domainName}
# Attach the bucket policy to restrict access to CloudFront only
BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref S3BucketFrontend
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: cloudfront.amazonaws.com
Action: "s3:GetObject"
Resource: "arn:aws:s3:::${self:custom.frontendBucketName}/*"
Condition:
StringEquals:
AWS:SourceArn: "arn:aws:cloudfront::${AWS::AccountId}:distribution/${self:custom.cloudFrontDistributionIds.${self:provider.stage}}"
# RDS PostgreSQL Instance
Postgres:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceIdentifier: Postgres
DBName: db
AllocatedStorage: "50"
DBInstanceClass: db.t4g.small
Engine: postgres
EngineVersion: "16.4"
MasterUsername: admin
MasterUserPassword: ${env:RDS_PASSWORD}
VPCSecurityGroups:
- sg-xxxxxxxx
PubliclyAccessible: false
BackupRetentionPeriod: 7
MultiAZ: false
StorageType: gp2
Outputs:
S3BucketFrontend:
Value: !Ref S3BucketFrontend
Export:
Name: ${self:service}-${self:provider.stage}-frontend-bucket
PostgresDBEndpoint:
Value: !GetAtt MyPostgresDB.Endpoint.Address
Export:
Name: ${self:service}-${self:provider.stage}-postgres-endpoint