Skip to content

Instantly share code, notes, and snippets.

@cbschuld
Last active May 2, 2024 19:32
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cbschuld/938190f81d00934f7a158ff223fb5e02 to your computer and use it in GitHub Desktop.
Save cbschuld/938190f81d00934f7a158ff223fb5e02 to your computer and use it in GitHub Desktop.
Obtain values from the AWS Parameter store with Typescript/node
import { SSM } from "aws-sdk";
const getParameterWorker = async (name:string, decrypt:boolean) : Promise<string> => {
const ssm = new SSM();
const result = await ssm
.getParameter({ Name: name, WithDecryption: decrypt })
.promise();
return result.Parameter.Value;
}
export const getParameter = async (name:string) : Promise<string> => {
return getParameterWorker(name,false);
}
export const getEncryptedParameter = async (name:string) : Promise<string> => {
return getParameterWorker(name,true);
}
@cbschuld
Copy link
Author

If using serverless you'll need the following IAM

provider:
  region: us-west-2
  accountId: XXXXXXXXXX
  iamRoleStatements:
    - Effect: Allow
      Action: 
        - ssm:GetParameter
      Resource:
        - "arn:aws:ssm:${self:provider.region}:${self:provider.accountId}:parameter/TWILIO*"

@rnag
Copy link

rnag commented Dec 6, 2021

Works like a charm! Only in my case I decided to modify it a bit, because my use case is I'm retrieving an SSM parameter value in my cdk script, so in my stack I actually need to create the SSM with the region info like new SSM({ region: this.region }). So I updated it slightly to pass in the SSM client to the function instead as in this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment