Skip to content

Commit

Permalink
Changed API functions to return response objects
Browse files Browse the repository at this point in the history
  • Loading branch information
biffgaut committed Feb 14, 2023
1 parent 8a6cc12 commit b5b8398
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export class ApiGatewayToLambda extends Construct {
});

// Setup the API Gateway
[this.apiGateway, this.apiGatewayCloudWatchRole,
this.apiGatewayLogGroup] = defaults.GlobalLambdaRestApi(this, this.lambdaFunction, props.apiGatewayProps, props.logGroupProps);
const restApi = defaults.GlobalLambdaRestApi(this, this.lambdaFunction, props.apiGatewayProps, props.logGroupProps);
this.apiGateway = restApi.api;
this.apiGatewayCloudWatchRole = restApi.role;
this.apiGatewayLogGroup = restApi.group;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,19 @@ function configureCloudwatchRoleForApi(scope: Construct, _api: api.RestApi): iam
return restApiCloudwatchRole;
}

interface ConfigureLambdaRestApiResponse {
api: api.RestApi,
role?: iam.Role
}

/**
* Creates and configures an api.LambdaRestApi.
* @param scope - the construct to which the LambdaRestApi should be attached to.
* @param defaultApiGatewayProps - the default properties for the LambdaRestApi.
* @param apiGatewayProps - (optional) user-specified properties to override the default properties.
*/
function configureLambdaRestApi(scope: Construct, defaultApiGatewayProps: api.LambdaRestApiProps,
apiGatewayProps?: api.LambdaRestApiProps): [api.RestApi, iam.Role | undefined] {
apiGatewayProps?: api.LambdaRestApiProps): ConfigureLambdaRestApiResponse {

// API Gateway doesn't allow both endpointTypes and endpointConfiguration, check whether endPointTypes exists
if (apiGatewayProps?.endpointTypes) {
Expand Down Expand Up @@ -119,7 +124,7 @@ function configureLambdaRestApi(scope: Construct, defaultApiGatewayProps: api.La
}

// Return the API and CW Role
return [_api, cwRole];
return { api: _api, role: cwRole};
}

/**
Expand Down Expand Up @@ -170,20 +175,32 @@ function configureRestApi(scope: Construct, defaultApiGatewayProps: api.RestApiP
return [_api, cwRole];
}

export interface GlobalLambdaRestApiResponse {
readonly api: api.RestApi,
readonly role?: iam.Role,
readonly group: logs.LogGroup
}

/**
* Builds and returns a global api.RestApi designed to be used with an AWS Lambda function.
* @param scope - the construct to which the RestApi should be attached to.
* @param _existingLambdaObj - an existing AWS Lambda function.
* @param apiGatewayProps - (optional) user-specified properties to override the default properties.
*/
export function GlobalLambdaRestApi(scope: Construct, _existingLambdaObj: lambda.Function,
apiGatewayProps?: api.LambdaRestApiProps, logGroupProps?: logs.LogGroupProps): [api.RestApi, iam.Role | undefined, logs.LogGroup] {
apiGatewayProps?: api.LambdaRestApiProps, logGroupProps?: logs.LogGroupProps): GlobalLambdaRestApiResponse {
// Configure log group for API Gateway AccessLogging
const logGroup = buildLogGroup(scope, 'ApiAccessLogGroup', logGroupProps);

const defaultProps = apiDefaults.DefaultGlobalLambdaRestApiProps(_existingLambdaObj, logGroup);
const [restApi, apiCWRole] = configureLambdaRestApi(scope, defaultProps, apiGatewayProps);
return [restApi, apiCWRole, logGroup];
const restApi = configureLambdaRestApi(scope, defaultProps, apiGatewayProps);
return { api: restApi.api, role: restApi.role, group: logGroup};
}

export interface RegionalLambdaRestApiResponse {
readonly api: api.RestApi,
readonly role?: iam.Role,
readonly group: logs.LogGroup,
}

/**
Expand All @@ -193,13 +210,13 @@ export function GlobalLambdaRestApi(scope: Construct, _existingLambdaObj: lambda
* @param apiGatewayProps - (optional) user-specified properties to override the default properties.
*/
export function RegionalLambdaRestApi(scope: Construct, _existingLambdaObj: lambda.Function,
apiGatewayProps?: api.LambdaRestApiProps, logGroupProps?: logs.LogGroupProps): [api.RestApi, iam.Role | undefined, logs.LogGroup] {
apiGatewayProps?: api.LambdaRestApiProps, logGroupProps?: logs.LogGroupProps): RegionalLambdaRestApiResponse {
// Configure log group for API Gateway AccessLogging
const logGroup = buildLogGroup(scope, 'ApiAccessLogGroup', logGroupProps);

const defaultProps = apiDefaults.DefaultRegionalLambdaRestApiProps(_existingLambdaObj, logGroup);
const [restApi, apiCWRole] = configureLambdaRestApi(scope, defaultProps, apiGatewayProps);
return [restApi, apiCWRole, logGroup];
const restApi = configureLambdaRestApi(scope, defaultProps, apiGatewayProps);
return { api: restApi.api, role: restApi.role, group: logGroup};
}

/**
Expand Down

0 comments on commit b5b8398

Please sign in to comment.