Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to HttpApi #50

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
});
}
}
34 changes: 17 additions & 17 deletions lambda/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ async fn main() -> Result<(), Error> {
let app = Router::new()
.route("/", get(root))
.route("/ping", get(ping))
.nest("/recipes", recipe_service);
let mealplanner = Router::new().nest("/mealplanner", app).layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::new().include_headers(true))
.on_request(DefaultOnRequest::new().level(Level::INFO))
.on_response(
DefaultOnResponse::new()
.level(Level::INFO)
.latency_unit(LatencyUnit::Micros),
)
.on_failure(
DefaultOnFailure::new()
.level(Level::INFO)
.latency_unit(LatencyUnit::Micros),
),
);
.nest("/recipes", recipe_service)
.layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::new().include_headers(true))
.on_request(DefaultOnRequest::new().level(Level::INFO))
.on_response(
DefaultOnResponse::new()
.level(Level::INFO)
.latency_unit(LatencyUnit::Micros),
)
.on_failure(
DefaultOnFailure::new()
.level(Level::INFO)
.latency_unit(LatencyUnit::Micros),
),
);

run(mealplanner).await
run(app).await
}
Loading