Day 2 – 第一個 AWS CDK 專案與參考資源分享

2020 12th 鐵人賽

昨天談完了為什麼想要學習 AWS CDK,今天就來建立屬於自己的第一個 AWS CDK 專案吧! 建立完成後跟大家談談學習 AWS CDK 有什麼資源可以看吧!

https://i2.wp.com/ithelp.ithome.com.tw/upload/images/20201022/20117701YfzE0FyUyP.jpg?w=640&ssl=1

建立我的第一個 AWS CDK 專案

安裝指令

  • 確認 NodeJS
$ node -v
v14.8.0

Node.js 版本需求 ≥ 10.13.0

  • 確認 NPM
$ npm -v
6.14.8
$ npm i -g aws-cdk
  • 建立專案資料夾
$ mkdir hello-cdk && cd hello-cdk
  • 初始化專案
$ cdk init sample-app --language=typescript

Applying project template sample-app for typescript
# Welcome to your CDK TypeScript project!

You should explore the contents of this project. It demonstrates a CDK app with an instance of a stack (`HelloCdkStack`)
which contains an Amazon SQS queue that is subscribed to an Amazon SNS topic.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

## Useful commands

 * `npm run build`   compile typescript to js
 * `npm run watch`   watch for changes and compile
 * `npm run test`    perform the jest unit tests
 * `cdk deploy`      deploy this stack to your default AWS account/region
 * `cdk diff`        compare deployed stack with current state
 * `cdk synth`       emits the synthesized CloudFormation template

Initializing a new git repository...
Executing npm install...
npm WARN deprecated request-promise-native@1.0.9: request-promise-native has been deprecated because it extends the now deprecated request package, see https://github.com/request/request/issues/3142
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN hello-cdk@0.1.0 No repository field.
npm WARN hello-cdk@0.1.0 No license field.

✅ All done!

指令詳細介紹:

  • 建立專案:cdk init sample-app –language=typescript
  • 初始化模板 sample-app:cdk init sample-app –language=typescript
  • 語言 typescript:sample-app cdk init sample-app –language=typescript

開啟專案

$ code .

https://i2.wp.com/ithelp.ithome.com.tw/upload/images/20200917/20117701Yh3Af7atRI.png?w=640&ssl=1

建議安裝 AWS Toolkit https://i1.wp.com/ithelp.ithome.com.tw/upload/images/20200917/20117701ecG9hcHP1W.png?w=640&ssl=1

資料夾檔案介紹

https://i2.wp.com/ithelp.ithome.com.tw/upload/images/20200917/201177016yYXkWkYSu.png?w=640&ssl=1

  • lib/cdk-workshop-stack.ts:CDK 主要的程式位置
  • bin/cdk-workshop.ts:主要的程式進入點預設會引用 lib/cdk-workshop-stack.ts
  • package.json:npm 模組清單裡面定義了套件的版本與指令,例如:buildwatchtestcdk
  • cdk.json:告訴 toolkit 如何執行 app
  • tsconfig.json:typescript 設定檔
  • .gitignore:告訴 git 應該要排除的文件
  • .npmignore:告訴 npm 應該要排除的文件
  • node_modules:nodejs 套件包執行完 npm install 後的文件都會安裝在此資料夾裡面
  • test:CDK 測試的程式位置

參考資源

  • AWS 文件 首先要跟大家說明的是 AWS 文件,文件會從 What is the AWS CDK? 開始介紹,目前只有英文介紹可能會比較深澀一點,後面的文章如果有需要會引用裡面的內容來說明

  • AWS CDK Workshop AWS CDK Workshop 內容是一個 Workshop 會從 Lambda、API Gateway、DynamoDB、CloudWatch 一步步教你如何做出一個簡單的 Web 服務,最後會說明怎麼把剛剛建立的服務給移除

  • AWS CDK Github AWS CDK 為一個 Open Source Project 因此所有的程式碼都可以從這邊找到,如果遇到了問題也可以直接發 issue 給官方詢問,有新的想法或是 BUG 修復也可以直接發送 PR

  • Pahud Dev Youtube 頻道 AWS CDK 影片目前有 11 支 4K 高畫質影片不是文字派就看影片吧! Pahud – Developer Advocate – AWS