@faceteer/cdk
Version:
CDK 2.0 constructs and helpers that make composing a Lambda powered service easier.
103 lines (102 loc) • 4.56 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceApiFunction = void 0;
const cdk = __importStar(require("aws-cdk-lib"));
const apigwv2 = __importStar(require("aws-cdk-lib/aws-apigatewayv2"));
const iam = __importStar(require("aws-cdk-lib/aws-iam"));
const base_function_1 = require("./base-function");
const route_to_alphanumeric_1 = require("../util/route-to-alphanumeric");
class ServiceApiFunction extends base_function_1.BaseFunction {
route;
integration;
constructor(scope, id, props) {
const { httpApi, authorizer, definition, defaultScopes = [], defaults, } = props;
super(scope, id, {
...props,
defaults: {
timeout: 30,
...defaults,
},
environment: {
DD_TAGS: `handler_type:api,handler_name:${definition.name}`,
...props.environment,
},
});
let authorizerType = 'NONE';
if (authorizer?.authorizerType === 'JWT') {
authorizerType = 'JWT';
}
else if (authorizer?.authorizerType === 'REQUEST') {
authorizerType = 'CUSTOM';
}
const apiGatewayServicePrincipal = new iam.ServicePrincipal('apigateway.amazonaws.com');
this.grantInvoke(apiGatewayServicePrincipal);
this.integration = new apigwv2.CfnIntegration(this, `Integration`, {
apiId: httpApi.ref,
description: definition.description,
integrationType: 'AWS_PROXY',
integrationUri: cdk.Fn.join('', [
'arn:',
cdk.Fn.ref('AWS::Partition'),
':apigateway:',
cdk.Fn.ref('AWS::Region'),
':lambda:path/2015-03-31/functions/',
this.functionArn,
'/invocations',
]),
integrationMethod: 'POST',
payloadFormatVersion: '2.0',
});
this.integration.overrideLogicalId(definition.cfnOverrides?.logicalIds?.integration ??
`ApiIntegration${definition.method}${(0, route_to_alphanumeric_1.routeToAlphaNumeric)(definition.route)}`);
if (definition.cfnOverrides?.logicalIds?.function) {
this.node.defaultChild.overrideLogicalId(definition.cfnOverrides?.logicalIds?.function);
}
this.route = new apigwv2.CfnRoute(this, `Route`, {
apiId: httpApi.ref,
routeKey: `${definition.method} ${definition.route}`,
target: cdk.Fn.join('/', ['integrations', this.integration.ref]),
authorizerId: definition.disableAuth ? undefined : authorizer?.ref,
authorizationType: definition.disableAuth ? 'NONE' : authorizerType,
authorizationScopes: definition.disableAuth || authorizerType !== 'JWT'
? undefined
: definition.scopes ?? defaults?.scopes ?? defaultScopes,
});
this.route.overrideLogicalId(definition.cfnOverrides?.logicalIds?.route ??
`ApiRoute${definition.method}${(0, route_to_alphanumeric_1.routeToAlphaNumeric)(definition.route)}`);
}
}
exports.ServiceApiFunction = ServiceApiFunction;