0
0
AWScloud~5 mins

REST API creation in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Creating a REST API lets you build a way for different programs to talk to each other over the internet. AWS provides a service called API Gateway that helps you create and manage these APIs easily without managing servers.
When you want to let a mobile app get data from your backend securely.
When you need to connect a web app to a database through a simple interface.
When you want to expose some functions of your service to other developers.
When you want to create a serverless backend that scales automatically.
When you want to manage and monitor API usage and control access.
Config File - template.yaml
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      DefinitionBody:
        openapi: 3.0.1
        info:
          title: MySampleAPI
          version: '1.0'
        paths:
          /hello:
            get:
              x-amazon-apigateway-integration:
                uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloFunction.Arn}/invocations
                httpMethod: POST
                type: aws_proxy
              responses: {}
  HelloFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs18.x
      CodeUri: ./src/
      Policies: AWSLambdaBasicExecutionRole
      Events:
        HelloApi:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /hello
            Method: get

This AWS SAM template defines a simple REST API with one GET endpoint /hello. It uses AWS Lambda as the backend function. The MyApi resource creates the API Gateway REST API. The HelloFunction resource is the Lambda function that handles requests. The x-amazon-apigateway-integration section connects the API Gateway to the Lambda function using a proxy integration.

Commands
This command builds your AWS Serverless Application Model (SAM) project, preparing the Lambda function code and dependencies for deployment.
Terminal
sam build
Expected OutputExpected
Building codeuri: ./src runtime: nodejs18.x metadata: {} architecture: x86_64 functions: ['HelloFunction'] Running NodejsNpmBuilder:CopyNpmrc Running NodejsNpmBuilder:Build Build Succeeded
This command deploys your SAM application to AWS. The guided flag helps you set parameters like region, permissions, and confirms before deploying.
Terminal
sam deploy --stack-name my-sample-api --guided
Expected OutputExpected
Configuring SAM deploy ====================== Stack Name [sam-app]: my-sample-api AWS Region [us-east-1]: 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-sample-api Region: us-east-1 Confirm changeset: false Deployment s3 bucket: sam-cli-managed-default-samclisourcebucket-abc123 Capabilities: ['CAPABILITY_IAM'] Waiting for stack create/update to complete Successfully created/updated stack - my-sample-api Outputs -------- MyApiEndpoint: https://abc123.execute-api.us-east-1.amazonaws.com/prod/
--stack-name - Names the CloudFormation stack for your deployment
--guided - Interactive mode to help configure deployment settings
This command tests the deployed REST API by sending a GET request to the /hello endpoint. It verifies that the API is working and the Lambda function responds.
Terminal
curl https://abc123.execute-api.us-east-1.amazonaws.com/prod/hello
Expected OutputExpected
"Hello from Lambda!"
Key Concept

If you remember nothing else from this pattern, remember: API Gateway connects internet requests to backend Lambda functions to create scalable REST APIs without managing servers.

Common Mistakes
Not deploying the API after building the SAM project
Building prepares the code but does not make it live; without deployment, the API won't be accessible.
Always run 'sam deploy' after 'sam build' to publish your API.
Using the wrong API endpoint URL when testing
The endpoint URL changes with each deployment; using an old or incorrect URL results in connection errors.
Copy the API endpoint URL from the deployment output and use it exactly in your test commands.
Forgetting to grant Lambda permission to be invoked by API Gateway
Without this permission, API Gateway cannot trigger the Lambda function, causing errors.
Use AWS SAM or CloudFormation which automatically sets this permission, or add it manually if deploying differently.
Summary
Use an AWS SAM template to define a REST API and Lambda function integration.
Build the project with 'sam build' to prepare your code for deployment.
Deploy the API with 'sam deploy --guided' to create the live API Gateway endpoint.
Test the API by sending HTTP requests to the deployed endpoint URL.