0
0
AWScloud~7 mins

Lambda with API Gateway pattern in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to run code in the cloud that responds to web requests without managing servers. AWS Lambda with API Gateway lets you do this by running your code only when needed and connecting it to web URLs.
When you want to create a simple web API without setting up servers.
When you need to run backend code that triggers on HTTP requests.
When you want to save costs by paying only for the code execution time.
When you want to quickly deploy a function that handles web requests.
When you want to build a serverless app that scales automatically.
Config File - template.yaml
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  MyApiFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs18.x
      CodeUri: ./src/
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /hello
            Method: GET

This is an AWS SAM template that defines a Lambda function and an API Gateway endpoint.

  • MyApiFunction: The Lambda function resource.
  • Handler: The file and function name Lambda runs.
  • Runtime: The language environment for the function.
  • CodeUri: Location of the function code.
  • Events: Defines API Gateway event to trigger Lambda on HTTP GET /hello.
Commands
Builds the Lambda function and prepares deployment artifacts.
Terminal
sam build
Expected OutputExpected
Building codeuri: ./src runtime: nodejs18.x metadata: {} architecture: x86_64 functions: ['MyApiFunction'] Running NodejsNpmBuilder:CopyNpmrc Running NodejsNpmBuilder:Build Build Succeeded
Deploys the Lambda function and API Gateway to AWS with guided prompts for configuration.
Terminal
sam deploy --stack-name my-api-stack --guided
Expected OutputExpected
Configuring SAM deploy Stack Name [my-api-stack]: AWS Region [us-east-1]: Confirm changes before deploy [y/N]: N Allow SAM CLI IAM role creation [Y/n]: Y Save arguments to samconfig.toml [Y/n]: Y Deploying with following values Stack name: my-api-stack Region: us-east-1 Confirm changeset: false Deployment s3 bucket: sam-artifacts-bucket Capabilities: ['CAPABILITY_IAM'] Waiting for stack create/update to complete Successfully created/updated stack - my-api-stack Outputs Key MyApiFunctionApi Description API Gateway endpoint URL for Prod stage for MyApiFunction function Value https://abcdefghij.execute-api.us-east-1.amazonaws.com/Prod/hello
--stack-name - Names the CloudFormation stack for deployment
--guided - Runs interactive deployment setup
Sends an HTTP GET request to the deployed API Gateway URL to test the Lambda function response.
Terminal
curl https://abcdefghij.execute-api.us-east-1.amazonaws.com/Prod/hello
Expected OutputExpected
{"message":"Hello from Lambda!"}
Key Concept

If you remember nothing else from this pattern, remember: API Gateway connects web requests to Lambda functions so your code runs only when needed without managing servers.

Common Mistakes
Not setting the correct handler path in the Lambda function configuration.
Lambda will fail to find the function to run, causing errors on invocation.
Ensure the handler matches the file and exported function name exactly, like 'index.handler'.
Forgetting to deploy the API Gateway after updating the Lambda function.
Changes won't be available on the web URL, so you see old or no responses.
Always run 'sam deploy' after code or configuration changes.
Using the wrong HTTP method when testing the API (e.g., POST instead of GET).
API Gateway will return method not allowed errors.
Use the HTTP method defined in the API event, here GET.
Summary
Use an AWS SAM template to define a Lambda function triggered by API Gateway on HTTP requests.
Build the function with 'sam build' to prepare deployment files.
Deploy the stack with 'sam deploy --guided' to create the Lambda and API Gateway resources.
Test the API by sending an HTTP request to the API Gateway URL to invoke the Lambda function.