|
| 1 | +# Bedrock Agent CloudFormation Deployment |
| 2 | + |
| 3 | +이 폴더는 Amazon Bedrock Agent를 다른 AWS 계정에서 재사용할 수 있도록 CloudFormation 템플릿과 Python 배포 스크립트를 제공합니다. |
| 4 | + |
| 5 | +## 파일 구성 |
| 6 | + |
| 7 | +- `bedrock-agent.yaml`: CloudFormation 템플릿 |
| 8 | +- `deploy-agent.py`: Python 배포 스크립트 |
| 9 | +- `README.md`: 사용 가이드 |
| 10 | + |
| 11 | +## 사전 요구사항 |
| 12 | + |
| 13 | +1. **AWS CLI 설정** |
| 14 | + ```bash |
| 15 | + aws configure |
| 16 | + ``` |
| 17 | + |
| 18 | +2. **Python 패키지 설치** |
| 19 | + ```bash |
| 20 | + pip install boto3 |
| 21 | + ``` |
| 22 | + |
| 23 | +3. **DynamoDB 테이블 존재** |
| 24 | + - 테이블명: `Class` (또는 사용자 지정) |
| 25 | + - 필수 필드: `name`, `description`, `searchableText`, `url`, `author`, `difficulty` |
| 26 | + |
| 27 | +4. **Bedrock 모델 액세스** |
| 28 | + - Claude 3 Sonnet 모델에 대한 액세스 권한 필요 |
| 29 | + - AWS Console > Bedrock > Model access에서 활성화 |
| 30 | + |
| 31 | +## 배포 방법 |
| 32 | + |
| 33 | +### 1. 기본 배포 |
| 34 | +```bash |
| 35 | +python deploy-agent.py |
| 36 | +``` |
| 37 | + |
| 38 | +### 2. 사용자 정의 배포 |
| 39 | +```bash |
| 40 | +python deploy-agent.py \ |
| 41 | + --stack-name my-bedrock-agent \ |
| 42 | + --agent-name MyAgent \ |
| 43 | + --region ap-northeast-2 \ |
| 44 | + --dynamodb-table MyClassTable \ |
| 45 | + --test |
| 46 | +``` |
| 47 | + |
| 48 | +### 3. AWS 프로필 사용 |
| 49 | +```bash |
| 50 | +python deploy-agent.py --profile my-aws-profile --region us-east-1 |
| 51 | +``` |
| 52 | + |
| 53 | +## 배포 매개변수 |
| 54 | + |
| 55 | +| 매개변수 | 기본값 | 설명 | |
| 56 | +|---------|--------|------| |
| 57 | +| `--stack-name` | `bedrock-course-agent` | CloudFormation 스택 이름 | |
| 58 | +| `--agent-name` | `CourseSearchAgent` | Bedrock Agent 이름 | |
| 59 | +| `--region` | `us-west-2` | AWS 리전 | |
| 60 | +| `--profile` | (없음) | AWS 프로필 이름 | |
| 61 | +| `--dynamodb-table` | `Class` | DynamoDB 테이블 이름 | |
| 62 | +| `--model-id` | `anthropic.claude-3-sonnet-20240229-v1:0` | 기반 모델 ID | |
| 63 | +| `--test` | (없음) | 배포 후 테스트 실행 | |
| 64 | + |
| 65 | +## 생성되는 리소스 |
| 66 | + |
| 67 | +1. **Bedrock Agent**: 코스 검색 AI 에이전트 |
| 68 | +2. **Lambda Function**: DynamoDB 검색 함수 |
| 69 | +3. **IAM Roles**: Agent 및 Lambda 실행 역할 |
| 70 | +4. **Action Group**: Agent와 Lambda 연결 |
| 71 | +5. **Agent Alias**: Agent 버전 관리 |
| 72 | + |
| 73 | +## 출력값 |
| 74 | + |
| 75 | +배포 완료 후 다음 정보가 출력됩니다: |
| 76 | + |
| 77 | +- `AgentId`: Bedrock Agent ID |
| 78 | +- `AgentAliasId`: Agent Alias ID |
| 79 | +- `LambdaFunctionArn`: Lambda 함수 ARN |
| 80 | +- `AgentArn`: Agent ARN |
| 81 | + |
| 82 | +## 사용 예시 |
| 83 | + |
| 84 | +### Python에서 Agent 호출 |
| 85 | +```python |
| 86 | +import boto3 |
| 87 | + |
| 88 | +client = boto3.client('bedrock-agent-runtime', region_name='us-west-2') |
| 89 | + |
| 90 | +response = client.invoke_agent( |
| 91 | + agentId='YOUR_AGENT_ID', |
| 92 | + agentAliasId='YOUR_ALIAS_ID', |
| 93 | + sessionId='session-123', |
| 94 | + inputText='EKS 강의 추천해줘' |
| 95 | +) |
| 96 | + |
| 97 | +# 스트리밍 응답 처리 |
| 98 | +for chunk in response['completion']: |
| 99 | + if 'chunk' in chunk: |
| 100 | + print(chunk['chunk']['bytes'].decode('utf-8'), end='') |
| 101 | +``` |
| 102 | + |
| 103 | +### AWS CLI에서 테스트 |
| 104 | +```bash |
| 105 | +aws bedrock-agent-runtime invoke-agent \ |
| 106 | + --agent-id YOUR_AGENT_ID \ |
| 107 | + --agent-alias-id YOUR_ALIAS_ID \ |
| 108 | + --session-id test-session \ |
| 109 | + --input-text "Lambda 기초 강의 추천해줘" \ |
| 110 | + --region us-west-2 \ |
| 111 | + response.json |
| 112 | +``` |
| 113 | + |
| 114 | +## 문제 해결 |
| 115 | + |
| 116 | +### 1. 권한 오류 |
| 117 | +``` |
| 118 | +Error: User is not authorized to perform: bedrock:CreateAgent |
| 119 | +``` |
| 120 | +**해결**: IAM 사용자에게 Bedrock 권한 추가 |
| 121 | +```json |
| 122 | +{ |
| 123 | + "Version": "2012-10-17", |
| 124 | + "Statement": [ |
| 125 | + { |
| 126 | + "Effect": "Allow", |
| 127 | + "Action": [ |
| 128 | + "bedrock:*", |
| 129 | + "lambda:*", |
| 130 | + "iam:*", |
| 131 | + "cloudformation:*" |
| 132 | + ], |
| 133 | + "Resource": "*" |
| 134 | + } |
| 135 | + ] |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +### 2. 모델 액세스 오류 |
| 140 | +``` |
| 141 | +Error: Model access denied |
| 142 | +``` |
| 143 | +**해결**: AWS Console > Bedrock > Model access에서 Claude 3 Sonnet 활성화 |
| 144 | + |
| 145 | +### 3. DynamoDB 테이블 없음 |
| 146 | +``` |
| 147 | +Error: Table 'Class' not found |
| 148 | +``` |
| 149 | +**해결**: DynamoDB 테이블 생성 또는 `--dynamodb-table` 매개변수로 기존 테이블 지정 |
| 150 | + |
| 151 | +### 4. Agent 준비 실패 |
| 152 | +``` |
| 153 | +Error: Agent preparation failed |
| 154 | +``` |
| 155 | +**해결**: |
| 156 | +- Lambda 함수 로그 확인 |
| 157 | +- IAM 권한 확인 |
| 158 | +- Action Group 스키마 검증 |
| 159 | + |
| 160 | +## 정리 |
| 161 | + |
| 162 | +스택 삭제: |
| 163 | +```bash |
| 164 | +aws cloudformation delete-stack --stack-name bedrock-course-agent |
| 165 | +``` |
| 166 | + |
| 167 | +## 지원 |
| 168 | + |
| 169 | +문제가 발생하면 다음을 확인하세요: |
| 170 | +1. AWS 자격 증명 및 권한 |
| 171 | +2. Bedrock 모델 액세스 권한 |
| 172 | +3. DynamoDB 테이블 존재 및 구조 |
| 173 | +4. CloudFormation 스택 이벤트 로그 |
0 commit comments