-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yaml
More file actions
147 lines (128 loc) · 4.88 KB
/
template.yaml
File metadata and controls
147 lines (128 loc) · 4.88 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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
How to organize, test, log and trace an AWS SAM application written in Python
Parameters:
Environment:
Default: staging
Type: String
PostsBucketName:
Default: posts
Type: String
PostsMetaTableName:
Default: posts-meta
Type: String
Project:
Default: aws-sam-application-template-python
Type: String
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Runtime: python3.9
Tags:
Environment: !Ref Environment
Project: !Ref Project
Timeout: 3
Tracing: Active # X-Ray tracing mode: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-tracing
Resources:
#region --------------------------------------------------------------- Layers
#
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-layers.html#building-applications-examples
LayerRequests:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub "${Project}-${Environment}-layer-requests"
ContentUri: layers/layer_requests/
CompatibleRuntimes:
- python3.9
Metadata:
BuildMethod: python3.9 # Required to have AWS SAM build this layer
LayerXRay:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub "${Project}-${Environment}-layer-x-ray"
ContentUri: layers/layer_x_ray/
CompatibleRuntimes:
- python3.9
Metadata:
BuildMethod: python3.9 # Required to have AWS SAM build this layer
#endregion -------------------------------------------------------------------
#region ---------------------------------------------------------- API Gateway
RestApiGateway:
Type: AWS::Serverless::Api
Properties:
Cors: "'*'"
GatewayResponses:
DEFAULT_4XX:
ResponseParameters:
Headers:
Access-Control-Allow-Origin: "'*'"
Access-Control-Allow-Headers: "'*'"
Access-Control-Allow-Methods: "'*'"
DEFAULT_5XX:
ResponseParameters:
Headers:
Access-Control-Allow-Origin: "'*'"
Access-Control-Allow-Headers: "'*'"
Access-Control-Allow-Methods: "'*'"
MinimumCompressionSize: 0 # compression enabled
StageName: !Sub "${Environment}"
#endregion -------------------------------------------------------------------
#region -------------------------------------------------------------- Lambdas
PostsGetFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: functions/posts_get/
Environment:
Variables:
BUCKET_POSTS: !Sub "${Project}-${Environment}-${PostsBucketName}"
TABLE_POSTS: !Sub "${Project}-${Environment}-${PostsMetaTableName}"
FunctionName: !Sub "${Project}-${Environment}-posts-get"
Handler: handler.posts_get
Layers:
- !Ref LayerRequests
- !Ref LayerXRay
Events:
ApiEvent:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /posts
Method: get
RestApiId:
Ref: RestApiGateway
Policies:
- DynamoDBReadPolicy:
TableName:
!Sub "${Project}-${Environment}-${PostsMetaTableName}"
- S3ReadPolicy:
BucketName:
!Sub "${Project}-${Environment}-${PostsBucketName}"
#endregion -------------------------------------------------------------------
#region ----------------------------------------------------------- S3 Buckets
PostsBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "${Project}-${Environment}-${PostsBucketName}"
#endregion -------------------------------------------------------------------
#region ------------------------------------------------------ DynamoDB Tables
PostsMetaTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "${Project}-${Environment}-${PostsMetaTableName}"
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
BillingMode: PAY_PER_REQUEST
# TimeToLiveSpecification:
# AttributeName: ttl
# Enabled: true
#endregion -------------------------------------------------------------------
Outputs:
ApiEndpoint:
Description: "Posts API"
Value: !Sub "https://${RestApiGateway}.execute-api.${AWS::Region}.amazonaws.com/${Environment}/"
RestApiId:
Value: !Ref RestApiGateway