Leveraging AWS SSM in a serverless.ts file with AWS Lambda: A guide to implementation

Having trouble utilizing SSM in the serverless.ts file and encountering issues.

  const serverlessConfiguration: AWS = {
  service: "data-lineage",
  frameworkVersion: "2",
  custom: {
    webpack: {
      webpackConfig: "./webpack.config.js",
      includeModules: true,
    },
    stages: ["dev", "staging", "prod"],
    region: "${opt:region, self:provider.region}",
    stage: "${opt:stage, self:provider.stage}",
    dburl: {
      dev: config.transactionalMongoUrl,
      staging:
        "${ssm:/some/some2/staging/dburl}",
      prod:
        "${ssm:/some/some2/prod/dburl}",
    },

.... ... ..

environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1",
      DB_URL:
        "${self:custom.dburl.${self:provider.stage}}",

My deployed lambda is not functioning correctly as process.env.DB_URL returns undefined.

Seeking guidance on effectively using SSM within the context of serverless.ts.

Answer №1

According to the information provided in the ssm plugin, it seems that the serverless-ssm-fetch plugin is required for installation.

serverless plugin install --name serverless-ssm-fetch

In the serverless.ts file:

plugins: [
  serverless-ssm-fetch
  ...
]

custom: {
   serverlessSsmFetch: {
      APP_ID: /aws/ssm/parameter/path/app_id
      APP_KEY: /aws/ssm/parameter/path/app_key
      APP_SECRET: /aws/ssm/parameter/path/app_secret~true
   }
}

Answer №2

This particular code structure suits my needs :

apiId:  "${ssm:/api/id}",
apiRootResourceId: "${ssm:/api/root-resource-id}"

Answer №3

Dealing with some frustration and consistently encountering the error message "Cannot resolve variable at 'provider.environment.XXX_CLIENT_SECRET': Value not found at 'ssm' source," I stumbled upon an enlightening solution. It dawned on me that my serverless.ts file was missing a crucial property - the region.

Once I added the correct region (set to us-east-2, where my parameter resided), I could finally utilize the '${ssm:/xxx_client_secret}' syntax without causing a serverless error. In my opinion, there needs to be more comprehensive documentation regarding the usage of serverless.ts. It's worth noting that I didn't need to rely on the serverless-ssm-fetch plugin, which is often recommended elsewhere.

If you're struggling to access your SSM values in your serverless.ts, ensure that you include the region property and confirm that it matches the location of your parameters.

// serverless.ts
import type { AWS } from '@serverless/typescript';

const serverlessConfiguration: AWS = {
  service: 'serverless',
  frameworkVersion: '3',
  variablesResolutionMode: '20210326',
  plugins: ['serverless-esbuild', 'serverless-local', 'serverless-offline'],
  provider: {
    name: 'aws',
    runtime: 'nodejs14.x',
    region: 'us-east-2', // <-- this is extremely important
    apiGateway: {
      minimumCompressionSize: 1024,
      shouldStartNameWithService: true
    },
    environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
      NODE_OPTIONS: '--enable-source-maps --stack-trace-limit=1000',
     XXX_CLIENT_SECRET: '${ssm:xxx_client_secret}',
     // ...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to updating component after closing MatDialog and updating data in database using Angular 6

Currently, I am in the process of learning a MEAN stack app with Angular 6. My main focus right now is on refreshing the component after making any changes, such as adding or updating new clients/cars/drivers/bookings. The issue I'm facing is that aft ...

Guide on showing the content of an uploaded file as an object in JavaScript using file reader

When using the file upload function to upload a json file and read its contents, I am encountering an issue where the result is in string format instead of object. How can I display it as an object? Here is my code: .html <div class="form-group"> ...

Tips for streamlining interface initialization and adding items to it

I have designed an interface that includes another interface: export interface Parent { children: Child[]; } export interface Child { identifier: string; data: string; } Is there a more efficient way to initialize and add items to the array? Curren ...

Application: The initialization event in the electron app is not being triggered

I am facing an issue while trying to run my electron app with TypeScript and webpack. I have a main.ts file along with the compiled main.js file. To troubleshoot, I made some edits to the main.js file to verify if the "ready" function is being called. ...

Ways to check the functionality of the secondary tier in the api.send method

Currently, I am testing a function that involves returning a promise and subsequently calling the same function again at a second level. However, I am facing difficulties in accessing this second level of call. Below is the function code: itemToForm = () ...

We were unable to identify any Next.js version in your project. Please ensure that the `"next"` package is installed in either the "dependencies" or "devDependencies" section

My attempt to deploy a Next app using the Vercel CLI has hit a roadblock. After running vercel build with no errors, I proceeded to deploy with vercel deploy --prebuilt, which also went smoothly. However, when trying to move the project from the preview en ...

Turn off or delete certain features within an npm package

Is it possible to disable or remove unused functions in a npm library? I only need certain functions from the library and don't want others to be accessible. I want to retain the read function while disabling the write function to prevent developers ...

Is there another option for addressing this issue - A function that does not declare a type of 'void' or 'any' must have a return value?

When using Observable to retrieve data from Http endpoints, I encountered an error message stating that "A function whose declared type is neither 'void' nor 'any' must return a value." The issue arises when I add a return statement in ...

The functionality of Angular's mat-autocomplete is hindered when it comes to utilizing options generated by a function

I decided to enhance the autocomplete feature on my project by taking an example from the Material official website. Instead of having the options stored in a variable within the component class, I created a function to retrieve the options. Although the o ...

The TypeScript type for a versatile onChange handler in a form

Let's skip the function declaration and dive into writing the current types for state, and the state itself. type BookFormState = { hasError: boolean; } BookForm<BookFormState> { ... state = { hasError: false }; Next, inside the class ...

Challenges with using async await alongside synchronous functions

I'm currently navigating through a library that utilizes async functions and feeling a bit overwhelmed. I'm attempting to call a function that should return a string, but I'm hitting some roadblocks. As I understand it, the ZeroEx library fu ...

The type 'MouseEvent<HTMLButtonElement, MouseEvent>' cannot be matched with the type 'boolean'

Just starting out with TS and running into a problem that TS is pointing out to me. Error: Type '(x: boolean) => void' is not compatible with type '(e: MouseEvent<HTMLButtonElement, MouseEvent>) => void'. Parameters ' ...

Transform a literal string type definition into a string value (similar to the typeof operator), or is it the other way around in TypeScript?

Is there a way to retrieve the string value of a string literal type without having to define it twice, similar to the typeof operator in C#? myStringLiteral: 'STRING TYPE'; myString: string = typeof(myStringLiteral); // I want myString to be e ...

Inheritance-based generic type inference in Typescript

Take a look at this piece of code: class A<T> { t?: T; } interface B {} class C implements A<B> {} function f<T1 extends A<T2>, T2>(a: T1): T2 | undefined { return a.t; } const result = f(new C()); const result2 = f(new A<B> ...

Why is my Angular promise unexpectedly landing in the error callback?

I am facing an issue with my Angular + Typescript client. I have developed a PHP API and need to send a post request to it. Upon receiving the request, the server fills the response body with the correct data (verified through server debugging). However, w ...

The argument provided needs to be a function, but instead, an object instance was received, not the original argument as expected

I originally had the following code: const util = require('util'); const exec = util.promisify(require('child_process').exec); But then I tried to refactor it like this: import * as exec from 'child_process'; const execPromis ...

The query fails to retrieve results for the specified date and the beginning of the month

I have encountered an issue with my query that is supposed to fetch values between the start and end of the month. Interestingly, when a record is entered on the first day of the month, it doesn't get returned in the query result. Only records entered ...

Experiment with Google Sign-In authentication in jest with Firebase

As I try to effectively mock firebase authentication with Google login, I am encountering some difficulties. Below is the code that I am currently working with: simple.tsx import React, { Component } from 'react'; import * as firebase from &apo ...

Using Typescript: What is the best way to convert a variable into a specific element of an array?

List of Strings: const myStrings = ["one", "two", "three"]; const newString = "two"; The variable newString is currently just a string, but I would like its type to be an element of myStrings. Is there a way to achi ...

How to easily enable or disable y-axis in Chart.js and customize their visibility

My Situation: Once the canvas is generated, I want to have a blank canvas area with no curves and no y-axis - surprisingly, it's working as expected. Starting View: https://i.sstatic.net/TzaTd.png Desired Outcome: When I click on a label (like Da ...