@webiny/pulumi-aws 0.0.0-mt-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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # @webiny/pulumi-aws
2
+
3
+ [![](https://img.shields.io/npm/dw/@webiny/pulumi-aws.svg)](https://www.npmjs.com/package/@webiny/pulumi-aws)
4
+ [![](https://img.shields.io/npm/v/@webiny/pulumi-aws.svg)](https://www.npmjs.com/package/@webiny/pulumi-aws)
5
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
6
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
7
+
8
+ A set of custom Pulumi components for Webiny.
9
+
10
+ ### Example
11
+
12
+ ```ts
13
+ import { WebsiteTenantRouter } from "@webiny/pulumi-aws";
14
+
15
+ const router = new WebsiteTenantRouter("tenant-router");
16
+ ```
@@ -0,0 +1,105 @@
1
+ import { readFileSync } from "fs";
2
+ import * as pulumi from "@pulumi/pulumi";
3
+ import * as aws from "@pulumi/aws";
4
+ import { getStackOutput } from "@webiny/cli-plugin-deploy-pulumi/utils";
5
+
6
+ interface Config {
7
+ apiFolder?: string;
8
+ }
9
+
10
+ interface Params {
11
+ region: string;
12
+ dynamoDbTable: string;
13
+ }
14
+
15
+ function createFunctionArchive({ dynamoDbTable, region }: Params) {
16
+ const handler = readFileSync(__dirname + "/functions/origin/request.js", "utf-8");
17
+
18
+ const source = handler
19
+ .replace("{DB_TABLE_NAME}", dynamoDbTable)
20
+ .replace("{DB_TABLE_REGION}", region);
21
+
22
+ return new pulumi.asset.AssetArchive({
23
+ "index.js": new pulumi.asset.StringAsset(source)
24
+ });
25
+ }
26
+
27
+ export class WebsiteTenantRouter extends pulumi.ComponentResource {
28
+ public readonly originRequest: aws.lambda.Function;
29
+
30
+ constructor(name: string, config: Config = {}, opts = {}) {
31
+ super("webiny:aws:WebsiteTenantRouter", name, {}, opts);
32
+
33
+ const { region, dynamoDbTable } = getStackOutput({
34
+ folder: config.apiFolder || "api",
35
+ env: String(process.env.WEBINY_ENV)
36
+ });
37
+
38
+ const callerIdentity = pulumi.output(aws.getCallerIdentity({}));
39
+
40
+ const tenantRouterPolicy = new aws.iam.Policy(`${name}-policy`, {
41
+ name: `${name}-policy`,
42
+ policy: {
43
+ Version: "2012-10-17",
44
+ Statement: [
45
+ {
46
+ Sid: "PermissionForDynamodb",
47
+ Effect: "Allow",
48
+ Action: ["dynamodb:GetItem"],
49
+ Resource: [
50
+ pulumi.interpolate`arn:aws:dynamodb:${region}:${callerIdentity.accountId}:table/${dynamoDbTable}`,
51
+ pulumi.interpolate`arn:aws:dynamodb:${region}:${callerIdentity.accountId}:table/${dynamoDbTable}/*`
52
+ ]
53
+ }
54
+ ]
55
+ }
56
+ });
57
+
58
+ const role = new aws.iam.Role(
59
+ `${name}-role`,
60
+ {
61
+ managedPolicyArns: [aws.iam.ManagedPolicies.AWSLambdaBasicExecutionRole],
62
+ assumeRolePolicy: {
63
+ Version: "2012-10-17",
64
+ Statement: [
65
+ {
66
+ Action: "sts:AssumeRole",
67
+ Principal: aws.iam.Principals.LambdaPrincipal,
68
+ Effect: "Allow"
69
+ },
70
+ {
71
+ Action: "sts:AssumeRole",
72
+ Principal: aws.iam.Principals.EdgeLambdaPrincipal,
73
+ Effect: "Allow"
74
+ }
75
+ ]
76
+ }
77
+ },
78
+ { parent: this }
79
+ );
80
+
81
+ new aws.iam.RolePolicyAttachment(`${name}-dynamodb`, {
82
+ role,
83
+ policyArn: tenantRouterPolicy.arn
84
+ });
85
+
86
+ // Some resources _must_ be put in us-east-1, such as Lambda at Edge.
87
+ const awsUsEast1 = new aws.Provider("us-east-1", { region: "us-east-1" });
88
+
89
+ this.originRequest = new aws.lambda.Function(
90
+ `${name}-origin-request`,
91
+ {
92
+ publish: true,
93
+ runtime: "nodejs14.x",
94
+ handler: "index.handler",
95
+ role: role.arn,
96
+ timeout: 5,
97
+ memorySize: 128,
98
+ code: createFunctionArchive({ region, dynamoDbTable })
99
+ },
100
+ { provider: awsUsEast1, parent: this }
101
+ );
102
+
103
+ this.registerOutputs();
104
+ }
105
+ }
@@ -0,0 +1,40 @@
1
+ const { DocumentClient } = require("aws-sdk/clients/dynamodb");
2
+
3
+ const DB_TABLE_NAME = "{DB_TABLE_NAME}";
4
+ const DB_TABLE_REGION = "{DB_TABLE_REGION}";
5
+
6
+ const documentClient = new DocumentClient({
7
+ convertEmptyValues: true,
8
+ region: DB_TABLE_REGION
9
+ });
10
+
11
+ exports.handler = async event => {
12
+ const request = event.Records[0].cf.request;
13
+ // console.log(JSON.stringify(event.Records[0].cf, null, 2));
14
+
15
+ const requestedDomain = request.headers.host[0].value;
16
+ const originDomain = request.origin.custom.domainName;
17
+
18
+ // Find tenant by domain
19
+ const params = {
20
+ TableName: DB_TABLE_NAME,
21
+ Key: {
22
+ PK: `DOMAIN#${requestedDomain}`,
23
+ SK: "A"
24
+ }
25
+ };
26
+
27
+ const { Item } = await documentClient.get(params).promise();
28
+
29
+ if (Item) {
30
+ const from = request.uri;
31
+ request.uri = `/${Item.tenant}${from}`;
32
+ console.log(`Rewriting request from "${from}" to "${request.uri}"`);
33
+ } else {
34
+ console.log(`Failed to find a tenant for domain "${requestedDomain}"`);
35
+ }
36
+ // Restore the Host header
37
+ request.headers.host[0].value = originDomain;
38
+
39
+ return request;
40
+ };
package/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { WebsiteTenantRouter } from "./components/tenantRouter/WebsiteTenantRouter";
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@webiny/pulumi-aws",
3
+ "version": "0.0.0-mt-1",
4
+ "main": "index.ts",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/webiny/webiny-js.git"
8
+ },
9
+ "description": "A set of custom Pulumi components for Webiny, to use with AWS.",
10
+ "author": "Webiny Ltd",
11
+ "license": "MIT",
12
+ "publishConfig": {
13
+ "access": "public",
14
+ "directory": "."
15
+ },
16
+ "files": [
17
+ "index.ts",
18
+ "components"
19
+ ],
20
+ "dependencies": {
21
+ "@pulumi/aws": "^4.10.0",
22
+ "@pulumi/pulumi": "< 3.18.0",
23
+ "@webiny/cli-plugin-deploy-pulumi": "0.0.0-mt-1"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^10.0.0"
27
+ },
28
+ "adio": {
29
+ "ignore": {
30
+ "dependencies": [
31
+ "@pulumi/pulumi",
32
+ "@pulumi/aws"
33
+ ]
34
+ }
35
+ },
36
+ "gitHead": "37736d8456a6ecb342a6c3645060bd9a3f2d4bb0"
37
+ }