|
| 1 | +import { PythonFunction } from '@aws-cdk/aws-lambda-python-alpha'; |
| 2 | +import { CfnOutput, CustomResource, Duration, Stack } from 'aws-cdk-lib'; |
| 3 | +import { ITableV2 } from 'aws-cdk-lib/aws-dynamodb'; |
| 4 | +import { IRepository, Repository } from 'aws-cdk-lib/aws-ecr'; |
| 5 | +import { DockerImageAsset, Platform } from 'aws-cdk-lib/aws-ecr-assets'; |
| 6 | +import { IGrantable, IPrincipal, ManagedPolicy, PolicyStatement, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam'; |
| 7 | +import { Runtime } from 'aws-cdk-lib/aws-lambda'; |
| 8 | +import { IBucket } from 'aws-cdk-lib/aws-s3'; |
| 9 | +import { IStringParameter } from 'aws-cdk-lib/aws-ssm'; |
| 10 | +import { Construct } from 'constructs'; |
| 11 | +import { ContainerImageBuild } from 'deploy-time-build'; |
| 12 | +import { readFileSync } from 'fs'; |
| 13 | +import { join } from 'path'; |
| 14 | +import { WorkerBus } from './bus'; |
| 15 | +import { DockerImageName, ECRDeployment } from 'cdk-ecr-deployment'; |
| 16 | + |
| 17 | +export interface AgentCoreRuntimeProps { |
| 18 | + repository: IRepository; |
| 19 | + storageTable: ITableV2; |
| 20 | + imageBucket: IBucket; |
| 21 | + bus: WorkerBus; |
| 22 | + slackBotTokenParameter: IStringParameter; |
| 23 | + gitHubApp?: { |
| 24 | + privateKeyParameterName: string; |
| 25 | + appId: string; |
| 26 | + installationId: string; |
| 27 | + }; |
| 28 | + gitHubAppPrivateKeyParameter?: IStringParameter; |
| 29 | + githubPersonalAccessTokenParameter?: IStringParameter; |
| 30 | + loadBalancing?: { |
| 31 | + awsAccounts: string[]; |
| 32 | + roleName: string; |
| 33 | + }; |
| 34 | + accessLogBucket: IBucket; |
| 35 | + amiIdParameterName: string; |
| 36 | + webappOriginSourceParameter: IStringParameter; |
| 37 | +} |
| 38 | + |
| 39 | +export class AgentCoreRuntime extends Construct implements IGrantable { |
| 40 | + public grantPrincipal: IPrincipal; |
| 41 | + public runtimeArn: string; |
| 42 | + |
| 43 | + constructor(scope: Construct, id: string, props: AgentCoreRuntimeProps) { |
| 44 | + super(scope, id); |
| 45 | + |
| 46 | + const repository = props.repository; |
| 47 | + |
| 48 | + const crHandler = new PythonFunction(this, 'CustomResourceHandler', { |
| 49 | + runtime: Runtime.PYTHON_3_13, |
| 50 | + timeout: Duration.seconds(10), |
| 51 | + entry: join(__dirname, 'resources', 'agent-core-runtime-cr'), |
| 52 | + }); |
| 53 | + crHandler.role!.addToPrincipalPolicy( |
| 54 | + new PolicyStatement({ |
| 55 | + actions: [ |
| 56 | + 'bedrock-agentcore:ListAgentRuntimes', |
| 57 | + 'bedrock-agentcore:CreateAgentRuntime', |
| 58 | + 'bedrock-agentcore:UpdateAgentRuntime', |
| 59 | + 'bedrock-agentcore:DeleteAgentRuntime', |
| 60 | + 'iam:PassRole', |
| 61 | + ], |
| 62 | + resources: ['*'], |
| 63 | + }) |
| 64 | + ); |
| 65 | + crHandler.role?.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('BedrockAgentCoreFullAccess')); |
| 66 | + |
| 67 | + const role = new Role(this, 'Role', { |
| 68 | + assumedBy: ServicePrincipal.fromStaticServicePrincipleName('bedrock-agentcore.amazonaws.com'), |
| 69 | + }); |
| 70 | + this.grantPrincipal = role; |
| 71 | + |
| 72 | + const image = new DockerImageAsset(this, 'WorkerImage', { |
| 73 | + directory: '..', |
| 74 | + file: join('docker', 'agent.Dockerfile'), |
| 75 | + exclude: readFileSync('.dockerignore').toString().split('\n'), |
| 76 | + platform: Platform.LINUX_ARM64, |
| 77 | + }); |
| 78 | + const deployDest = new DockerImageName(`${repository.repositoryUri}:${image.imageTag}`); |
| 79 | + const deploy = new ECRDeployment(this, 'ImageDeploy', { |
| 80 | + src: new DockerImageName(image.imageUri), |
| 81 | + dest: deployDest, |
| 82 | + imageArch: ['arm64'], |
| 83 | + }); |
| 84 | + |
| 85 | + role.addToPrincipalPolicy( |
| 86 | + new PolicyStatement({ |
| 87 | + actions: ['ecr:BatchGetImage', 'ecr:GetDownloadUrlForLayer'], |
| 88 | + resources: [`${repository.repositoryArn}`], |
| 89 | + }) |
| 90 | + ); |
| 91 | + role.addToPrincipalPolicy( |
| 92 | + new PolicyStatement({ |
| 93 | + actions: [ |
| 94 | + 'ecr:GetAuthorizationToken', |
| 95 | + 'xray:PutTraceSegments', |
| 96 | + 'xray:PutTelemetryRecords', |
| 97 | + 'xray:GetSamplingRules', |
| 98 | + 'xray:GetSamplingTargets', |
| 99 | + 'cloudwatch:PutMetricData', |
| 100 | + 'logs:DescribeLogStreams', |
| 101 | + 'logs:DescribeLogGroups', |
| 102 | + 'logs:CreateLogGroup', |
| 103 | + 'logs:CreateLogStream', |
| 104 | + 'logs:PutLogEvents', |
| 105 | + 'bedrock-agentcore:GetWorkloadAccessToken', |
| 106 | + 'bedrock-agentcore:GetWorkloadAccessTokenForJWT', |
| 107 | + 'bedrock-agentcore:GetWorkloadAccessTokenForUserId', |
| 108 | + ], |
| 109 | + resources: ['*'], |
| 110 | + }) |
| 111 | + ); |
| 112 | + role.addToPrincipalPolicy( |
| 113 | + new PolicyStatement({ |
| 114 | + actions: ['bedrock:InvokeModel'], |
| 115 | + resources: ['*'], |
| 116 | + }) |
| 117 | + ); |
| 118 | + props.storageTable.grantReadWriteData(role); |
| 119 | + props.imageBucket.grantReadWrite(role); |
| 120 | + props.gitHubAppPrivateKeyParameter?.grantRead(role); |
| 121 | + props.githubPersonalAccessTokenParameter?.grantRead(role); |
| 122 | + props.slackBotTokenParameter.grantRead(role); |
| 123 | + props.bus.api.grantPublishAndSubscribe(role); |
| 124 | + props.bus.api.grantConnect(role); |
| 125 | + |
| 126 | + const resource = new CustomResource(this, 'Resource', { |
| 127 | + serviceToken: crHandler.functionArn, |
| 128 | + resourceType: 'Custom::AgentCoreRuntime', |
| 129 | + properties: { |
| 130 | + ContainerUri: `${repository.repositoryUri}:${image.imageTag}`, |
| 131 | + RoleArn: role.roleArn, |
| 132 | + ServerProtocol: 'HTTP', |
| 133 | + Env: { |
| 134 | + AWS_REGION: Stack.of(this).region, |
| 135 | + WORKER_RUNTIME: 'agent-core', |
| 136 | + EVENT_HTTP_ENDPOINT: props.bus.httpEndpoint, |
| 137 | + GITHUB_APP_PRIVATE_KEY_PARAMETER_NAME: props.gitHubAppPrivateKeyParameter?.parameterName ?? '', |
| 138 | + GITHUB_APP_ID: props.gitHubApp?.appId ?? '', |
| 139 | + GITHUB_APP_INSTALLATION_ID: props.gitHubApp?.installationId ?? '', |
| 140 | + TABLE_NAME: props.storageTable.tableName, |
| 141 | + BUCKET_NAME: props.imageBucket.bucketName, |
| 142 | + WEBAPP_ORIGIN_NAME_PARAMETER: props.webappOriginSourceParameter.parameterName, |
| 143 | + // BEDROCK_AWS_ACCOUNTS: props.loadBalancing?.awsAccounts.join(',') ?? '', |
| 144 | + // BEDROCK_AWS_ROLE_NAME: props.loadBalancing?.roleName ?? '', |
| 145 | + SLACK_BOT_TOKEN_PARAMETER_NAME: props.slackBotTokenParameter.parameterName ?? '', |
| 146 | + GITHUB_PERSONAL_ACCESS_TOKEN_PARAMETER_NAME: props.githubPersonalAccessTokenParameter?.parameterName ?? '', |
| 147 | + }, |
| 148 | + }, |
| 149 | + serviceTimeout: Duration.seconds(20), |
| 150 | + }); |
| 151 | + resource.node.addDependency(deploy.node.findChild('CustomResource')); |
| 152 | + |
| 153 | + this.runtimeArn = resource.getAttString('agentRuntimeArn'); |
| 154 | + new CfnOutput(this, 'RuntimeArn', { value: this.runtimeArn }); |
| 155 | + } |
| 156 | +} |
0 commit comments