|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
1 | 13 | import * as cdk from 'aws-cdk-lib'; |
2 | 14 | import { Construct } from 'constructs'; |
3 | | -// import * as sqs from 'aws-cdk-lib/aws-sqs'; |
| 15 | +import * as ecs from 'aws-cdk-lib/aws-ecs'; |
| 16 | +import path = require('path'); |
| 17 | +import { AlbToFargate } from '@aws-solutions-constructs/aws-alb-fargate'; |
| 18 | +import * as ecr_assets from 'aws-cdk-lib/aws-ecr-assets'; |
| 19 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 20 | +import { Duration, RemovalPolicy } from 'aws-cdk-lib'; |
| 21 | +import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2'; |
| 22 | +import * as logs from 'aws-cdk-lib/aws-logs'; |
4 | 23 |
|
5 | 24 | export class McpStatelessEcsStack extends cdk.Stack { |
6 | 25 | constructor(scope: Construct, id: string, props?: cdk.StackProps) { |
7 | 26 | super(scope, id, props); |
8 | 27 |
|
9 | | - // The code that defines your stack goes here |
| 28 | + const execution_role = new iam.Role( |
| 29 | + this, |
| 30 | + "ExecutionRole", |
| 31 | + { |
| 32 | + assumedBy: new iam.ServicePrincipal("ecs-tasks.amazonaws.com"), |
| 33 | + managedPolicies: [ |
| 34 | + iam.ManagedPolicy.fromAwsManagedPolicyName("service-role/AmazonECSTaskExecutionRolePolicy") |
| 35 | + ] |
| 36 | + } |
| 37 | + ) |
10 | 38 |
|
11 | | - // example resource |
12 | | - // const queue = new sqs.Queue(this, 'McpStatelessEcsQueue', { |
13 | | - // visibilityTimeout: cdk.Duration.seconds(300) |
14 | | - // }); |
15 | | - } |
16 | | -} |
| 39 | + const task_role = new iam.Role( |
| 40 | + this, |
| 41 | + "TaskRole", |
| 42 | + { |
| 43 | + assumedBy: new iam.ServicePrincipal("ecs-tasks.amazonaws.com"), |
| 44 | + } |
| 45 | + ) |
| 46 | + |
| 47 | + task_role.addToPolicy( |
| 48 | + new iam.PolicyStatement( |
| 49 | + { |
| 50 | + actions: ["logs:CreateLogGroup", |
| 51 | + "logs:CreateLogStream", |
| 52 | + "logs:PutLogEvents", |
| 53 | + ], |
| 54 | + resources: ["*"], |
| 55 | + } |
| 56 | + ) |
| 57 | + ) |
| 58 | + |
| 59 | + const log_group = new logs.LogGroup( |
| 60 | + this, |
| 61 | + "LogGroup", |
| 62 | + { |
| 63 | + retention: logs.RetentionDays.ONE_WEEK, |
| 64 | + removalPolicy: RemovalPolicy.DESTROY, |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + const task_definition = new ecs.FargateTaskDefinition( |
| 69 | + this, |
| 70 | + "TaskDefinition", |
| 71 | + { |
| 72 | + taskRole: task_role, |
| 73 | + executionRole: execution_role, |
| 74 | + } |
| 75 | + ) |
| 76 | + |
| 77 | + const container_definition_props = { |
| 78 | + image: ecs.ContainerImage.fromAsset( |
| 79 | + path.join(__dirname, '../mcp_server'), |
| 80 | + { |
| 81 | + file: 'Dockerfile', |
| 82 | + platform: ecr_assets.Platform.LINUX_AMD64 |
| 83 | + } |
| 84 | + ), |
| 85 | + essential: true, |
| 86 | + portMappings: [ |
| 87 | + { |
| 88 | + containerPort: 3000, |
| 89 | + hostPort: 3000, |
| 90 | + protocol: ecs.Protocol.TCP |
| 91 | + } |
| 92 | + ], |
| 93 | + logging: ecs.LogDrivers.awsLogs({ |
| 94 | + logGroup: log_group, |
| 95 | + streamPrefix: "ecs" |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + task_definition.addContainer("McpServerContainer", |
| 100 | + { |
| 101 | + image: ecs.ContainerImage.fromAsset( |
| 102 | + path.join(__dirname, '../mcp_server'), |
| 103 | + { |
| 104 | + file: 'Dockerfile', |
| 105 | + platform: ecr_assets.Platform.LINUX_AMD64 |
| 106 | + } |
| 107 | + ), |
| 108 | + essential: true, |
| 109 | + portMappings: [ |
| 110 | + { |
| 111 | + containerPort: 3000, |
| 112 | + hostPort: 3000, |
| 113 | + protocol: ecs.Protocol.TCP |
| 114 | + } |
| 115 | + ], |
| 116 | + logging: ecs.LogDrivers.awsLogs({ |
| 117 | + logGroup: log_group, |
| 118 | + streamPrefix: "ecs" |
| 119 | + }) |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + // Define target group configuration |
| 124 | + const target_group_props = { |
| 125 | + port: 3000, |
| 126 | + protocol: elbv2.ApplicationProtocol.HTTP, |
| 127 | + healthCheck: { |
| 128 | + path: "/health", |
| 129 | + port: "3000", |
| 130 | + healthyHttpCodes: "200", |
| 131 | + interval: Duration.seconds(30), |
| 132 | + timeout: Duration.seconds(5) |
| 133 | + }, |
| 134 | + targetType: elbv2.TargetType.IP |
| 135 | + } |
| 136 | + |
| 137 | + const albToFargate = new AlbToFargate(this, 'AlbToFargate', { |
| 138 | + publicApi: true, |
| 139 | + fargateTaskDefinitionProps: task_definition, |
| 140 | + targetGroupProps: target_group_props, |
| 141 | + containerDefinitionProps: container_definition_props, |
| 142 | + listenerProps: { |
| 143 | + port: 80, |
| 144 | + protocol: elbv2.ApplicationProtocol.HTTP, |
| 145 | + }, |
| 146 | + }); |
| 147 | + |
| 148 | + // add output for the endpoint |
| 149 | + new cdk.CfnOutput(this, 'McpServerEndpoint', { |
| 150 | + description: 'MCP Server Endpoint', |
| 151 | + value: `http://${albToFargate.loadBalancer.loadBalancerDnsName}/mcp`, |
| 152 | + exportName: `${cdk.Stack.of(this).stackName}${id}McpServerEndpoint`, |
| 153 | +}); |
| 154 | + |
| 155 | +}} |
0 commit comments