Facebook Twitter Instagram
    Thursday, June 30
    Trending
    • Family travel news for June 2022
    • Marriott mistakenly adds redemption surcharge to some award stays
    • Green Chemistry Start-Up Evolved By Nature Raises $120 Million
    • FTSE 100 tumbles as housing market cools and recession alarm rings
    • Moscow trial of WNBA star Brittney Griner to start
    • My Favourite Tools for Working With JavaScript | by Tommaso De Ponti | Jun, 2022
    • Family House Renovation in Blonay / Graber & Petter Architectes Sàrl
    • Tom Hiddleston Is Going To Be A Dad – Zawe Ashton Is Pregnant!
    Facebook Twitter Instagram Pinterest VKontakte
    Swave Digest
    • Home
    • World News
    • Technology
      • Smartphones
      • Computers
      • Programming
      • Automobiles
    • Entertainment
      • Music
      • Anime
      • Movies
    • Sports
      • Football
      • Basketball
      • Tennis
    • Business
      • Crypto
      • Stocks
      • NFT
    • Lifestyle
      • Fashion
      • Health
      • Travel
    • Shop
    Swave Digest
    Home»Technology»Programming»AWS SAM— Setting Local Serverless Development With Lambda and DynamoDB | by jakjus | May, 2022
    Programming

    AWS SAM— Setting Local Serverless Development With Lambda and DynamoDB | by jakjus | May, 2022

    Swave DigestBy Swave DigestMay 23, 2022No Comments4 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    AWS SAM— Setting Local Serverless Development With Lambda and DynamoDB | by jakjus | May, 2022 0*GSRxiQ GmKOz9IqO?resize=1024&w=1024
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Table of Contents

    • Overview of AWS SAM and its processes
    • Requirements
    • Initializing a project
    • Overview
    • Trying to invoke events locally
    • Adding DynamoDB locally
    • Adjust function handler
    • Create a table locally
    • Run the test event again

    Overview of AWS SAM and its processes

    AWS SAM— Setting Local Serverless Development With Lambda and DynamoDB | by jakjus | May, 2022 4415 829434AWS SAM— Setting Local Serverless Development With Lambda and DynamoDB | by jakjus | May, 2022 4415
    AWS SAM— Setting Local Serverless Development With Lambda and DynamoDB | by jakjus | May, 2022 0*GSRxiQ GmKOz9IqO
    Photo by Cait Ellis on Unsplash

    AWS SAM stands for Serverless Application Model. It’s a serverless framework for AWS resources exclusively. It’s used to define, test, and deploy serverless applications. SAM templating may resemble CloudFormation — and rightfully so because CloudFormation lies underneath and takes care of the deployment part.

    When you deploy our fully defined stack — lambda functions, roles, S3 buckets, serverless-native databases — you can test it while deployed in a cloud environment in the non-production stage (and it may incur charges). The development process could be faster and cheaper by running everything locally — and you will build such a setup in this article.

    Requirements

    Initializing a project

    You can browse all templates or write from scratch using sam init but we will start with the “Serverless API” template provided by AWS.

    sam init --name my-sam-app --runtime nodejs14.x --app-template quick-start-web

    Overview

    Now, let’s have a quick look at template.yamlwhich describes the setup. Your goal is to comprehend resources in just 20 seconds. All right, start!

    Readable version for article purposes. Find the full file in your working directory.

    Few CRUD functions and a DynamoDB Table. You don’t need to define API gateway resources and roles in this framework, making it much simpler than Terraform equivalent.

    Trying to invoke events locally

    You can check if functions are working by using mocked events.

    Let’s choose getAllItemsFunction function from the template and a related event from events/. It should return an empty array because no items have been inserted yet.

    sam local invoke getAllItemsFunction -e events/event-get-all-items.json

    Unfortunately, we get:

    Invoke Error  {"errorType":"ResourceNotFoundException","errorMessage":"Requested resource not found"...

    It happens because we do not have DynamoDB locally.

    Look at these few extracted lines from the function handler src/handlers/get-all-items.js:

    AWS SDK (imported in the first line) depends on execution context — and magically finds the related DynamoDB service when being deployed in the cloud. Locally, it finds nothing.

    Adding DynamoDB locally

    Run DynamoDB in Docker by using the official AWS image. Here’s the command:

    docker run -p 8000:8000 amazon/dynamodb-local

    Adjust function handler

    Change the docClient declaration in the first lines of src/handlers/get-all-items.js

    It will change the connection used depending on AWS’s SAM environment, then point to the local DynamoDB.

    We are using host.docker.internal instead of localhost because the AWS SAM event launcher is already in a docker container. This way, we get the address of the host machine from a guest container. Mind blown, I know.

    Create a table locally

    We are using the SimpleTable resource, which represents one table in DynamoDB. It is automatically created or used (if it exists), but we must create it locally.

    Use table name SampleTableso that the local table is automatically compliant with template.yaml and existing handlers.

    Do the same for attributes and keys — use the id defined in template.yaml

    aws dynamodb create-table --table-name SampleTable --attribute-definitions AttributeName=id,AttributeType=S --key-schema AttributeName=id,KeyType=HASH --billing-mode PAY_PER_REQUEST --endpoint-url http://localhost:8000

    Check if it’s created with the following:

    aws dynamodb list-tables --endpoint-url http://localhost:8000

    Run the test event again

    $ sam local invoke getAllItemsFunction -e events/event-get-all-items.json[...]"statusCode":200,"body":"[]"

    Request processed. The body is indeed an empty array. You did it! 🏆

    Now you can keep developing your lambda functions with the database connected. If you are ready to go live, you can do it with sam deploy .

    In this article, you have:

    1. Understood AWS SAM basics
    2. Added DynamoDB integration locally
    3. Made local development setup ready for your beautiful app

    In some scenarios, it takes too much effort to build a fully integrated local environment for development in serverless. There have to be changes in the code base that switch variables depending on whether they are executed locally or remotely.

    It may result in inconsistencies that escalate into code that works perfectly locally but fails in the cloud. Consider if you need the whole integration locally or if CI/CD should take care of integration testing.

    On top of that, DynamoDB has a smooth local integration because AWS provides the docker image. The setup with other, more uncommon resources can be cumbersome.

    What serverless framework are you using and how does it simplify your development?

    Let me know in the comments below!

    2022 and aws development dynamodb jakjus lambda, local may programming sam serverless setting with
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Swave Digest
    • Website
    • Twitter
    • Pinterest

    Related Posts

    Family travel news for June 2022

    June 30, 2022

    FTSE 100 tumbles as housing market cools and recession alarm rings

    June 30, 2022

    My Favourite Tools for Working With JavaScript | by Tommaso De Ponti | Jun, 2022

    June 30, 2022

    Kyrgios powers past No.26 seed at Wimbledon | 30 June, 2022 | All News | News and Features | News and Events

    June 30, 2022
    Add A Comment

    Leave A Reply Cancel Reply

    Twitter Instagram Pinterest
    • Home
    • Privacy Policy
    • Terms & Conditions
    • Contact Us
    © 2022 Swave Digest. All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.

    Posting....
    We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
    In case of sale of your personal information, you may opt out by using the link Do not sell my personal information.
    Cookie settingsACCEPT
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
    Necessary
    Always Enabled
    Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
    CookieDurationDescription
    cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
    cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
    cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
    cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
    cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
    viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
    Functional
    Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
    Performance
    Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
    Analytics
    Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
    Advertisement
    Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
    Others
    Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
    Save & Accept