|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT-0 |
| 3 | + |
| 4 | +import { Construct, Duration, Stack } from "@aws-cdk/core"; |
| 5 | +import * as apigateway from "@aws-cdk/aws-apigateway"; |
| 6 | +import * as iam from "@aws-cdk/aws-iam"; |
| 7 | +import { AuthorizationType } from "@aws-cdk/aws-apigateway"; |
| 8 | +import * as lambda from "@aws-cdk/aws-lambda"; |
| 9 | +import { getLambdaPath } from "../utils/lambda"; |
| 10 | + |
| 11 | +export interface MeetingProviderProps { |
| 12 | + lambdaAssetDirectory: string; |
| 13 | + environment: { [key: string]: string }; |
| 14 | + policyStatements: iam.PolicyStatement[]; |
| 15 | +} |
| 16 | + |
| 17 | +export interface MeetingProviderApi { |
| 18 | + api: apigateway.RestApi; |
| 19 | + queryPath: string; |
| 20 | +} |
| 21 | + |
| 22 | +export default class MeetingProvider extends Construct { |
| 23 | + public readonly api: MeetingProviderApi; |
| 24 | + |
| 25 | + constructor(scope: Stack, id: string, props: MeetingProviderProps) { |
| 26 | + super(scope, id); |
| 27 | + |
| 28 | + const apiDefaults = { |
| 29 | + restApiName: `${id}Api`, |
| 30 | + description: `${id}Api`, |
| 31 | + defaultCorsPreflightOptions: { |
| 32 | + allowOrigins: apigateway.Cors.ALL_ORIGINS, |
| 33 | + allowMethods: apigateway.Cors.ALL_METHODS, |
| 34 | + }, |
| 35 | + policy: new iam.PolicyDocument({ |
| 36 | + statements: [ |
| 37 | + // Allow only callers with credentials from the AWS account |
| 38 | + // for this stage |
| 39 | + new iam.PolicyStatement({ |
| 40 | + effect: iam.Effect.ALLOW, |
| 41 | + principals: [new iam.AccountPrincipal(scope.account)], |
| 42 | + actions: ["execute-api:Invoke"], |
| 43 | + resources: ["execute-api:/*"], |
| 44 | + }), |
| 45 | + // Open up OPTIONS to allow browsers to make unauthenticated |
| 46 | + // preflight requests |
| 47 | + new iam.PolicyStatement({ |
| 48 | + effect: iam.Effect.ALLOW, |
| 49 | + principals: [new iam.AnyPrincipal()], |
| 50 | + actions: ["execute-api:Invoke"], |
| 51 | + resources: ["execute-api:/*/OPTIONS/*"], |
| 52 | + }), |
| 53 | + ], |
| 54 | + }), |
| 55 | + }; |
| 56 | + |
| 57 | + const api = new apigateway.RestApi(this, `${id}Api`, apiDefaults); |
| 58 | + |
| 59 | + const lambdaFn = new lambda.Function(scope, `${id}-Handler`, { |
| 60 | + runtime: lambda.Runtime.NODEJS_12_X, |
| 61 | + code: lambda.Code.fromAsset(getLambdaPath(props.lambdaAssetDirectory)), |
| 62 | + handler: `index.chimeCallHandler`, |
| 63 | + timeout: Duration.seconds(30), |
| 64 | + environment: props.environment, |
| 65 | + initialPolicy: props.policyStatements, |
| 66 | + }); |
| 67 | + |
| 68 | + const apiPath = "call-create"; |
| 69 | + const apiResource = api.root.addResource(apiPath); |
| 70 | + |
| 71 | + const lambdaIntegration = new apigateway.LambdaIntegration(lambdaFn); |
| 72 | + |
| 73 | + apiResource.addMethod("GET", lambdaIntegration, { |
| 74 | + authorizationType: AuthorizationType.IAM, |
| 75 | + }); |
| 76 | + |
| 77 | + this.api = { |
| 78 | + api, |
| 79 | + queryPath: `/${apiPath}`, |
| 80 | + }; |
| 81 | + } |
| 82 | +} |
0 commit comments