Skip to content

Commit

Permalink
Offload HttpApi construction to another file
Browse files Browse the repository at this point in the history
  • Loading branch information
NChitty committed Mar 29, 2024
1 parent 9d77a3c commit b40a437
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 25 deletions.
30 changes: 5 additions & 25 deletions cdk/lib/application-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
RecordTarget,
} from 'aws-cdk-lib/aws-route53';
import { ApiGatewayDomain } from 'aws-cdk-lib/aws-route53-targets';
import { MealPlannerHttpApi } from './constructs/api';

export interface ApplicationLayerStackProps extends StackProps {
readonly delegationRole: Role;
Expand Down Expand Up @@ -73,31 +74,10 @@ export default class ApplicationLayerStack extends Stack {
delegationRole: props.delegationRole,
});

const domainName = ['api', props.domain].join('.');
const certificate = new Certificate(this, 'ApiDomainCertificate', {
domainName,
validation: CertificateValidation.fromDns(hostedZone),
});
const api = new LambdaRestApi(this, 'MealPlannerApi', {
handler,
proxy: true,
domainName: {
domainName,
certificate,
basePath: 'mealplanner',
},
disableExecuteApiEndpoint: true,
});

const apiDomainName = api.domainName || api.addDomainName('ApiDomainName', {
domainName,
certificate,
});

new ARecord(this, 'ApiAliasRecord', {
zone: hostedZone,
recordName: 'api',
target: RecordTarget.fromAlias(new ApiGatewayDomain(apiDomainName)),
new MealPlannerHttpApi(this, 'ApiConstruct', {
domain: props.domain,
hostedZone,
lambda: handler,
});
}
}
54 changes: 54 additions & 0 deletions cdk/lib/constructs/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Construct } from 'constructs';
import { DomainName, HttpApi } from 'aws-cdk-lib/aws-apigatewayv2';
import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
import { IFunction } from 'aws-cdk-lib/aws-lambda';
import { ARecord, IHostedZone, RecordTarget } from 'aws-cdk-lib/aws-route53';
import { Certificate, CertificateValidation } from 'aws-cdk-lib/aws-certificatemanager';
import { ApiGatewayv2DomainProperties } from 'aws-cdk-lib/aws-route53-targets';

export interface MealPlannerHttpApiProps {
readonly domain: string;
readonly hostedZone: IHostedZone;
readonly lambda: IFunction;
}

/**
* Construct for building HttpApi.
*/
export class MealPlannerHttpApi extends Construct {
/**
* Builds an HttpApi.
* @param{Construct} scope the parent scope
* @param{string} id the logical id
* @param{MealPlannerHttpApiProps} props properties
*/
constructor(scope: Construct, id: string, props: MealPlannerHttpApiProps) {
super(scope, id);
const lambda = new HttpLambdaIntegration('LambdaIntegration', props.lambda);
const domainName = ['api', props.domain].join('.');
const certificate = new Certificate(this, 'ApiDomainCertificate', {
domainName,
validation: CertificateValidation.fromDns(props.hostedZone),
});
const domain = new DomainName(scope, 'Domain', {
domainName,
certificate,
});
new ARecord(this, 'ApiAliasRecord', {
zone: props.hostedZone,
recordName: 'api',
target: RecordTarget.fromAlias(new ApiGatewayv2DomainProperties(
domain.regionalDomainName,
domain.regionalHostedZoneId,
)),
});
new HttpApi(scope, 'Api', {
defaultIntegration: lambda,
defaultDomainMapping: {
domainName: domain,
mappingKey: 'mealplanner',
},
disableExecuteApiEndpoint: true,
});
}
}

0 comments on commit b40a437

Please sign in to comment.