@mstuercke/pulumi-modules 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,219 @@
1
+ import {type CustomResourceOptions, type Input, type Output, Resource} from '@pulumi/pulumi'
2
+ import {
3
+ Api,
4
+ ApiMapping,
5
+ Deployment,
6
+ DomainName,
7
+ Integration,
8
+ IntegrationResponse,
9
+ Route,
10
+ Stage,
11
+ } from '@pulumi/aws/apigatewayv2'
12
+ import {Policy, Role} from '@pulumi/aws/iam'
13
+ import {Record} from '@pulumi/aws/route53'
14
+ import type {NodeLambdaFunction} from '../lambda'
15
+
16
+ export interface WebsocketApiArgs {
17
+ name: string
18
+ lambdaFunction: NodeLambdaFunction
19
+ routeThrottling?: {
20
+ rateLimit?: number
21
+ burstLimit?: number
22
+ }
23
+ stageName?: string
24
+ domain: {
25
+ zoneId: Input<string>
26
+ fullName: Input<string>
27
+ euCentral1CertificateArn: Input<string>
28
+ }
29
+ projectId: string
30
+ }
31
+
32
+ export class WebsocketApi extends Resource {
33
+ id: Output<string>
34
+ url: string
35
+
36
+ constructor(id: string, args: WebsocketApiArgs, opts?: CustomResourceOptions) {
37
+ super('mstuercke:websocket:WebsocketApi', id, false, undefined, opts)
38
+
39
+ const {name, lambdaFunction, routeThrottling, stageName = 'v1', domain, projectId} = args
40
+
41
+ const api = new Api(
42
+ 'api',
43
+ {
44
+ name,
45
+ protocolType: 'WEBSOCKET',
46
+ routeSelectionExpression: '\\$default',
47
+ tags: {
48
+ project: projectId,
49
+ },
50
+ },
51
+ {parent: this},
52
+ )
53
+
54
+ const executeLambdaPolicy = new Policy(
55
+ 'execute-lambda',
56
+ {
57
+ name: `${name}-execute-lambda`,
58
+ path: '/',
59
+ policy: {
60
+ Version: '2012-10-17',
61
+ Statement: [
62
+ {
63
+ Action: 'lambda:InvokeFunction',
64
+ Effect: 'Allow',
65
+ Resource: lambdaFunction.arn,
66
+ },
67
+ ],
68
+ },
69
+ tags: {
70
+ project: projectId,
71
+ },
72
+ },
73
+ {parent: api},
74
+ )
75
+
76
+ const wsApiRole = new Role(
77
+ 'role',
78
+ {
79
+ name: `${name}-role`,
80
+ assumeRolePolicy: JSON.stringify({
81
+ Version: '2012-10-17',
82
+ Statement: [
83
+ {
84
+ Effect: 'Allow',
85
+ Action: 'sts:AssumeRole',
86
+ Principal: {Service: 'apigateway.amazonaws.com'},
87
+ },
88
+ ],
89
+ }),
90
+ tags: {
91
+ project: projectId,
92
+ },
93
+ managedPolicyArns: [executeLambdaPolicy.arn],
94
+ },
95
+ {parent: api},
96
+ )
97
+
98
+ const wsLambdaIntegration = new Integration(
99
+ 'lambda-integration',
100
+ {
101
+ apiId: api.id,
102
+ integrationType: 'AWS_PROXY',
103
+ integrationUri: lambdaFunction.invokeArn,
104
+ credentialsArn: wsApiRole.arn,
105
+ contentHandlingStrategy: 'CONVERT_TO_TEXT',
106
+ passthroughBehavior: 'WHEN_NO_MATCH',
107
+ },
108
+ {parent: this},
109
+ )
110
+ new IntegrationResponse(
111
+ 'lambda-integration-response',
112
+ {
113
+ apiId: api.id,
114
+ integrationId: wsLambdaIntegration.id,
115
+ integrationResponseKey: '/200/',
116
+ },
117
+ {parent: wsLambdaIntegration},
118
+ )
119
+
120
+ const routes: Route[] = []
121
+ const routeKeys = ['$connect', '$default', '$disconnect']
122
+ for (const routeKey of routeKeys) {
123
+ const route = new Route(
124
+ `${routeKey}-route`,
125
+ {
126
+ apiId: api.id,
127
+ routeKey: routeKey,
128
+ target: wsLambdaIntegration.id.apply((id) => `integrations/${id}`),
129
+ authorizationType: 'NONE',
130
+ },
131
+ {parent: wsLambdaIntegration},
132
+ )
133
+ routes.push(route)
134
+ }
135
+
136
+ const deployment = new Deployment(
137
+ 'deployment',
138
+ {
139
+ apiId: api.id,
140
+ },
141
+ {
142
+ dependsOn: [wsLambdaIntegration, ...routes],
143
+ parent: api,
144
+ },
145
+ )
146
+
147
+ const stage = new Stage(
148
+ 'stage',
149
+ {
150
+ apiId: api.id,
151
+ name: stageName,
152
+ deploymentId: deployment.id,
153
+ defaultRouteSettings: {
154
+ throttlingRateLimit: routeThrottling?.rateLimit ?? 100,
155
+ throttlingBurstLimit: routeThrottling?.burstLimit ?? 100,
156
+ },
157
+ tags: {
158
+ project: projectId,
159
+ },
160
+ },
161
+ {parent: deployment},
162
+ )
163
+
164
+ const domainName = new DomainName(
165
+ 'domain',
166
+ {
167
+ domainName: domain.fullName,
168
+ domainNameConfiguration: {
169
+ certificateArn: domain.euCentral1CertificateArn,
170
+ endpointType: 'REGIONAL',
171
+ securityPolicy: 'TLS_1_2',
172
+ },
173
+ },
174
+ {parent: api},
175
+ )
176
+
177
+ new Record(
178
+ 'redirect',
179
+ {
180
+ zoneId: domain.zoneId,
181
+ name: domain.fullName,
182
+ type: 'A',
183
+ aliases: [
184
+ {
185
+ zoneId: domainName.domainNameConfiguration.hostedZoneId,
186
+ name: domainName.domainNameConfiguration.targetDomainName,
187
+ evaluateTargetHealth: true,
188
+ },
189
+ ],
190
+ allowOverwrite: true,
191
+ },
192
+ {parent: api},
193
+ )
194
+
195
+ new ApiMapping(
196
+ 'root',
197
+ {
198
+ apiId: api.id,
199
+ domainName: domainName.id,
200
+ stage: stage.name,
201
+ },
202
+ {parent: api},
203
+ )
204
+
205
+ new ApiMapping(
206
+ 'stage',
207
+ {
208
+ apiId: api.id,
209
+ domainName: domainName.id,
210
+ stage: stage.name,
211
+ apiMappingKey: stage.name,
212
+ },
213
+ {parent: api},
214
+ )
215
+
216
+ this.id = api.id
217
+ this.url = `wss://${domain.fullName}/`
218
+ }
219
+ }
@@ -0,0 +1,7 @@
1
+ import {WebsocketApi} from './WebsocketApi'
2
+
3
+ export * from './WebsocketApi'
4
+
5
+ export const websocket = {
6
+ WebsocketApi,
7
+ }