KarWei
    • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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
    • Make a copy
    • 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 Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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: '20 AWS Serverless Application Model (SAM)' disqus: hackmd --- :::info AWS Certified Developer Associate DVA-C01 ::: 20 AWS Serverless Application Model (SAM) === <style> img{ /* border: 2px solid red; */ margin-left: auto; margin-right: auto; width: 90%; display: block; } </style> ## Table of Contents [TOC] AWS SAM --- - serverless app model (sam) - framework for developing and deploying serverless apps - all config in yaml - generate complex cloudformation from simple SAM YAML file - supports anything from cloudformation - outputs, mappings, params, res etc. - only 2 commands to deploy to aws - sam can use codedeploy to deploy lambda funcs - sam can help you run lambda, api gateway, dynamodb locally - dont need to deploy lambda func to test it ### What it Looks Like - transform header indicating SAM template - `Transform: 'AWS::Serverless-2016-10-31'` - write code - `AWS::Serverless::Function` - lambda - `AWS::Serverless::Api` - api gateway - `AWS:Serverless::SimpleTable` - dynamodb - package and deploy - `aws cloudformation package / sam package` - `aws cloudformation deploy / sam deploy` ### SAM Deployment ![](https://i.imgur.com/QOVDssI.png) - aws cloudformation package - upload code zip file to s3 - also transforms sam template into cloudformation template - generated templates will have a reference to s3 - aws cloudformation deploy - create and execute a change set - change set is figuring out how cloudformation shld take its existing state and move it to the next state based on modifications generated - cloudformation then applies it to our stack - stack may comprise of all services ### SAM Policy Templates - list of templates to apply perms to your lambda funcs - full list [here](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html) - important examples - `S3ReadPolicy` - gives read only perms to objs in s3 - `SQSPollerPolicy` - allows to poll on sqs queue - `DynamoDBCrudPolicy` - create read update delete ![](https://i.imgur.com/aHrfWlq.png) - instead of creating iam role, attach policy in sam template instead ### SAM with CodeDeploy - sam framework natively uses codedeploy to update lambda functions - leverage traffic shifting feature using aliases - can also define pre and post traffic hooks features to validate deployment - before traffic shift starts and after it ends - easy & automated rollback using cloudwatch alarms ![](https://i.imgur.com/Qn0Lu3M.png) - trigger deployment in codedeploy - runs pre traffic hook test using another lambda func - optional - do traffic shifting with alias - then monitor cloudwatch alarm - optional - ensure everything goes well during deployment - once deployment and traffic shifting done, run post-traffic hook lambda func - also optional - runs some tests on your alias - if everything goes well, v1 func of alias goes away - only left with v2 ### Summary - sam is built on cloudformation - sam requires `Transform` and `Resources` section - commands to know - `sam build` - fetch dependencies and create local deployment artifacts - `sam package` - package and upload to amazon s3 - generate CF template - `sam deploy` - deploy to cloudformation - sam policy templates for easy iam policy definition - sam is integrated with codedeploy to do deploy to lambda aliases ### Console - https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install-windows.html - follow this - windows just download a sam installer - use brew for mac and linux #### Init Project - use `sam init` to generate a sam template for your specific runtime - but can also create from scratch on your own ![](https://i.imgur.com/atXek0f.png) - can go to sam examples github for dummy code - https://github.com/aws/serverless-application-model ![](https://i.imgur.com/jxYiTII.png) - example template yaml file - `Transform` means sam template - resource list - 1st res is function - handler shld be `<file name>.<function name>` - codeuri shld point to dir with the code ![](https://i.imgur.com/jQruvMr.png) - create cli commands to generate your transformed cloudformation template - first create s3 bucket - next upload code and do transformation from aws cloudformation package - can also use `sam package` since sam is just shorthand for aws cloudformation ![](https://i.imgur.com/6YxSIcZ.png) - generated template - codeuri now points to s3 ![](https://i.imgur.com/nqGKlgq.png) - running package will also output a sample deploy cli command ![](https://i.imgur.com/hBaZrC0.png) ![](https://i.imgur.com/IVO9jSz.png) - failed as didnt add `CAPABILITY_IAM` into your cli cmd ![](https://i.imgur.com/jqbWB4y.png) ![](https://i.imgur.com/9Zd7nUK.png) - once deployed, can go to cloudformation to check your stack out #### Adding API Gateway - look for api gateway in the examples github for ref ![](https://i.imgur.com/N9rf4bB.png) - example lambda func for api gateway demo ![](https://i.imgur.com/dXZKo8b.png) - add events segment in lambda func to create new api - func is invoked everytime a get req is called to /hello - rerun your cloudformation commands to create the new api gateway ![](https://i.imgur.com/u1lhSGe.png) - more resources created for your cloudformation - like api gateway and iam roles #### Adding DynamoDB - look for dynamodb example in github - actually prev api gateway example alr used dynamodb so we'll just use that ![](https://i.imgur.com/13uN9uS.png) - example lambda func for adding of dynamodb - get region from os environment vars ![](https://i.imgur.com/paRhsB6.png) - read doc for full list of properties u can pass into your simpletable segment - set provisioned throughput to save costs for this demo ![](https://i.imgur.com/JBKPjvr.png) - add your simpletable section in the yaml file ![](https://i.imgur.com/wQhc5xn.png) - can add env vars in your yaml template - table name refs the simpletable created - region refs a pseudo param `AWS::Region` ![](https://i.imgur.com/wAbC2eb.png) - rmb to add iam policy to your func - this basically gives a dynamodbcrudpolicy with ref to the table created to your lambda func ![](https://i.imgur.com/sIY8ccW.png) - sample function scans the dynamodb and returns it #### CloudFormation Designer and App Repository ![](https://i.imgur.com/lINuNhR.png) - can see details of what was created in cloudformation from viewing the stack - pic above shows the template code ![](https://i.imgur.com/ZFj1Zw2.png) - can go actions > view designer ![](https://i.imgur.com/qdOeHCE.png) - high lvl view of what was created ![](https://i.imgur.com/Iu4aoZl.png) - also when creating your lambda func, u have option to use aws serverless app repo - is basically sam templates created by a lot of people - can search through #### SAM with CodeDeploy ![](https://i.imgur.com/6TZeZ3a.png) - using sam hello world python example ![](https://i.imgur.com/53FazTL.png) - codeploy yaml to be integrated with your template yaml ![](https://i.imgur.com/MdSlswX.png) - add `AutoPublishAlias` in your lambda func section in template yaml - create new alias "live" - also deployment pref specific canary 10% 10 mins - 10% of traffic go to new ver for 10mins then shift 100% ![](https://i.imgur.com/A2PyL8k.png) - can also use `sam deploy --guided` instead of package and deploy - run `sam build` before ![](https://i.imgur.com/aqsHPHQ.png) - live alias created ![](https://i.imgur.com/8ThfOvM.png) - can view codedeploy traffic shift from codedeploy console ###### tags: `AWS Developer Associate` `Notes`

    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
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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