Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Don't Forget To Set MemorySize And Timeout On Your AWS SAM Templates!

6/2/2018

0 Comments

 
Once upon a time I was the serverless guy at a startup company building lambda functions. My function went into production at 128mb of memory and a timeout of 3 seconds. Because of game had "live races" we would see big spikes in traffic at certain times of the day, and unfortunately I would also see big spikes in the number of errors in the CloudWatch metrics charts... My boss even opened the website during the high volume event and noticed requests to the lambda returning 400 error. In the cloudwatch logs I could norrow down the logs to just the high volume time and see many timeout errors happening during this period. Needless to say, this is not a good look, and you don't want it to happen to you (especially you, future Jim)! 

Changing Memory And Timeout Settings Like a Bitchass

Once you have created and deployed your lambda function it will be visible in the web AWS console. You can always go in and drag the slider / increment the timeout to edit the settings for you lambda function.
Picture

Change MemorySize And
Timeout With A SAM Template

I joking call the above approach the "bitchass method" because it takes extra time and manual effort, can be error prone, and just prone to people forgetting to change it. When you introduce multiple environments that multiplies the issues. You have to keep going back to the browser console just to "make sure" the memory settings are ok. BUT, what if there was a way to just automatically deploy and specify in the code config files exactly the memory size and timeout you want it to have so you can just deploy and forget about it? Well yes, there is. If you use AWS's whole CodePipeline / CodeBuild CI / CD pipeline (which I highly recommended trying by scaffolding out a starter project with AWS CodeStar) then you should have a template.yml file in the root of your project. Since I'm building a simple AWS Lambda function right now I have a Resource named HelloWorld with the type AWS::Serverless::Function. Then in the properties I set the memory to 512mb and the timeout to 5 seconds. Below I've copied full code for my current template.yml file, and just in case the code changes you can see the current version of this file here. 
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar

Parameters:
  ProjectId:
    Type: String
    Description: AWS CodeStar projectID used to associate new resources to team members

Resources:
  HelloWorld:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs6.10
      MemorySize: 512
      Timeout: 5
      Environment:
        Variables:
          ProjectId: !Ref 'ProjectId'
      Role:
        Fn::ImportValue:
          !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
      Events:
        GetEvent:
          Type: Api
          Properties:
            Path: /
            Method: get
Specifying MemorySize and Timeout here is really nice to me because then I never need to go into the browser console and fiddle with things later. When you are finished with the code and about to deploy, regardless of the current settings, you can be sure that your lambda function will be deployed with the settings specified in this template. I think that control, piece of mind, and ability to automate these settings are actually a feather in the cap of AWS. Not too shabby.

Changing The Memory And Timeout Settings With Serverless Framework

I have been known to be so impatient waiting around for AWS CodePipeline to finish that I also add a serverless.yml file to many of my AWS lambda projects so that I can do an "impatient deploy". All you have to do is add two lines in the provider section of the config file. It should look something like the snippet below, and again you can find the full version here.
provider:
  name: aws
  runtime: nodejs6.10
  memorySize: 512
  timeout: 6
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021