-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtemplate.yml
146 lines (133 loc) · 5.53 KB
/
template.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
# This is the SAM template that represents the architecture of your serverless application
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html
# The AWSTemplateFormatVersion identifies the capabilities of the template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/format-version-structure.html
AWSTemplateFormatVersion: 2010-09-09
Description: >-
aws-sam-typescript-layers-example
# Transform section specifies one or more macros that AWS CloudFormation uses to process your template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html
Transform:
- AWS::Serverless-2016-10-31
Globals:
Function:
Layers:
- !Ref RuntimeDependenciesLayer
Environment:
Variables:
SAMPLE_TABLE: !Ref SampleTable
ITEM_QUEUE: !Ref WriteQueue
Runtime: nodejs18.x
MemorySize: 128
Timeout: 100
# Resources declares the AWS resources that you want to include in the stack
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
Resources:
# Each Lambda function is defined by properties:
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
# This is a Lambda function config associated with the source code: get-all-items.js
getAllItemsFunction:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: makefile
Properties:
Handler: dist/handlers/get-all-items.getAllItemsHandler
Description: A simple example includes a HTTP get method to get all items from a DynamoDB table.
Policies:
# Give Create/Read/Update/Delete Permissions to the SampleTable
- DynamoDBCrudPolicy:
TableName: !Ref SampleTable
Events:
Api:
Type: Api
Properties:
Path: /
Method: GET
# Each Lambda function is defined by properties:
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
# This is a Lambda function config associated with the source code: get-by-id.js
getByIdFunction:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: makefile
Properties:
Handler: dist/handlers/get-by-id.getByIdHandler
Description: A simple example includes a HTTP get method to get one item by id from a DynamoDB table.
Policies:
# Give Create/Read/Update/Delete Permissions to the SampleTable
- DynamoDBCrudPolicy:
TableName: !Ref SampleTable
Events:
Api:
Type: Api
Properties:
Path: /{id}
Method: GET
# Each Lambda function is defined by properties:
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
# This is a Lambda function config associated with the source code: put-item.js
putItemFunction:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: makefile
Properties:
Handler: dist/handlers/put-item.putItemHandler
Description: A simple example includes a HTTP post method to add one item to an SQS queue (to be written to a DynamoDB table later).
Policies:
# Give permission to send message to an Amazon SQS queue. See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-template-list.html#sqs-send-message-policy
- SQSSendMessagePolicy:
QueueName: !GetAtt WriteQueue.QueueName
Events:
Api:
Type: Api
Properties:
Path: /
Method: POST
# This is a Lambda function config associated with the source code: write-item.ts
writeItemFunction:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: makefile
Properties:
Handler: dist/handlers/write-item.writeItemHandler
Description: A simple example includes an SQS subscription to write queued object to DynamoDB
Timeout: 25 # Chosen to be less than the default SQS Visibility Timeout of 30 seconds
Policies:
# Give Create/Read/Update/Delete Permissions to the SampleTable, see https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-template-list.html#dynamo-db-crud-policy
- DynamoDBCrudPolicy:
TableName: !Ref SampleTable
Events:
# Subscription to primary SQS queue
SQSQueueEvent:
Type: SQS
Properties:
Queue: !GetAtt WriteQueue.Arn
BatchSize: 1
# Simple syntax to create a DynamoDB table with a single attribute primary key, more in
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlesssimpletable
# DynamoDB table to store item: {id: <ID>, name: <NAME>}
SampleTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 2
WriteCapacityUnits: 2
WriteQueue:
Type: AWS::SQS::Queue
RuntimeDependenciesLayer:
Type: AWS::Serverless::LayerVersion
Metadata:
BuildMethod: makefile
Properties:
Description: Runtime dependencies for Lambdas
ContentUri: ./
CompatibleRuntimes:
- nodejs18.x
RetentionPolicy: Retain
Outputs:
WebEndpoint:
Description: "API Gateway endpoint URL for Prod stage"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"