autoboxer
autoboxer

Reputation: 1397

How Can I Reference Environment Variables in serverless.ts?

I'm using serverless-ssm-fetch in my serverless.ts file, which resolves many of the variables that are environment specific. This works great when I'm referencing these variables in my code, however, I have two values in my serverless.ts file itself that I'd like to draw from SSM Parameter Store. Below is my serverless.ts file, and what I'm trying to do to pull in lambda-security-group-ids and lambda-subnet-ids is working, but I'm not sure how to reference them within the serverless.ts file. Does anyone know how to do this?

import type { AWS } from '@serverless/typescript';
import importFacility from '@functions/ImportFacility';
import ProcessEvent from '@functions/ProcessEvent';

const serverlessConfiguration: AWS = {
    service: 'myservice',
    frameworkVersion: '2',
    custom: {
        webpack: {
            webpackConfig: './webpack.config.js',
            includeModules: true,
        },
        bundle: {
            ignorePackages: ['pg-native']
        },
        serverlessSsmFetch: {
            DB_Host: 'database-host~true',
            PORT: 'serverless-database-port~true',
            DB_NAME: 'clinical-database-name~true',
            DB_USER_NAME: 'database-username~true',
            DB_PASSWORD: 'database-password~true',
            AWS_ACCESS_KEY: 'serverless-access-key-id~true',
            AWS_SECRECT_KEY: 'serverless-access-key-secret~true',
            LAMBDA_SECURITY_GROUP_IDS: 'lambda-security-group-ids~true', // WANT TO REFERENCE
            LAMBDA_SUBNET_IDS: 'lambda-subnet-ids~true' // WANT TO REFERENCE
        }
    },
    plugins: ['serverless-webpack', 'serverless-ssm-fetch'],
    provider: {
        name: 'aws',
        runtime: 'nodejs14.x',
        apiGateway: {
            minimumCompressionSize: 1024,
            shouldStartNameWithService: true,
        },
        environment: {
            AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1'
        },
        lambdaHashingVersion: '20201221',
        vpc: {
            securityGroupIds: [`${process.env.LAMBDA_SECURITY_GROUP_IDS}`], // NOT WORKING
            subnetIds: [`${process.env.LAMBDA_SUBNET_IDS}`] // NOT WORKING
        }
    },
    functions: { importFacility, ProcessEvent },
};

module.exports = serverlessConfiguration;

Upvotes: 0

Views: 1667

Answers (1)

Ivann Morice
Ivann Morice

Reputation: 36

For anyone wondering juste had the issue

the syntax :

vpc: {
  securityGroupIds: ['${ssm:${self:custom.serverlessSsmFetch.LAMBDA_SECURITY_GROUP_IDS}}'], 
  subnetIds: ['${ssm:${self:custom.serverlessSsmFetch.LAMBDA_SUBNET_IDS}}]
} 

worked for me.

As far as I understand you have to use the syntax as it would have been rendered while using serverless.yml template

Upvotes: 2

Related Questions