0
0
AWScloud~5 mins

Resources and methods in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you build an API on AWS, you need to organize how users access your backend. Resources are like folders or paths in a website, and methods are the actions users can do on those paths, like reading or writing data.
When you want to create a web API that users or apps can call to get or send data.
When you need to organize your API into different parts, like /users or /orders.
When you want to control what actions users can do on each part, like GET to read or POST to add data.
When you want to connect your API to backend services like AWS Lambda functions.
When you want to manage and secure your API endpoints easily.
Config File - api-gateway-template.yaml
api-gateway-template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyApi:
    Type: 'AWS::ApiGateway::RestApi'
    Properties:
      Name: 'MyExampleApi'
  UsersResource:
    Type: 'AWS::ApiGateway::Resource'
    Properties:
      ParentId:
        Fn::GetAtt:
          - MyApi
          - RootResourceId
      PathPart: 'users'
      RestApiId:
        Ref: MyApi
  GetUsersMethod:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      HttpMethod: 'GET'
      ResourceId:
        Ref: UsersResource
      RestApiId:
        Ref: MyApi
      AuthorizationType: 'NONE'
      Integration:
        Type: 'MOCK'
        RequestTemplates:
          application/json: '{"statusCode": 200}'

This YAML file is a CloudFormation template that creates an API Gateway REST API named 'MyExampleApi'.

It defines a resource called 'users' under the root path.

It adds a GET method to the 'users' resource that returns a mock response with status code 200.

This setup shows how to organize resources and methods in AWS API Gateway.

Commands
This command deploys the CloudFormation template to create the API Gateway with resources and methods defined.
Terminal
aws cloudformation deploy --template-file api-gateway-template.yaml --stack-name my-api-stack
Expected OutputExpected
Waiting for stack create/update to complete... Successfully created/updated stack - my-api-stack
--template-file - Specifies the CloudFormation template file to deploy
--stack-name - Names the CloudFormation stack for management
This command lists the resources (paths) of the deployed API to verify the 'users' resource exists.
Terminal
aws apigateway get-resources --rest-api-id $(aws apigateway get-rest-apis --query 'items[?name==`MyExampleApi`].id' --output text)
Expected OutputExpected
{ "items": [ { "id": "abc123", "path": "/" }, { "id": "def456", "path": "/users" } ] }
--rest-api-id - Specifies which API's resources to list
This command retrieves details about the GET method on the 'users' resource to confirm it is set up correctly.
Terminal
aws apigateway get-method --rest-api-id $(aws apigateway get-rest-apis --query 'items[?name==`MyExampleApi`].id' --output text) --resource-id def456 --http-method GET
Expected OutputExpected
{ "httpMethod": "GET", "authorizationType": "NONE", "methodIntegration": { "type": "MOCK", "requestTemplates": { "application/json": "{\"statusCode\": 200}" } } }
--resource-id - Specifies the resource ID to check the method on
--http-method - Specifies the HTTP method to retrieve
Key Concept

If you remember nothing else from this pattern, remember: Resources are the paths in your API, and methods are the actions users can perform on those paths.

Common Mistakes
Trying to create a method on a resource that does not exist yet.
The method creation fails because the resource path must exist before adding methods to it.
Always create the resource first, then add methods to it.
Using incorrect resource IDs when managing methods via CLI.
Commands fail or show wrong data because resource IDs are unique and must be accurate.
Retrieve and use the correct resource ID from the API before managing methods.
Summary
Use CloudFormation or AWS CLI to create API Gateway resources representing API paths.
Add methods like GET or POST to resources to define allowed actions.
Verify resources and methods exist using AWS CLI commands to ensure correct API setup.