banner

KuchBhiLearning - A free website to learn and code

This is a good learning site. This contains details of cloud computing, AWS, AWS-CDK, AWS-SDK codes and examples including S3, Redis, lambda, api-gateway, cloudfront, cloudformation.

Retrieving AWS SSM Parameter Store Using Nodejs Example

SSM (Systems Manager) is a service provided by AWS that allows you to securely store and retrieve data for your application (amongst other things). This can be environment based authentication credentials, or properties you’d like to change without needing a re-deploy of your application. 

Let's first create some secrets in Parameter Store.

For that login to AWS and navigate to AWS System Manager > Parameter Store


Create a new Parameter


In this example I am storing DB username and DB password.

Create the parameter.

const AWS = require('aws-sdk');

AWS.config.update({
  region: 'us-east-1',
});

export class ParameterStore {
  static async getParam() {
    return new Promise((res, rej) => {
      new AWS.SSM().getParameter(
        {
          Name: 'test/dbcred',
          WithDecryption: true,
        },
        (err: any, data: any) => {
          if (err) {
            console.log('err', err);
            return rej(err);
          }
          console.log('data', data)
          return res(data);
        }
      );
    });
  }
}


import { ParameterStore } from 'aws/parameter-store';

exports.handler = async function (event: any) {
  try {
    const params = await ParameterStore.getParam();
    return {
      statusCode: 200,
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ Message: `params ${params}` }),
    };
  } catch (error) {
    console.log(`The following error occurred while getting params : ${error} `);
    return error;
  }
};

After deploying the lambda, we need to provide permission for lambda to access parameter store.

Navigate to AWS functions and then navigate to Configurations tab and look for Permissions.


 Click on the Role name 


Default Lambda and VPC execution roles are attached.

Click on Add permissions and Attach Policies and search for ssmfullaccess


Select the policy and Attach the policy. 

After attaching the policy lambda call access parameter store and fetch the secrets based on the name.

No comments:

Post a Comment

If you have any doubts, Please let me know

Copyright 2022, KuchBhiLearning - A free website to learn and code. All rights Reserved.
| Designed by Yaseen Shariff