iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 2
1
DevOps

用 CDK 定義 AWS 架構系列 第 2

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 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://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

想要看更多

歡迎到 Clarence 部落格 參觀,文章內容主要是網路或是程式開發類型文章分享

本文同步刊載於 Clarence 部落格:Day 2 - 第一個 AWS CDK 專案與參考資源分享


「AWS CDK 完全學習手冊:打造雲端基礎架構程式碼 IaC」
本書改編並延伸自第 12 屆 iT 邦幫忙鐵人賽獲得 DevOps 組冠軍的《用 CDK 定義 AWS 架構》系列文章,以簡單、好讀的行文風格詳述技術細節,並提供完整的程式碼範例與說明,一步一步帶領新手從零開始踏上 AWS CDK 技術達人之路。

有興趣的朋友歡迎至天瓏書局選購!

購書連結 https://bit.ly/2ZMAebE

https://ithelp.ithome.com.tw/upload/images/20211103/20117701W7l6fQnn2L.jpg


上一篇
Day 1 - 一起學習 AWS Cloud Development Kit (AWS CDK)
下一篇
Day 3 - AWS CDK 指令介紹
系列文
用 CDK 定義 AWS 架構30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言