Bootstrap FreeKB - Amazon Web Services (AWS) - POST JSON to a Serverless Application Model (SAM) app
Amazon Web Services (AWS) - POST JSON to a Serverless Application Model (SAM) app

Updated:   |  Amazon Web Services (AWS) articles

This assumes you have already:

Let's say the files that make up your SAM app are located at /tmp/sam-app.

]$ ll /tmp/sam-app
total 20
drwxrwxr-x 2 ec2-user ec2-user   24 Nov 16 02:13 events
drwxrwxr-x 2 ec2-user ec2-user   63 Nov 16 02:13 my_app
-rw-rw-r-- 1 ec2-user ec2-user    0 Nov 16 02:13 __init__.py
-rw-rw-r-- 1 ec2-user ec2-user 8329 Nov 16 02:13 README.md
-rw-rw-r-- 1 ec2-user ec2-user  679 Nov 16 02:13 samconfig.toml
-rw-rw-r-- 1 ec2-user ec2-user 1681 Nov 16 02:13 template.yaml
drwxrwxr-x 4 ec2-user ec2-user   80 Nov 16 02:13 tests

 

And the app is a Python app. Here is an example of what you could have in /tmp/sam-app/my_app/app.py.

import json

def lambda_handler(event, context):

    return {
        "body": json.dumps({
            "foo": event.get('foo')
        }),
    }

 

You will need to ensure your SAM app allows the POST method.

Resources:
  myFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: myapp/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Events:
        myapp:
          Type: Api
          Properties:
            Path: /api/v1
            Method: post

 

The aws apigateway get-rest-apis command can be used to list your API Gateway REST APIs.

]$ aws apigateway get-rest-apis
{
    "items": [
        {
            "apiKeySource": "HEADER",
            "name": "sam-app",
            "tags": {
                "aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-1:123456789012:stack/sam-app/0882e400-842b-11ee-9053-0e798d5724d7",
                "aws:cloudformation:stack-name": "sam-app",
                "aws:cloudformation:logical-id": "ServerlessRestApi"
            },
            "endpointConfiguration": {
                "types": [
                    "EDGE"
                ]
            },
            "version": "1.0",
            "createdDate": 1700103122,
            "id": "abcdefg123"
        }
    ]
}

 

And then the aws apigateway get-resources command can be used to list your API Gateway Resources using the id returned in the prior command. In this example, we can see that the resource is configured to process POST requests.

]$ aws apigateway get-resources --rest-api-id abcdefg123
{
    "items": [
        {
            "path": "/api",
            "id": "5fzp03",
            "pathPart": "api",
            "parentId": "wxhgpi1kz5"
        },
        {
            "path": "/api/sendgrid",
            "id": "c02nnh",
            "pathPart": "sendgrid",
            "parentId": "5fzp03"
        },
        {
            "path": "/api/sendgrid/v1",
            "resourceMethods": {
                "POST": {}
            },
            "id": "cn5emi",
            "pathPart": "v1",
            "parentId": "c02nnh"
        },
        {
            "path": "/",
            "id": "wxhgpi1kz5"
        }
    ]
}

 

And here is how you could POST JSON using curl.

curl \
--request POST \
--url https://abcdefg123.execute-api.us-east-1.amazonaws.com/Prod/api/v1 \
--header 'content-type: application/json' \
--data '{"foo":"bar"}'

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 2d5ef1 in the box below so that we can be sure you are a human.