M1 MacでAWS CDK環境を整備する

# このドキュメントは
Mac上でCDK環境を整備する方法をまとめたものです

# 前提条件
使用PC

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/930724/797e253e-faa1-c0c4-5231-e1acba9c0bc2.png)


# 手順
## homebrewでaws-cdkをインストールする

```bash
% brew install aws-cdk  
==> Downloading https://ghcr.io/v2/homebrew/core/node/manifests/17.4.0
Already downloaded: /Users/mukai/Library/Caches/Homebrew/downloads/794ea4ad96ad44757653ec3839632a88467cde669de3305f9d5f12ce1bd81769--node-17.4.0.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/node/blobs/sha256:1954cb0ba19e7426054249eda70dff8bda197a960f4e0524af717245faa13529
Already downloaded: /Users/mukai/Library/Caches/Homebrew/downloads/66eb66783323a07fdee08e1e680a0c3cf4081301b4b5e2c706aaacadcbc01c5f--node--17.4.0.arm64_monterey.bottle.tar.gz
==> Downloading https://ghcr.io/v2/homebrew/core/aws-cdk/manifests/2.8.0
Already downloaded: /Users/mukai/Library/Caches/Homebrew/downloads/86f67ab001382e06f746a2028f74cd1b456fd42205fd9027bdc334462758c5cf--aws-cdk-2.8.0.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/aws-cdk/blobs/sha256:d3b1acac013ab7e7d72dbca9e4c2e475d265eb0b7c4fcaeb1c19df92582bb98b
Already downloaded: /Users/mukai/Library/Caches/Homebrew/downloads/2059e2a9adac14728af21d7f8d0328c3ddd5c329ec2d57c6a2db505e93197acf--aws-cdk--2.8.0.arm64_monterey.bottle.tar.gz
==> Installing dependencies for aws-cdk: node
==> Installing aws-cdk dependency: node
==> Pouring node--17.4.0.arm64_monterey.bottle.tar.gz
🍺  /opt/homebrew/Cellar/node/17.4.0: 1,984 files, 44.5MB
==> Installing aws-cdk
==> Pouring aws-cdk--2.8.0.arm64_monterey.bottle.tar.gz
🍺  /opt/homebrew/Cellar/aws-cdk/2.8.0: 9,755 files, 174.9MB
==> Running `brew cleanup aws-cdk`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
```

## バージョン確認

正常に動いてはいるが、依存関係でインストールされたnodeのバージョンが新しいのでCDKは動作未確認とのこと。

```bash
% cdk --version
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!                                                                                  !!
!!  This software has not been tested with node v17.4.0.                            !!
!!  You may to encounter runtime issues, and should switch to a supported release.  !!
!!                                                                                  !!
!!  As of the current release, supported versions of node are:                      !!
!!  - ^12.7.0                                                                       !!
!!  - ^14.5.0                                                                       !!
!!  - ^16.3.0                                                                       !!
!!                                                                                  !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2.8.0 (build 8a5eb49)
```

## nodeのダウングレード

cdkのコマンドを叩くたびにいちいちコーションが出るのは嫌なのでnodeをダウングレードする。
Homebrewでインストールされたnodeをアンインストールし、nodebrewでnodeを改めてインストールする。

```bash
% brew uninstall node  
Error: Refusing to uninstall /opt/homebrew/Cellar/node/17.4.0
because it is required by aws-cdk, which is currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies node
```

aws-cdkと依存関係があるのでアンインストールできないらしい。
指示に従って、オプションをつけてリトライ

```bash
% brew uninstall --ignore-dependencies node
Uninstalling /opt/homebrew/Cellar/node/17.4.0... (1,984 files, 44.5MB)
```

できた。続いて、nodebrewインストール。

```bash
% brew install nodebrew  
==> Downloading https://ghcr.io/v2/homebrew/core/nodebrew/manifests/1.1.0-1
Already downloaded: /Users/mukai/Library/Caches/Homebrew/downloads/f5786fdcabaf7a91171f3d803ecbb7b8d5d0fd7bd5f9f13a0fda7b7722b46af8--nodebrew-1.1.0-1.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/nodebrew/blobs/sha256:f9e68ad3b92827534fc9faf5d7b9c4d1fe61e4e1fa11a99e03c6cc476593fe09
Already downloaded: /Users/mukai/Library/Caches/Homebrew/downloads/e570150909b477bae65e749b152e5d87aa71158439019f8e2309456f25cf9993--nodebrew--1.1.0.all.bottle.1.tar.gz
==> Pouring nodebrew--1.1.0.all.bottle.1.tar.gz
==> Caveats
You need to manually run setup_dirs to create directories required by nodebrew:
  /opt/homebrew/opt/nodebrew/bin/nodebrew setup_dirs

Add path:
  export PATH=$HOME/.nodebrew/current/bin:$PATH

To use Homebrew's directories rather than ~/.nodebrew add to your profile:
  export NODEBREW_ROOT=/opt/homebrew/var/nodebrew

zsh completions have been installed to:
  /opt/homebrew/share/zsh/site-functions
==> Summary
🍺  /opt/homebrew/Cellar/nodebrew/1.1.0: 8 files, 39KB
==> Running `brew cleanup nodebrew`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
```

パスを通せと書かれているので、`~/.zshrc`に以下を追記

```sh
# nodebrew
export PATH=$HOME/.nodebrew/current/bin:$PATH
```

設定を反映する

```bash
source ~/.zshrc
```

今回はcdkも検証が完了しているv16.3.0を使うことにする

```bash
% nodebrew install-binary v16.3.0
Fetching: https://nodejs.org/dist/v16.3.0/node-v16.3.0-darwin-arm64.tar.gz
######################################################################################################################################################################################################################### 100.0%
Installed successfully
% nodebrew ls                    
v16.3.0

current: none
% nodebrew use v16.3.0
use v16.3.0
% node -v
v16.3.0
```

もう一度、cdkのバージョン確認コマンドを打鍵し、コーションが出ないか確認する。

```bash
% cdk --version
2.8.0 (build 8a5eb49)
```

問題なしなのでこれで完了