RestfulAPI powered by AWS-Lambda

  • Full fledged Restful API backed by Lambda behind APIGateway.
  • Creation of resources using CloudFormation “template.yaml”.
  • CodeUri → This is that folderName which would contain the template.yaml file.
  • Handler This contains the FQDN for the code-file where our actual Lambda Function is being coded within Java directory.
  • The “OrderEvents” section creates a API gateway endpoint.
  • Type is “Api”, which stands for API-Gateway.
  • Path” defines the endpoint with which this API endpoint should be created and it should be binded to the HTTP method post
  • We have created “DynamoDBCrudPolicy”, which is a type of security-policy, to tell that, our Lambda function would be performing CRUD operations on the mentioned table.
  • Note here that, “!Ref” is an intrinsic CFN function, because we are creating the DynamoDB table on the fly i.e. table itself shall be created dynamically using the template.yaml file.
  • This Policy will give our lambda function the permissions to perform CRUD operations against the mentioned Dynamo DB tables. In our case, it’s OrdersTable only.
  • The “OrderEvents” section creates a API gateway endpoint.
  • Type is “Api”, which stands for API-Gateway.
  • Path” defines the endpoint with which this API endpoint should be created and it should be binded to the GET method post
  • We have created “DynamoDBReadPolicy”, which is a type of security-policy, to tell that, our Lambda function can perform only Read operations on the mentioned table. Note here, that “ReadOrdersFunction” lambda function can NOT perform the CRUD operation with this policy.
  • Note here that, “!Ref” is an intrinsic function, because we are creating the DynamoDB table on the fly.
  • This Policy will give our lambda function the permissions to perform READ operations against the mentioned Dynamo DB tables. In our case, it’s OrdersTable only.
  • These policies (e.g. DynamoDBReadPolicy) are already defined in sam and in cloud formation as the name itself says.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
ordersapi

Sample SAM Template for ordersapi

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 30
Runtime: java8
MemorySize: 512
Environment:
Variables:
ORDERS_TABLE: !Ref OrdersTable

Resources:
OrdersTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: Number

CreateOrdersFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabspw/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: ordersapi
Handler: com.aditya.learn.function.CreateOrderLambda::createOrder
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref OrdersTable
Events:
OrderEvents:
Type: Api
Properties:
Path: /orders
Method: POST

ReadOrdersFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabspw/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: ordersapi
Handler: com.aditya.learn.function.ReadOrderLambda::readOrders
Policies:
- DynamoDBReadPolicy:
TableName: !Ref OrdersTable
Events:
OrderEvents:
Type: Api
Properties:
Path: /orders
Method: GET

Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
CreateOrdersAPI:
Description: "API Gateway endpoint URL for Prod stage for Create Orders API"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/orders/"
CreateOrdersFunction:
Description: "Create Orders Lambda Function ARN"
Value: !GetAtt CreateOrdersFunction.Arn
CreateOrdersFunctionIamRole:
Description: "Create Orders Lambda Function ARN"
Value: !GetAtt CreateOrdersFunctionRole.Arn
sam build
sam deploy
  • Note here, some adhoc chars have been appended with the name of the DynamoDB table and this is the reason, we needed the !Ref function while referring it in our template.yaml file.
  • Also, we can very well observe the entries present in this table.

--

--

--

Software Engineer for Big Data distributed systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Code Smell 64 — Inappropriate Intimacy

Development Environments at Cornershop

The Magic of iOS Unit Testing with XCTest and Swift 3

Tutorial Arduino Burn Bootloader Atmega 328 100% Successful

 Tutorial Arduino Burn Bootloader Atmega 328 100% Successful

Technical Interview Prep: Animal Shelter (Queues) — Ruby

Python in a AWS Lambda quickly

KYVE TESTNET TUTORIAL COMMUNITY TASK

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aditya goel

aditya goel

Software Engineer for Big Data distributed systems

More from Medium

Asynchronous workflows powered by AWS-Lambda

Memory usage analysis for a go application on Linux

S3: Paths to all existing files in directory are not loading (Ruby)

How to use Protobuf with Go?