title: How does AWS Lambda know the amount of memory and CPU requirements needed to run my Lambda code?
body: <p>As per my understanding, AWS Lambda runs the uploaded code on an EC2 instance that is not accessible to the user. It creates the runtime environment required to run the uploaded code, manages the permissions and balances load. This is what I think AWS Lambda does behind the scenes.</p>
  
  <p>Quoting <a href="https://en.wikipedia.org/wiki/Serverless_computing" rel="nofollow noreferrer">Wikipedia</a>:</p>
  
  <blockquote>
    <p>Infrequently-used serverless code may suffer from greater response latency than code that is continuously running on a dedicated server, virtual machine, or container. This is because, unlike with an autoscaling, the cloud provider typically "spins down" the serverless code completely when not in use.</p>
  </blockquote>
  
  <p>This makes sense, but AWS Lambda does <a href="https://aws.amazon.com/lambda/" rel="nofollow noreferrer">claim</a> to use autoscaling:</p>
  
  <blockquote>
    <p>AWS Lambda automatically scales your application by running code in response to each trigger. Your code runs in parallel and processes each trigger individually, scaling precisely with the size of the workload.</p>
  </blockquote>
  
  <p>My questions are -</p>
  
  <ul>
  <li>How does AWS Lambda allocate memory and CPU for running my code?</li>
  <li>Do all my Lambda requests get serviced by a single EC2 instance (if at all)?</li>
  <li>If <strong>yes</strong> then how do they scale it when the number of requests increase?</li>
  <li>If <strong>no</strong> then how do they reduce latency if they have to create a runtime environment for the code to run each time they get a lambda request?</li>
  </ul>