-
Notifications
You must be signed in to change notification settings - Fork 2
/
cdk-deploy.ts
74 lines (61 loc) · 2.63 KB
/
cdk-deploy.ts
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
import cdk = require('@aws-cdk/core');
import apigateway = require('@aws-cdk/aws-apigateway');
import lambda = require('@aws-cdk/aws-lambda');
import iam = require('@aws-cdk/aws-iam');
import certificate = require('@aws-cdk/aws-certificatemanager');
const sha256File = require('sha256-file');
const stackPrefix = process.env.STACK_NAME || 'GenericSFCCProxyServer';
const domainName: string = process.env.DOMAIN_NAME!;
const certARN: string = process.env.CERTIFICATE_ARN!;
export class SFCCProductSearchServerProxyStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const handler = new lambda.Function(this, stackPrefix + 'Handler', {
runtime: lambda.Runtime.NODEJS_10_X,
code: lambda.Code.fromAsset('dist.zip'),
description: `Generated on ${ new Date().toISOString() }`,
handler: 'src/lambda/index.handler',
role: new iam.Role(this, 'AllowLambdaServiceToAssumeRole', {
assumedBy: new iam.CompositePrincipal(
new iam.ServicePrincipal('lambda.amazonaws.com'),
new iam.ServicePrincipal('edgelambda.amazonaws.com'),
),
managedPolicies: [ iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole') ],
})
});
const sha = sha256File('./dist.zip');
handler.addVersion(sha);
const proxyApi = new apigateway.LambdaRestApi(this, 'sfcc-proxy-rest-api', {
domainName: {
domainName: domainName,
certificate: certificate.Certificate.fromCertificateArn(this, 'sfcc-proxy-certificate', certARN)
},
restApiName: 'sfcc-proxy-api',
description: 'Proxy server for deploying sfcc ui extension.',
handler: handler,
proxy: false
});
const productsById = proxyApi.root.addResource('products');
productsById.addMethod('GET');
addCorsOptions(productsById);
const productsSearch = proxyApi.root.addResource('product-search');
productsSearch.addMethod('POST');
addCorsOptions(productsSearch);
}
}
const app = new cdk.App();
console.log(`STACK_NAME: "${ stackPrefix }"`);
new SFCCProductSearchServerProxyStack(app, stackPrefix + 'Service');
app.synth();
export function addCorsOptions(apiResource: apigateway.IResource) {
const allowedHeaders = [ 'x-auth-id', 'x-auth-secret', 'Access-Control-Allow-Origin', 'endpoint' ];
allowedHeaders.push(...apigateway.Cors.DEFAULT_HEADERS,);
const allowedOrigins = [ 'null' ];
apiResource.addCorsPreflight({
statusCode: 200,
allowOrigins: allowedOrigins,
allowCredentials: false,
allowHeaders: allowedHeaders,
allowMethods: apigateway.Cors.ALL_METHODS,
});
}