Closed6

Claude Codeを触ってみる

horie-thorie-t

準備

Claudeでアカウントを作って一度ログインしておく。(チャットとAPIの2つにログインしておく)

インストール

いきなりエラー。

$ npm install -g @anthropic-ai/claude-code
npm error code EACCES
npm error syscall mkdir
npm error path /usr/local/lib/node_modules/@anthropic-ai
npm error errno -13
npm error Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/@anthropic-ai'
npm error     at async mkdir (node:internal/fs/promises:858:10)
npm error     at async /usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:638:20
npm error     at async Promise.allSettled (index 0)
npm error     at async [reifyPackages] (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:336:11)
npm error     at async Arborist.reify (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:141:5)
npm error     at async Install.exec (/usr/local/lib/node_modules/npm/lib/commands/install.js:149:5)
npm error     at async Npm.exec (/usr/local/lib/node_modules/npm/lib/npm.js:208:9)
npm error     at async module.exports (/usr/local/lib/node_modules/npm/lib/cli/entry.js:67:5) {
npm error   errno: -13,
npm error   code: 'EACCES',
npm error   syscall: 'mkdir',
npm error   path: '/usr/local/lib/node_modules/@anthropic-ai'
npm error }
npm error
npm error The operation was rejected by your operating system.
npm error It is likely you do not have the permissions to access this file as the current user
npm error
npm error If you believe this might be a permissions issue, please double-check the
npm error permissions of the file and its containing directories, or try running
npm error the command again as root/Administrator.
npm error A complete log of this run can be found in: /home/tetsuya/.npm/_logs/2025-05-03T01_02_17_886Z-debug-0.log

多分、Ubuntuのapt-getでインストールしているからかな。

Claude Codeのトラブルシューティングに従う。

npm list -g --depth=0 > ~/npm-global-packages.txt
mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
npm install -g @anthropic-ai/claude-code

インストールし直したいパッケージを確認。

$ cat npm-global-packages.txt 
/usr/local/lib
├── @aws-amplify/cli@10.3.0
├── n@7.2.2
├── npm@11.3.0
├── serverless@3.14.0
└── wscat@4.0.1
horie-thorie-t

起動

適当なgitrリポジトリに移動してclaudeを起動する

cd repo/<Gitリポジトリ>
claude

ダークモードを選択

APIを選択。Maxは最初から使うには高すぎる。

ブラウザが起動して以下の画面が表示される

クレジットの購入が求められる。

クレジットカードと住所の入力を求められる。

ログインの成功やセキュリティに関する表示があるのでEnterキーで進む。

horie-thorie-t

Claudeの実行

/init を実行するとCLAUDE.mdを生成しようとする。 yesを選択して生成させる。

CLAUDE.mdをコミットしてください。

と依頼すると確認を求められながら、コミットできる。

horie-thorie-t

EKSのクラスタを作成

terraformディレクトリにEKSのクラスタを作成するコードを書いてください。EKSはFargetを使って、リージョンはus-west-2 に作成してください。Terraform AWS modulesを使用してください。

と依頼すると以下のファイルを生成

terraform/
├── eks.tf
├── main.tf
├── outputs.tf
├── variables.tf
└── vpc.tf

中身を確認しても問題なさそう。

Terraformでplanを実行してください。

● Terraform planが正常に実行されました。計画によると、以下のリソースが作成される予定です:

  1. AWS EKSクラスター (リージョン: us-west-2)
  2. Fargateプロファイル (default と applications)
  3. VPCとサブネット (プライベートサブネット、パブリックサブネット)
  4. セキュリティグループ
  5. IAMロールとポリシー
  6. KMSキー (クラスタ暗号化用)

  計59リソースが追加される予定です。これらはすべて、指定された要件に従ってFargateを使用するEKSクラスターをプロビジ
  ョニングするために必要なリソースです。

  適用したい場合は「terraform apply」を実行することができます。

流石にplanの中身までは確認できない。

別ターミナルで確認するとちょっと警告が出る程度。

$ terraform plan
module.eks.module.kms.data.aws_caller_identity.current[0]: Reading...
module.eks.data.aws_caller_identity.current: Reading...
# 中略
╷
│ Warning: Argument is deprecated
│ 
│   with module.eks.aws_iam_role.this[0],
│   on .terraform/modules/eks/main.tf line 293, in resource "aws_iam_role" "this":
│  293: resource "aws_iam_role" "this" {
│ 
│ inline_policy is deprecated. Use the aws_iam_role_policy resource instead. If Terraform should exclusively manage all
│ inline policy associations (the current behavior of this argument), use the aws_iam_role_policies_exclusive resource
│ as well.
╵

Terraformをapplyしてください。

● TerraformのApplyを実行します。

● Bash(cd /home/johndoe/repo/lean-saas-tech/terraform && terraform apply -auto-approve)…
  ⎿  Error: Command timed out after 2m 0.0s
     module.eks.data.aws_partition.current: Reading...
     module.eks.data.aws_caller_identity.current: Reading...
     module.eks.module.kms.data.aws_caller_identity.current[0]: Reading...
     module.eks.module.kms.data.aws_partition.current[0]: Reading...
     module.eks.data.aws_partition.current: Read complete after 0s [id=aws]
     module.eks.module.kms.data.aws_partition.current[0]: Read complete after 0s [id=aws]
     module.eks.data.aws_iam_policy_document.assume_role_policy[0]: Reading...
     module.eks.data.aws_iam_policy_document.assume_role_policy[0]: Read complete after 0s [id=2764486067]
     module.eks.module.fargate_profile["applications"].data.aws_partition.current: Reading...
     … +117 lines (ctrl+r to see all)

● Terraformのapplyコマンドが2分のタイムアウトに達しました。これは、EKSクラスターやVPCのNATゲートウェイなどのリソー
  スの作成に時間がかかるためです。タイムアウトを延長して再度実行してみましょう。

● Bash(cd /home/johndoe/repo/lean-saas-tech/terraform && terraform apply -auto-approve)…

タイムアウトを起こすんだ・・・

Terraformをapplyをタイムアウトと10分にして実行してください。

● タイムアウトを10分に設定してTerraform applyを実行します。

● Bash(cd /home/tetsuya/repo/lean-saas-tech/terraform && terraform apply -auto-approve)…
  ⎿  Error: ╷
     │ Error: creating EKS Cluster (lean-saas-eks): operation error EKS: CreateCluster, https response error 
     StatusCode: 409, RequestID: 9c0aac4d-c751-4e84-a39c-647f2cb0fa99, ResourceInUseException: Cluster already 
     exists with name: lean-saas-eks
     │
     │   with module.eks.aws_eks_cluster.this[0],
     │   on .terraform/modules/eks/main.tf line 25, in resource "aws_eks_cluster" "this":
     │   25: resource "aws_eks_cluster" "this" {
     │
     ╵
     ╷
     │ Error: waiting for EC2 NAT Gateway (nat-0abca33405cf7bca8) create: unexpected state 'failed', wanted target 
     'available'. last error: Resource.AlreadyAssociated: Elastic IP address [eipalloc-06db0c1a2d4a6b0f0] is alread
      associated
     … +145 lines (ctrl+r to see all)

● Terraformのapply実行中にエラーが発生しました。主に以下の2つのエラーが表示されています:

  1. EKSクラスターのエラー: Cluster already exists with name: lean-saas-eks
    - すでに同じ名前のEKSクラスターが存在するというエラーです。
  2. NATゲートウェイのエラー: Elastic IP address is already associated
    - Elastic IPアドレスがすでに関連付けられているというエラーです。

  これは前回の実行で一部のリソースがすでに作成されているために発生しています。Terraformの状態を更新するために、まず
  リソースの状態を確認してから再実行しましょう。

  Bash(cd /home/tetsuya/repo/lean-saas-tech/terraform && terraform refresh)…

前の実行状態が中途半端な状態で残っているみたい。

Terraformのリソースの状態を確認してください。

● Terraformのリソースの状態を確認します。

● Bash(cd /home/tetsuya/repo/lean-saas-tech/terraform && terraform state list)…
  ⎿  module.eks.data.aws_caller_identity.current
     module.eks.data.aws_iam_policy_document.assume_role_policy[0]
     module.eks.data.aws_iam_session_context.current
     module.eks.data.aws_partition.current
     module.eks.aws_cloudwatch_log_group.this[0]

     … +52 lines (ctrl+r to expand)

     module.eks.module.kms.data.aws_caller_identity.current[0]
     module.eks.module.kms.data.aws_iam_policy_document.this[0]
     module.eks.module.kms.data.aws_partition.current[0]
     module.eks.module.kms.aws_kms_alias.this["cluster"]
     module.eks.module.kms.aws_kms_key.this[0]

● 多くのリソースが既に作成されていますが、まだいくつかのリソースが不足しています。特に、EKSクラスター自体とFargate
  プロファイルはまだ完全に作成されていないようです。もう少し詳細な情報を確認しましょう。

  Bash(cd /home/tetsuya/repo/lean-saas-tech/terraform && terraform state show 'module.eks.aws_eks_cluster.this[0]'…
      || echo "EKSクラスターはまだ作成されていません")
  ⎿  Running…
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Bash command                                                                                                         │
│                                                                                                                      │
│   terraform state show 'module.eks.aws_eks_cluster.this[0]' || echo "EKSクラスターはまだ作成されていません"          │
│   EKSクラスターの状態を確認                                                                                          │
│                                                                                                                      │
│ Do you want to proceed?                                                                                              │
│ ❯ 1. Yes                                                                                                             │
│   2. No, and tell Claude what to do differently (esc)                                                                │
│                                                                                                                      │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

確認が続くので、不足分を作ってもらう

現在の状態を取り込んで、不足分を作成してください。

● プランを見ると、EKSクラスターが置き換えられる予定(must be replaced)であることが分かります。また、NATゲートウェイも問題があるようです。これらを修正して、安全にリソースを適用するために、個別に処理します。

  まず、NATゲートウェイの問題を解決します:

ちょっと色々、ハマっているので、人手で介入する事にする。

まずは中途半端にできてしまったものを削除するため、別ターミナルで以下を実行して作成されたものを削除する。

terraform destroy

この時点で課金額をBillingの画面で確認すると、1ドル程度だった。Logsの画面でAPIのログを見るとかなりの回数の呼び出しがあってTokenが消費されている。

出力の多いコマンドの実行はTokenを消費するから、横着せずにコマンドを質問してシェルから実行した方が良さそうです。

削除後に手動でapplyを実行したらエラーが発生。

$ erraform apply
# 中略
╷
│ Error: Have got the following error while validating the existence of the ConfigMap "aws-auth": Get "http://localhost/api/v1/namespaces/kube-system/configmaps/aws-auth": dial tcp 127.0.0.1:80: connect: connection refused
│ 
│   with module.eks.kubernetes_config_map_v1_data.aws_auth[0],
│   on .terraform/modules/eks/main.tf line 562, in resource "kubernetes_config_map_v1_data" "aws_auth":
│  562: resource "kubernetes_config_map_v1_data" "aws_auth" {
│ 
╵

こちらの問題が発生した。

一旦、destroyしてから、eksモジュールのバージョンを最新版に上げて、 eks.tfファイルを以下のように編集する。

    # Configure AWS Auth for Fargate
-   manage_aws_auth_configmap = true
+   enable_cluster_creator_admin_permissions = true

その後、クラスタを再作成するがエラー。

$ terraform init
$terraform apply
kubernetes_namespace.applications: Creating...
╷
│ Error: Post "http://localhost/api/v1/namespaces": dial tcp 127.0.0.1:80: connect: connection refused
│ 
│   with kubernetes_namespace.applications,
│   on eks.tf line 57, in resource "kubernetes_namespace" "applications":
│   57: resource "kubernetes_namespace" "applications" {
│ 
╵

こちらを参考にして、main.tfに以下の記述を追加。

provider "kubernetes" {
  host                   = module.eks.cluster_endpoint
  cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
  exec {
    api_version = "client.authentication.k8s.io/v1beta1"
    args        = ["eks", "get-token", "--cluster-name", var.cluster_name]
    command     = "aws"
  }
}

再度、applyすると成功した。

kubectlのconfigを更新する。

aws eks update-kubeconfig --region us-west-2 --name lean-saas-eks

クラスタの状態を取得する。

$ kubectl get nodes
No resources found
$ kubectl get namespaces 
NAME              STATUS   AGE
applications      Active   30m
default           Active   58m
kube-node-lease   Active   58m
kube-public       Active   58m
kube-system       Active   58m

applicationのnamespaceが作成されている。

horie-thorie-t

コミット作業

ここまでの成果物をコミットする

Terraformのmodule類をコミットしないようにしてください。

色々と問い合わせに答えていると、最終的にコミットまでしてくれた。

horie-thorie-t

Podのデプロイ

$ kubectl create -f sample-pod.yaml 
pod/sample-pod created
$ kubectl get pods 
NAME         READY   STATUS    RESTARTS   AGE
sample-pod   0/1     Pending   0          4s

いくら待ってもPendingのまま。

$ kubectl delete -f sample-pod.yaml 
$ kubectl get pods -A
NAMESPACE     NAME                             READY   STATUS    RESTARTS   AGE
kube-system   coredns-7864b4f64c-94x42         0/1     Pending   0          6h19m
kube-system   coredns-7864b4f64c-fg7w7         0/1     Pending   0          6h19m

CoreDNSが立ち上がっていないし、ちゃんとセットアップできていないみたい。

このスクラップは2025/05/16にクローズされました