Parameterize AWS Lambda with SSM Param Store

Here's an example on how to add a Slack channel and a webhook url from SSM Parameter Store into AWS Lambda environment variable:

# cloudformation template
AWSTemplateFormatVersion: '2010-09-09'  
Transform: AWS::Serverless-2016-10-31

Parameters:
	SlackWebhookUrl:  
	  Type: AWS::SSM::Parameter::Value<String>  
	  Default: '/services/Slack/incomingWebhookUrl'  

	SlackChannel:  
	  Type: AWS::SSM::Parameter::Value<String>  
	  Default: '/services/Slack/channels/exampleChannel'
	  
Resources:  
  ExampleLambda:  
    Type: AWS::Serverless::Function  
    Properties:  
      Environment:  
        Variables:  
          SLACK_WEBHOOK_URL: !Ref SlackWebhookUrl  
          SLACK_CHANNEL: !Ref SlackChannel	  
	# ... CodeUri, etc...

in Lambda, read from env vars:

// in the Lambda handler (Typescript example)
const slack = Slack({  
	url: process.env.SLACK_WEBHOOK_URL,  
	channel: process.env.SLACK_CHANNEL,  
})

For local develoment and sam local invoke, use env-vars option or supply env vars to Lambda via bash:

# content of dev-env.sh
# better not in version control, because it contains secrets
export SLACK_CHANNEL=slack-integration-test  
export SLACK_WEBHOOK_URL=<your-slack-url>

followed by:

$ source dev-env.sh && sam local invoke Main #...remaining sam args

Would you like to connect? Subscribe via email or RSS , or follow me on Twitter!