-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-stack.ts
More file actions
77 lines (70 loc) · 2.76 KB
/
Copy pathexample-stack.ts
File metadata and controls
77 lines (70 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
/**
* CDK スタック定義。MCP Lambda サーバーと API Gateway をデプロイします。
* @class PlatformStack
*/
export class ExampleStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const projectName = this.node.tryGetContext('project-name') ?? 'aws-lambda';
// Lambda関数のIAMロール
const lambdaRole = new cdk.aws_iam.Role(this, 'LambdaRole', {
assumedBy: new cdk.aws_iam.ServicePrincipal('lambda.amazonaws.com'),
managedPolicies: [
cdk.aws_iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole'),
],
});
const mcpServerFunctionName = `${projectName}-mcp-server-example`;
const mcpServerLogGroup = new cdk.aws_logs.LogGroup(this, 'McpServerFunctionLogGroup', {
logGroupName: `/aws/lambda/${mcpServerFunctionName}`,
removalPolicy: cdk.RemovalPolicy.DESTROY,
retention: cdk.aws_logs.RetentionDays.ONE_DAY,
});
// MCP Server のLambda関数
const mcpServer = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'McpServer', {
functionName: mcpServerFunctionName,
entry: 'lambda/index.ts',
runtime: cdk.aws_lambda.Runtime.NODEJS_24_X,
architecture: cdk.aws_lambda.Architecture.ARM_64,
role: lambdaRole,
timeout: cdk.Duration.seconds(30),
logGroup: mcpServerLogGroup,
loggingFormat: cdk.aws_lambda.LoggingFormat.JSON,
memorySize: 128,
bundling: {
// No externalModules since we want to bundle everything
nodeModules: [
'@modelcontextprotocol/sdk',
'hono',
'zod',
],
externalModules: [
'dotenv',
'@hono/node-server',
],
// minify: true, // コードの最小化
sourceMap: true, // ソースマップを有効化(デバッグ用)
keepNames: true,
format: cdk.aws_lambda_nodejs.OutputFormat.ESM,
target: 'node22', // Target Node.js 22.x
banner: 'import { createRequire } from \'module\';const require = createRequire(import.meta.url);',
},
});
// API Gateway
const api = new cdk.aws_apigateway.RestApi(this, 'MCPAPI', {
restApiName: 'MCP API',
description: 'API for MCP',
defaultCorsPreflightOptions: {
allowOrigins: cdk.aws_apigateway.Cors.ALL_ORIGINS,
allowMethods: cdk.aws_apigateway.Cors.ALL_METHODS,
},
deployOptions: {
stageName: 'v1',
},
endpointTypes: [cdk.aws_apigateway.EndpointType.REGIONAL],
});
const mcpResource = api.root.addResource('mcp');
mcpResource.addMethod('ANY', new cdk.aws_apigateway.LambdaIntegration(mcpServer));
}
}