mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-06-02 07:27:33 +00:00
All the examples using SAM have a default Lambda runtime environment memory size of 512Mb. Lambda functions run in a microVM defined by its memory size. The memory size influences the CPU power. (see https://docs.aws.amazon.com/lambda/latest/dg/configuration-memory.html) Increasing memory size increases runtime performance but also increase costs. As most of our examples are very simple and small functions, 512Mb memory is not required. This PR reduces Lambda runtime execution environment to 128Mb to reduce AWS costs. Co-authored-by: Sebastien Stormacq <stormacq@amazon.lu>
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftAWSLambdaRuntime open source project
|
|
//
|
|
// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import * as cdk from 'aws-cdk-lib';
|
|
import * as lambda from 'aws-cdk-lib/aws-lambda';
|
|
import * as apigateway from 'aws-cdk-lib/aws-apigatewayv2';
|
|
import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
|
|
|
|
export class LambdaApiStack extends cdk.Stack {
|
|
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
|
|
super(scope, id, props);
|
|
|
|
// Create the Lambda function
|
|
const lambdaFunction = new lambda.Function(this, 'SwiftLambdaFunction', {
|
|
runtime: lambda.Runtime.PROVIDED_AL2,
|
|
architecture: lambda.Architecture.ARM_64,
|
|
handler: 'bootstrap',
|
|
code: lambda.Code.fromAsset('../.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGatewayLambda/APIGatewayLambda.zip'),
|
|
memorySize: 128,
|
|
timeout: cdk.Duration.seconds(30),
|
|
environment: {
|
|
LOG_LEVEL: 'debug',
|
|
},
|
|
});
|
|
|
|
// Create the integration
|
|
const integration = new HttpLambdaIntegration(
|
|
'LambdaIntegration',
|
|
lambdaFunction
|
|
);
|
|
|
|
// Create HTTP API with the integration
|
|
const httpApi = new apigateway.HttpApi(this, 'HttpApi', {
|
|
defaultIntegration: integration,
|
|
});
|
|
|
|
// Output the API URL
|
|
new cdk.CfnOutput(this, 'ApiUrl', {
|
|
value: httpApi.url ?? 'Something went wrong',
|
|
});
|
|
}
|
|
}
|
|
|