|
| 1 | +/** |
| 2 | + * MIT No Attribution |
| 3 | + * |
| 4 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + * the Software without restriction, including without limitation the rights to |
| 8 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 9 | + * the Software, and to permit persons to whom the Software is furnished to do so. |
| 10 | + * |
| 11 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 12 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 14 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 15 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 17 | + * SOFTWARE. |
| 18 | + */ |
| 19 | + |
| 20 | +import { RemovalPolicy } from 'aws-cdk-lib'; |
| 21 | +import * as dynamodb from 'aws-cdk-lib/aws-dynamodb'; |
| 22 | +import * as ec2 from 'aws-cdk-lib/aws-ec2'; |
| 23 | +import * as ecr_assets from 'aws-cdk-lib/aws-ecr-assets'; |
| 24 | +import * as ecs from 'aws-cdk-lib/aws-ecs'; |
| 25 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 26 | +import * as logs from 'aws-cdk-lib/aws-logs'; |
| 27 | +import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager'; |
| 28 | +import { NagSuppressions } from 'cdk-nag'; |
| 29 | +import { Construct } from 'constructs'; |
| 30 | + |
| 31 | +export interface EcsAgentClusterProps { |
| 32 | + readonly vpc: ec2.IVpc; |
| 33 | + readonly agentImageAsset: ecr_assets.DockerImageAsset; |
| 34 | + readonly taskTable: dynamodb.ITable; |
| 35 | + readonly taskEventsTable: dynamodb.ITable; |
| 36 | + readonly userConcurrencyTable: dynamodb.ITable; |
| 37 | + readonly githubTokenSecret: secretsmanager.ISecret; |
| 38 | + readonly memoryId?: string; |
| 39 | +} |
| 40 | + |
| 41 | +export class EcsAgentCluster extends Construct { |
| 42 | + public readonly cluster: ecs.Cluster; |
| 43 | + public readonly taskDefinition: ecs.FargateTaskDefinition; |
| 44 | + public readonly securityGroup: ec2.SecurityGroup; |
| 45 | + public readonly containerName: string; |
| 46 | + public readonly taskRoleArn: string; |
| 47 | + public readonly executionRoleArn: string; |
| 48 | + |
| 49 | + constructor(scope: Construct, id: string, props: EcsAgentClusterProps) { |
| 50 | + super(scope, id); |
| 51 | + |
| 52 | + this.containerName = 'AgentContainer'; |
| 53 | + |
| 54 | + // ECS Cluster with Fargate capacity provider and container insights |
| 55 | + this.cluster = new ecs.Cluster(this, 'Cluster', { |
| 56 | + vpc: props.vpc, |
| 57 | + containerInsights: true, |
| 58 | + }); |
| 59 | + |
| 60 | + // Security group — egress TCP 443 only |
| 61 | + this.securityGroup = new ec2.SecurityGroup(this, 'TaskSG', { |
| 62 | + vpc: props.vpc, |
| 63 | + description: 'ECS Agent Tasks - egress TCP 443 only', |
| 64 | + allowAllOutbound: false, |
| 65 | + }); |
| 66 | + |
| 67 | + this.securityGroup.addEgressRule( |
| 68 | + ec2.Peer.anyIpv4(), |
| 69 | + ec2.Port.tcp(443), |
| 70 | + 'Allow HTTPS egress (GitHub API, AWS services)', |
| 71 | + ); |
| 72 | + |
| 73 | + // CloudWatch log group for agent task output |
| 74 | + const logGroup = new logs.LogGroup(this, 'TaskLogGroup', { |
| 75 | + retention: logs.RetentionDays.THREE_MONTHS, |
| 76 | + removalPolicy: RemovalPolicy.DESTROY, |
| 77 | + }); |
| 78 | + |
| 79 | + // Task execution role (used by ECS agent to pull images, write logs) |
| 80 | + // CDK creates this automatically via taskDefinition, but we need to |
| 81 | + // grant additional permissions to the task role. |
| 82 | + |
| 83 | + // Fargate task definition |
| 84 | + this.taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', { |
| 85 | + cpu: 2048, |
| 86 | + memoryLimitMiB: 4096, |
| 87 | + runtimePlatform: { |
| 88 | + cpuArchitecture: ecs.CpuArchitecture.ARM64, |
| 89 | + operatingSystemFamily: ecs.OperatingSystemFamily.LINUX, |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + // Container |
| 94 | + this.taskDefinition.addContainer(this.containerName, { |
| 95 | + image: ecs.ContainerImage.fromDockerImageAsset(props.agentImageAsset), |
| 96 | + logging: ecs.LogDrivers.awsLogs({ |
| 97 | + logGroup, |
| 98 | + streamPrefix: 'agent', |
| 99 | + }), |
| 100 | + environment: { |
| 101 | + CLAUDE_CODE_USE_BEDROCK: '1', |
| 102 | + TASK_TABLE_NAME: props.taskTable.tableName, |
| 103 | + TASK_EVENTS_TABLE_NAME: props.taskEventsTable.tableName, |
| 104 | + USER_CONCURRENCY_TABLE_NAME: props.userConcurrencyTable.tableName, |
| 105 | + LOG_GROUP_NAME: logGroup.logGroupName, |
| 106 | + GITHUB_TOKEN_SECRET_ARN: props.githubTokenSecret.secretArn, |
| 107 | + ...(props.memoryId && { MEMORY_ID: props.memoryId }), |
| 108 | + }, |
| 109 | + }); |
| 110 | + |
| 111 | + // Task role permissions |
| 112 | + const taskRole = this.taskDefinition.taskRole; |
| 113 | + |
| 114 | + // DynamoDB read/write on task tables |
| 115 | + props.taskTable.grantReadWriteData(taskRole); |
| 116 | + props.taskEventsTable.grantReadWriteData(taskRole); |
| 117 | + props.userConcurrencyTable.grantReadWriteData(taskRole); |
| 118 | + |
| 119 | + // Secrets Manager read for GitHub token |
| 120 | + props.githubTokenSecret.grantRead(taskRole); |
| 121 | + |
| 122 | + // Bedrock model invocation |
| 123 | + taskRole.addToPrincipalPolicy(new iam.PolicyStatement({ |
| 124 | + actions: [ |
| 125 | + 'bedrock:InvokeModel', |
| 126 | + 'bedrock:InvokeModelWithResponseStream', |
| 127 | + ], |
| 128 | + resources: ['*'], |
| 129 | + })); |
| 130 | + |
| 131 | + // CloudWatch Logs write |
| 132 | + logGroup.grantWrite(taskRole); |
| 133 | + |
| 134 | + // Expose role ARNs for scoped iam:PassRole in the orchestrator |
| 135 | + this.taskRoleArn = taskRole.roleArn; |
| 136 | + this.executionRoleArn = this.taskDefinition.executionRole!.roleArn; |
| 137 | + |
| 138 | + NagSuppressions.addResourceSuppressions(this.taskDefinition, [ |
| 139 | + { |
| 140 | + id: 'AwsSolutions-IAM5', |
| 141 | + reason: 'DynamoDB index/* wildcards generated by CDK grantReadWriteData; Bedrock InvokeModel requires * resource; Secrets Manager wildcards from CDK grantRead; CloudWatch Logs wildcards from CDK grantWrite', |
| 142 | + }, |
| 143 | + { |
| 144 | + id: 'AwsSolutions-ECS2', |
| 145 | + reason: 'Environment variables contain table names and configuration, not secrets — GitHub token is fetched from Secrets Manager at runtime', |
| 146 | + }, |
| 147 | + ], true); |
| 148 | + |
| 149 | + NagSuppressions.addResourceSuppressions(this.cluster, [ |
| 150 | + { |
| 151 | + id: 'AwsSolutions-ECS4', |
| 152 | + reason: 'Container insights is enabled via the containerInsights prop', |
| 153 | + }, |
| 154 | + ], true); |
| 155 | + } |
| 156 | +} |
0 commit comments