AWS Serverless Deployment


YAML File

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
  hello:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: handler.hello
      Runtime: nodejs14.x
      CodeUri: .
      Description: Test Description
      MemorySize: 512
      Timeout: 10   
      Events:
        API1:
          Type: Api
          Properties:
            Path: /hello
            Method: ANY


handler.js


'use strict';

const databaseManager = require('./databaseManager');
const uuidv1 = require('uuid/v1');

exports.hello = async (event) => {
	console.log(event);

	switch (event.httpMethod) {
		case 'DELETE':
			return deleteItem(event);
		case 'GET':
			return getItem(event);
		case 'POST':
			return saveItem(event);
		case 'PUT':
			return updateItem(event);
		default:
			return sendResponse(404, `Unsupported method "${event.httpMethod}"`);
	}
};

function saveItem(event) {
	const item = JSON.parse(event.body);
	item.itemId = uuidv1();

	return databaseManager.saveItem(item).then(response => {
		console.log(response);
		return sendResponse(200, item.itemId);
	});
}

function getItem(event) {
	const itemId = event.pathParameters.itemId;

	return databaseManager.getItem(itemId).then(response => {
		console.log(response);
		return sendResponse(200, JSON.stringify(response));
	});
}

function deleteItem(event) {
	const itemId = event.pathParameters.itemId;

	return databaseManager.deleteItem(itemId).then(response => {
		return sendResponse(200, 'DELETE ITEM');
	});
}

function updateItem(event) {
	const itemId = event.pathParameters.itemId;

	const body = JSON.parse(event.body);
	const paramName = body.paramName;
	const paramValue = body.paramValue;

	return databaseManager.updateItem(itemId, paramName, paramValue).then(response => {
		console.log(response);
		return sendResponse(200, JSON.stringify(response));
	});
}

function sendResponse(statusCode, message) {
	const response = {
		statusCode: statusCode,
		body: JSON.stringify(message)
	};
	return response
}
	

Step 1:


Step 2:


Step 3:


Create S3 Bucket

aws s3 mb s3://hexatral-sam


Step 4:


Create packaged-template.yaml File

aws cloudformation package --template-file e:\Hexatral\SAM\template.yml --s3-bucket hexatral-sam --output-template-file e:\Hexatral\SAM\packaged-template.yaml


Step 5:


Step 6:


Deploy CloudFormation Stack

aws cloudformation deploy --template-file e:\Hexatral\SAM\packaged-template.yaml --stack-name hexatral-sam-cloudformation --capabilities CAPABILITY_IAM


Step 7:


Cloudformation Deployed

Step 8:


API Gateway Created

Step 9:


Lambda Created

Step 10:



Delete CloudFormation Stack

aws cloudformation delete-stack --stack-name hexatral-sam-cloudformation