Bird
Raised Fist0
AWScloud~5 mins

REST API creation in AWS - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of creating a REST API in AWS?
easy
A. To store large amounts of data securely in the cloud.
B. To allow different applications to communicate over the internet using defined paths and actions.
C. To run virtual machines for hosting websites.
D. To create user accounts and manage permissions.

Solution

  1. Step 1: Understand REST API purpose

    REST APIs enable communication between apps using simple commands over the internet.
  2. Step 2: Identify AWS REST API role

    In AWS, REST APIs define resources (paths) and methods (actions) to allow this communication.
  3. Final Answer:

    To allow different applications to communicate over the internet using defined paths and actions. -> Option B
  4. Quick Check:

    REST API = communication interface [OK]
Hint: REST APIs connect apps via internet paths and actions [OK]
Common Mistakes:
  • Confusing REST API with data storage services
  • Thinking REST API runs virtual machines
  • Mixing REST API with user management
2. Which AWS service is primarily used to create REST APIs?
easy
A. Amazon API Gateway
B. AWS Lambda
C. Amazon S3
D. Amazon EC2

Solution

  1. Step 1: Identify AWS services for APIs

    Amazon API Gateway is designed to create, publish, and manage REST APIs.
  2. Step 2: Differentiate other services

    Amazon S3 stores files, Lambda runs code, EC2 runs servers, but API Gateway manages APIs.
  3. Final Answer:

    Amazon API Gateway -> Option A
  4. Quick Check:

    API Gateway = REST API service [OK]
Hint: API Gateway is the AWS REST API builder [OK]
Common Mistakes:
  • Choosing Lambda as the API creator
  • Confusing S3 with API service
  • Selecting EC2 for API management
3. Given this API Gateway setup: a resource path /books with a GET method using MOCK integration. What happens when a client sends a GET request to /books?
medium
A. The API fetches book data from a connected database.
B. The API returns an error because MOCK integration is invalid.
C. The API triggers a Lambda function to process the request.
D. The API returns a predefined mock response without calling any backend.

Solution

  1. Step 1: Understand MOCK integration

    MOCK integration returns a fixed response defined in API Gateway without backend calls.
  2. Step 2: Analyze GET method on /books

    Since GET uses MOCK, the client receives the mock response directly.
  3. Final Answer:

    The API returns a predefined mock response without calling any backend. -> Option D
  4. Quick Check:

    MOCK integration = fixed response [OK]
Hint: MOCK returns fixed response, no backend called [OK]
Common Mistakes:
  • Assuming MOCK calls database or Lambda
  • Thinking MOCK integration causes errors
  • Confusing MOCK with real backend integration
4. You created a POST method on resource /orders in API Gateway but get a 403 Forbidden error when testing. What is the most likely cause?
medium
A. The MOCK integration is missing a response template.
B. The resource path /orders is misspelled in the client request.
C. The method does not have permission to invoke the backend service.
D. The API Gateway does not support POST methods.

Solution

  1. Step 1: Understand 403 Forbidden in API Gateway

    403 usually means permission denied to invoke backend or access resource.
  2. Step 2: Check method permissions

    POST method likely lacks permission to call backend service, causing 403 error.
  3. Final Answer:

    The method does not have permission to invoke the backend service. -> Option C
  4. Quick Check:

    403 = permission denied [OK]
Hint: 403 means permission denied to backend [OK]
Common Mistakes:
  • Assuming API Gateway disallows POST
  • Blaming client path spelling without checking
  • Thinking MOCK integration causes 403
5. You want to create a REST API with a resource /users that supports GET and POST methods. You start with MOCK integration for both methods to test. Later, you want GET to fetch data from a Lambda function and POST to store data in DynamoDB via Lambda. What is the best approach to achieve this?
hard
A. Create the API with MOCK integration first, then update GET and POST methods to use Lambda integration with proper permissions.
B. Create separate APIs for MOCK and Lambda integrations and switch clients between them.
C. Use MOCK integration permanently because it is simpler and faster.
D. Directly connect POST method to DynamoDB without Lambda and keep GET as MOCK.

Solution

  1. Step 1: Start with MOCK integration for testing

    MOCK lets you test API behavior without backend, a good first step.
  2. Step 2: Update methods to Lambda integration

    Later, replace MOCK with Lambda integration for GET and POST to connect real backend logic.
  3. Step 3: Ensure permissions and backend setup

    Configure Lambda permissions and DynamoDB access properly for smooth operation.
  4. Final Answer:

    Create the API with MOCK integration first, then update GET and POST methods to use Lambda integration with proper permissions. -> Option A
  5. Quick Check:

    Start MOCK, then switch to Lambda integration [OK]
Hint: Test with MOCK, then switch methods to Lambda integration [OK]
Common Mistakes:
  • Trying to use MOCK and Lambda in separate APIs
  • Keeping MOCK integration permanently
  • Connecting POST directly to DynamoDB without Lambda
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard