An easy way to manage Serverless project resources by using AWS Resource Groups

Written by yi | Published 2019/08/13
Tech Story Tags: serverless | manage-serverless-project | aws-resource-groups | latest-tech-stories | restful-apis | serverless-framework | creating-a-resource-group-aws | viewing-cloudtrail-events

TLDR AWS Resource Groups recently announced its support for more resource types. Using Resource Groups, you might find it very useful to act on all corresponding resources as group rather than move around from one service to another. In this article, I will be building a serverless restful API project using Serverless Framework and create a resource group that contains a collection of related AWS services for our project. The following must be done before following this guide: setup an AWS account, install or update the Serverless framework to latest version:via the TL;DR App

If you work with serverless projects, as you might be aware that a serverless project can end up with hundreds of Lambda functions which definitely increases the complexity of your application, good new is that AWS Resource Groups recently announced its support for more resource types, especially for groups based on an AWS CloudFormation stack.
By using Resource Groups, you might find it very useful to act on all corresponding resources as group rather than move around from one service to another.
In this article, I will be building a serverless restful api project using Serverless Framework and create a resource group that contains a collection of related AWS services for our api project.

Prerequisites

The following must be done before following this guide:
  • Setup an AWS account
  • Install Serverless framework 

Building a RESTful API with the Serverless Framework

First of all we have to install or update the Serverless framework to latest version:
npm install -g serverless
Next, let’s create a new serverless project:
sls create --template aws-nodejs --path myapp
The directory that is created includes two files — handler.js is the Lambda function, the serverless.yml file contains the configurations of the backend, add following config to serverless.yml:
service: myapp
provider:
  name: aws
  runtime: nodejs8.10
  region: ap-southeast-2
  tags:
    serverless: myapp
functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: get
Let’s update our handler.js, create a lambda function to handle api gateway request:
'use strict';
module.exports.hello = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,
    }),
  };
next, deploy the api:
sls deploy --stage dev
The output looks like this:
Now, we have our api deployed, this is where the fun begins! Let’s create a resource group to manage our api resources.

Creating a resource group

Let’s open the AWS Resource Groups console, on the navigation pane, under Resource Groups, choose Create Resource Group.
Select CloudFormation stack based, then choose the CloudFormation stack, in this example, we have myapp-dev stack just deployed, 
click View group resources button to see all the related services included the stack.
In Group details, type the group name and description. 
Everything is configured, choose Create group. We can go to Saved resource groups to view resource group we just created.
Navigating to the group resource, we can see the our serverless project includes S3, ApiGateway RestApi, lambda, CloudWatch Logs and Iam role resources.

Viewing CloudTrail events for AWS Resource Group

System Managers show detailed information about the resources in our groups, such as config and AWS CloudTrail logs.
Let’s open AWS Systems Manager console, choose Resource Groups in the left navigation pane under Operations Management, We can see all the event log data from CloudTrail.
That’s all about it! Thanks for reading, I hope you have found this article useful, please give me a follow if you’d like to see more in future! 

Published by HackerNoon on 2019/08/13