How to access SSM Parameter Store from SAM lambda local in node

I have a lambda with node and for local deployment I am using SAM CLI. This lambda requires some parameters in the SSM parameter store to be able to connect to the DB. I configured the AWS_ACCES_KEY_ID and AWS_SECRET_ACCESS_KEY, as environment variables, in addition to the region. When executing the local lamda, I do not get any error, as it goes to aws, but it does not bring me anything. It is not a code issue, because if I deploy it already in aws it works without problem. I don't know if I need to do another configuration for it to work.

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  ciencuadras-appraisal-request

  Sample SAM Template for ciencuadras-appraisal-request

Parameters:
  Stage:
    Type: String
    Default: dev
    
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  ApiDeployment:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref Stage

  RequestAppraisalFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction    
    Properties:
      CodeUri: dist/
      Handler: main.handler
      Runtime: nodejs14.x
      Environment:
        Variables:
          AWS_REGION: 'us-east-1'    
      Events:
        RequestAppraisal:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /sendemail-new-appraisal
            Method: post
            RestApiId: !Ref ApiDeployment

Thanks

Upvotes: 3

Views: 5016

Answers (2)

AkhRoot
AkhRoot

Reputation: 31

You can still use SSM in your template and pass --env-vars when invoking the function locally, for example:

template.yaml

MyFunction:
  Type: AWS::Serverless::Function 
  Properties:
    CodeUri: dist/
    Handler: main.handler
    Runtime: nodejs14.x
    Environment:
      Variables:
        API_KEY: !Sub '{{resolve:ssm:API_KEY:1}}'

env.json:

{
  "Parameters": {
    "API_KEY": "123"
  }
}

pass env.json when invoking the function:

sam local invoke --env-vars env.json MyFunction

Note: If you are running API locally too, you can do:

sam local start-api --env-vars env.json

Upvotes: 3

Robert Kossendey
Robert Kossendey

Reputation: 7018

Currently there is no possibility to access Parameter Store variables from Sam Local as you can read up here.

Instead, you can use --env-vars option on SAM CLI to pass values to the running function.

Upvotes: 7

Related Questions