Clarence Lin
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: AWS 初心者 - 用 AWS CDK 我也可以一鍵上傳前端網站 tags: AWSUG slideOptions: theme: league transition: 'slide' --- ## 用 AWS CDK 我也可以一鍵上傳前端網站 `---` AWS User Group Taiwan Meetup 2021-12 線上 / 下小聚 --- ## Clarence Lin - 《AWS CDK 完全學習手冊》作者 - 第 12 屆 iT 邦幫忙鐵人賽 DevOps 組冠軍。 - 現職 * 長問科技雲端技術資深工程師 * 田茂智能互聯雲端解決方案技術顧問 - Twitter https://twitter.com/clarence_lin - 部落格 https://blog.clarence.tw - Facebook https://fb.me/ClarenceTaiwan --- ## AWS CDK 完全學習手冊 書籍特色 * 中文書籍 * 一步一腳印 * 高實用性的工具書 * 自動化部署 LAMP(Linux、Apache、MariaDB、PHP)叢集 * 在 AWS 上部署 Docker 容器解決方案 * 開發自動部署的 CDK Construct Library --- ## 簡介 * 帶初心者從 0 到 1 認識 AWS CDK,並且教你使用 CDK 一鍵上傳前端網站,體驗基礎架構程式碼 (IaC) 的神奇魔法把靜態網站部署上 S3,歡迎大家一起討論。 --- ### AWS CDK 簡介 ![](https://i.imgur.com/mpIc7Cz.png =500x) 目前支援語言 TypeScript, JavaScript, Python, Java, C#/.Net 還有正在 preview 的 Go --- ### 為什麼要使用 AWS CDK 如下 20 行的 CDK 可以變出高達 500 行的 CloudFormation 腳本 ```typescript= export class MyEcsConstructStack extends core.Stack { constructor(scope: core.App, id: string, props?: core.StackProps) { super(scope, id, props); const vpc = new ec2.Vpc(this, "MyVpc", { maxAzs: 3 // 使用 3 個 AZ }); const cluster = new ecs.Cluster(this, "MyCluster", { vpc: vpc }); // 建立 ALB 串接 Fargate 的 ECS Service new ecs_patterns.ApplicationLoadBalancedFargateService(this, "MyFargateService", { cluster, taskImageOptions: { image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample") }, publicLoadBalancer: true // 預設為 false 僅內部使用 ALB }); } } ``` ---- ### 為什麼要使用 AWS CDK 此程式定義了 50 種資源 * AWS::EC2::EIP * AWS::EC2::InternetGateway * AWS::EC2::NatGateway * AWS::EC2::Route * AWS::EC2::RouteTable * AWS::EC2::SecurityGroup * AWS::EC2::Subnet * AWS::EC2::SubnetRouteTableAssociation ---- ### 為什麼要使用 AWS CDK 此程式定義了 50 種資源 * AWS::EC2::VPCGatewayAttachment * AWS::EC2::VPC * AWS::ECS::Cluster * AWS::ECS::Service * AWS::ECS::TaskDefinition * AWS::ElasticLoadBalancingV2::Listener * AWS::ElasticLoadBalancingV2::LoadBalancer * AWS::ElasticLoadBalancingV2::TargetGroup * AWS::IAM::Policy * AWS::IAM::Role * AWS::Logs::LogGroup ---- ### 為什麼要使用 AWS CDK 超好用的自動補完提示 ![](https://i.imgur.com/J4XGjLC.png =550x) --- ### AWS CDK 版本 - 目前同步更新 v1 與 v2 - CDK v1 在 2022/06/01 進入維護模式,之後只會修復 BUG 與進行安全更新 - CDK v1 最後支援到 2023/06/01 --- ### 安裝 CDK 必要指令 * AWS CLI: ```bash= python3 -m pip install --no-input --upgrade awscli ``` * AWS CDK CLI: ```bash= npm install -g aws-cdk ``` --- ### 設定 AWS CLI ```bash= $ aws configure AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY Default region name [None]: us-west-2 Default output format [None]: ``` ---- ### 測試 AWS CLI 是否有設定正常 ``` $ aws sts get-caller-identity { "UserId": "AKIAIOSFODNN7EXAMPLE", "Account": "888888888888", "Arn": "arn:aws:iam::888888888888:user/clarence" } ``` ---- ### CDK CLI 指令 - 列出所有的 stack:`cdk list` aka `cdk ls` - 列出 CDK 專案的 CloudFormation 腳本:`cdk synthesize` aka `cdk synth` - `cdk synth --json` - 部署:CDK toolkit stack:`cdk bootstrap` - 部署指令:`cdk deploy` - 即時監看指令:`cdk watch` - 移除部署:`cdk destroy` - 部署比對:`cdk diff` - 列出 CDK docs:`cdk docs` ---- ### CDK CLI 指令 - 創建 CDK 專案 `cdk init` - 列出創建專案可使用的指令 `cdk init --list` - `cdk init -l typescript` - `cdk init lib` ---- ### CDK CLI 部署指令 - 關閉 approval `--require-approval=never` - 指定當前的 Region `AWS_DEFAULT_REGION=us-west-2 cdk deploy` --- ### CDK 參考文件 - https://docs.aws.amazon.com/zh_tw/cdk/latest/guide/home.html - https://github.com/aws-samples/aws-cdk-examples - https://constructs.dev - https://www.youtube.com/PahudDev - https://www.ernestchiang.com/zh/notes/aws/cdk/ --- ### 創建專案 #### 使用 CDK CLI ```bash= mkdir hello-cdk && cd hello-cdk cdk init --language=typescript ``` #### 使用 projen ```bash= mkdir hello-cdk && cd hello-cdk npx projen new awscdk-app-ts ``` ---- ### 創建專案 #### 使用 projen 指令版本 ```bash= mkdir hello-cdk && cd hello-cdk npx projen new awscdk-app-ts --cdk-version 2.1.0 ``` --- ### 比較 CDK v1 與 v2 import 差別 #### CDK v1 ```typescript= import * as s3 from '@aws-cdk/aws-s3'; import * as s3deploy from '@aws-cdk/aws-s3-deployment'; ``` #### CDK v2 ```typescript= aws_s3 as s3 aws_s3_deployment as s3deploy ``` --- ## Workshop https://github.com/clarencetw/gitpod-cdk-web

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully