./knowledge-base/NHKAPIアクセスガイド.md

## 📘 概要

このドキュメントは、NHK AI Platform(生成AI基盤)にアクセスするAWSリソースを開発する際に必要な情報をまとめたものです。

**作成日**: 2026年1月26日

**リージョン**: ap-northeast-1(東京)

**アカウントID**: 903877990773

**固定IPアドレス**: `52.68.152.34`

---

## 🏗️ ネットワーク構成

### VPC情報

- **VPC ID**: `vpc-08d84efb87d052cf9`
- **CIDR**: `10.0.0.0/16`
- **DNS解決**: 有効
- **DNSホスト名**: 有効

### サブネット構成

| サブネット名         | サブネットID                 | CIDR             | AZ                  | 用途             |
| -------------------- | ---------------------------- | ---------------- | ------------------- | ---------------- |
| Public Subnet        | subnet-0aa985c78e9b67381     | 10.0.0.0/24      | ap-northeast-1a     | NAT Gateway配置  |
| **Private Subnet A** | **subnet-0ebcb5a9bc54d1bd1** | **10.0.10.0/24** | **ap-northeast-1a** | **アプリ配置用** |
| **Private Subnet C** | **subnet-03fa4782f20ba49ec** | **10.0.11.0/24** | **ap-northeast-1c** | **アプリ配置用** |

### NAT Gateway

- **NAT Gateway ID**: `nat-0355c595db360d105`
- **配置先**: Public Subnet (ap-northeast-1a)
- **固定IP**: `52.68.152.34` (Elastic IP: eipalloc-07f0d7325b014b8e2)

### Security Group

- **Security Group ID**: `sg-0faff8d9c30c94dc0`
- **名前**: `nhk-ai-platform-access-sg`
- **説明**: Security group for NHK AI Platform access
- **アウトバウンドルール**: HTTPS (443) → 0.0.0.0/0

---

## 🔐 APIキー取得方法

### Secrets Manager情報

- **ARN**: `arn:aws:secretsmanager:ap-northeast-1:903877990773:secret:nhk_ai_api_key_lite_llm-Yymln7`
- **Secret名**: `nhk_ai_api_key_lite_llm`
- **リージョン**: ap-northeast-1

### AWS SDK / CLI での取得例

### AWS CLI

```bash
aws secretsmanager get-secret-value \\
  --secret-id nhk_ai_api_key_lite_llm \\
  --region ap-northeast-1 \\
  --query SecretString \\
  --output text

```

### Python (Boto3)

```python
import boto3
import json

def get_api_key():
    client = boto3.client('secretsmanager', region_name='ap-northeast-1')
    response = client.get_secret_value(SecretId='nhk_ai_api_key_lite_llm')
    secret = json.loads(response['SecretString'])
    return secret['api_key']  # キー名は実際の構造に合わせて調整

```

### Node.js (AWS SDK v3)

```jsx
import {
  SecretsManagerClient,
  GetSecretValueCommand,
} from "@aws-sdk/client-secrets-manager";

async function getApiKey() {
  const client = new SecretsManagerClient({ region: "ap-northeast-1" });
  const response = await client.send(
    new GetSecretValueCommand({ SecretId: "nhk_ai_api_key_lite_llm" }),
  );
  const secret = JSON.parse(response.SecretString);
  return secret.api_key; // キー名は実際の構造に合わせて調整
}
```

---

## 🚀 設定ガイド

### AWS Lambda の場合

### VPC設定

```yaml
VPC Configuration:
  VPC ID: vpc-08d84efb87d052cf9
  Subnets:
    - subnet-0ebcb5a9bc54d1bd1 # Private Subnet A (推奨)
    - subnet-03fa4782f20ba49ec # Private Subnet C (冗長化)
  Security Groups:
    - sg-0faff8d9c30c94dc0
```

### 必要なIAMポリシー

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ec2:CreateNetworkInterface",
        "ec2:DescribeNetworkInterfaces",
        "ec2:DeleteNetworkInterface"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "secretsmanager:GetSecretValue",
      "Resource": "arn:aws:secretsmanager:ap-northeast-1:903877990773:secret:nhk_ai_api_key_lite_llm-Yymln7"
    }
  ]
}
```

## ⚠️ 重要な注意事項

### ✅ 必ず守ること

1. **Private Subnet に配置**
   - アプリケーションリソースは必ずPrivate Subnet(subnet-0ebcb5a9bc54d1bd1 または subnet-03fa4782f20ba49ec)に配置
   - Public Subnet(subnet-0aa985c78e9b67381)には配置しない
2. **Security Group の使用**
   - 必ず `sg-0faff8d9c30c94dc0` を割り当てる
   - カスタムSecurity Groupを作る場合も、HTTPS (443) アウトバウンドルールを設定
3. **パブリックIP の割り当て禁止**
   - EC2インスタンス、ECS TaskにパブリックIPを割り当てない
   - 必ずNAT Gatewayを経由させる
4. **APIキーの取得**
   - Secrets Managerから動的に取得する
   - コード内にハードコードしない
   - 環境変数や設定ファイルにも直接記載しない

### ❌ やってはいけないこと

- ❌ NAT Gatewayの削除(固定IPが失われます)
- ❌ Elastic IPの解放(固定IPが変わります)
- ❌ Public Subnetへのアプリケーション配置
- ❌ APIキーのコード内ハードコード
- ❌ Security Groupの不必要な変更

---

## 📊 リソース一覧(クイックリファレンス)

```json
{
  "vpc_id": "vpc-08d84efb87d052cf9",
  "vpc_cidr": "10.0.0.0/16",
  "internet_gateway_id": "igw-0ef94f264e55288d1",
  "public_subnet_id": "subnet-0aa985c78e9b67381",
  "public_subnet_cidr": "10.0.0.0/24",
  "private_subnet_a_id": "subnet-0ebcb5a9bc54d1bd1",
  "private_subnet_a_cidr": "10.0.10.0/24",
  "private_subnet_a_az": "ap-northeast-1a",
  "private_subnet_c_id": "subnet-03fa4782f20ba49ec",
  "private_subnet_c_cidr": "10.0.11.0/24",
  "private_subnet_c_az": "ap-northeast-1c",
  "nat_gateway_id": "nat-0355c595db360d105",
  "elastic_ip": "52.68.152.34",
  "elastic_ip_allocation_id": "eipalloc-07f0d7325b014b8e2",
  "public_route_table_id": "rtb-041ce0664475071ea",
  "private_route_table_id": "rtb-04dc674fc8fbc4d42",
  "security_group_id": "sg-0faff8d9c30c94dc0",
  "security_group_name": "nhk-ai-platform-access-sg",
  "secrets_manager_arn": "arn:aws:secretsmanager:ap-northeast-1:903877990773:secret:nhk_ai_api_key_lite_llm-Yymln7",
  "region": "ap-northeast-1",
  "account_id": "903877990773",
  "fixed_ip": "52.68.152.34"
}
```