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

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

https://ithelp.ithome.com.tw/upload/images/20201022/20117701YfzE0FyUyP.jpg

建立我的第一個 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 [email protected]: 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 [email protected]: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated [email protected]: this library is no longer supported
npm WARN deprecated [email protected]: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated [email protected]: Please see https://github.com/lydell/urix#deprecated
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No repository field.
npm WARN [email protected] 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://ithelp.ithome.com.tw/upload/images/20200917/20117701Yh3Af7atRI.png

建議安裝 AWS Toolkit https://ithelp.ithome.com.tw/upload/images/20200917/20117701ecG9hcHP1W.png

資料夾檔案介紹

https://ithelp.ithome.com.tw/upload/images/20200917/201177016yYXkWkYSu.png

  • 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