-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathringalarm-gateway.yaml
170 lines (156 loc) · 4.87 KB
/
ringalarm-gateway.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
164
165
166
167
168
169
170
AWSTemplateFormatVersion: '2010-09-09'
Description: AWS API Gateway with a Lambda Integration
Parameters:
lambdaFunctionName:
Type: "String"
AllowedPattern: "^[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+$"
Description: Ring Alarm Lambda function name. (Recommend to keep default)
Default: "st-ring-alarm-f8"
apiStageName:
Type: "String"
Description: Ring API Staging Name. (Recommend to keep default)
Default: "v1"
s3BucketName:
Type: "String"
Description: Amazon S3 bucket name with the deployment.zip file.
Default: "st-ring-alarm"
Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
ApiKeySourceType: HEADER
Description: An API Gateway for Ring Alarm APIs
EndpointConfiguration:
Types:
- REGIONAL
Name: !Join ["", [{"Ref": "AWS::StackName"}, "-api"]]
ProxyResource:
Type: 'AWS::ApiGateway::Resource'
Properties:
RestApiId: !Ref ApiGatewayRestApi
ParentId: !GetAtt ApiGatewayRestApi.RootResourceId
PathPart: '{ring-action+}'
ProxyResourceANY:
Type: 'AWS::ApiGateway::Method'
Properties:
RestApiId: !Ref ApiGatewayRestApi
ResourceId: !Ref ProxyResource
HttpMethod: ANY
ApiKeyRequired: true
AuthorizationType: NONE
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations'
ApiGatewayModel:
Type: AWS::ApiGateway::Model
Properties:
ContentType: 'application/json'
RestApiId: !Ref ApiGatewayRestApi
Schema: {}
ApiGatewayStage:
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId: !Ref ApiGatewayDeployment
Description: Lambda API Stage v1
RestApiId: !Ref ApiGatewayRestApi
StageName: !Ref "apiStageName"
ApiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn: ProxyResourceANY
Properties:
Description: Lambda API Deployment
RestApiId: !Ref ApiGatewayRestApi
ApiGatewayIamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: ''
Effect: 'Allow'
Principal:
Service:
- 'apigateway.amazonaws.com'
Action:
- 'sts:AssumeRole'
Path: '/'
Policies:
- PolicyName: LambdaAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action: 'lambda:*'
Resource: !GetAtt LambdaFunction.Arn
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket: !Ref s3BucketName
S3Key: 'deployment.zip'
Description: Ring API Lambda function
FunctionName: !Ref "lambdaFunctionName"
Handler: main
MemorySize: 512
Role: !GetAtt LambdaIamRole.Arn
Runtime: go1.x
Timeout: 60
LambdaIamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Service:
- 'lambda.amazonaws.com'
Action:
- 'sts:AssumeRole'
Path: '/'
Policies:
- PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Effect: "Allow"
Resource:
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${lambdaFunctionName}:*"
PolicyName: !Join ["", [{"Ref": "AWS::StackName"}, "-lambda-log"]]
LambdaPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt LambdaFunction.Arn
Action: lambda:InvokeFunction
Principal: 'apigateway.amazonaws.com'
LambdaLogGroup:
Type: "AWS::Logs::LogGroup"
Properties:
LogGroupName: !Sub "/aws/lambda/${lambdaFunctionName}"
RetentionInDays: 3
ApiKey:
Type: AWS::ApiGateway::ApiKey
Properties:
Name: !Join ["", [{"Ref": "AWS::StackName"}, "-apikey"]]
Description: !Join ["", [{"Ref": "AWS::StackName"}, "api key"]]
Enabled: true
GenerateDistinctId: false
ApiUsagePlan:
Type: "AWS::ApiGateway::UsagePlan"
DependsOn: ApiGatewayStage
Properties:
ApiStages:
- ApiId: !Ref ApiGatewayRestApi
Stage: !Ref "apiStageName"
Description: !Join [" ", [{"Ref": "AWS::StackName"}, "usage plan"]]
UsagePlanName: !Join ["", [{"Ref": "AWS::StackName"}, "-usage-plan"]]
ApiUsagePlanKey:
Type: "AWS::ApiGateway::UsagePlanKey"
Properties:
KeyId: !Ref ApiKey
KeyType: API_KEY
UsagePlanId: !Ref ApiUsagePlan