rohithpr
rohithpr

Reputation: 6330

How to use AWS SSM parameter store values in Cloudformation template conditionals?

I have configured a key value pair in the AWS SSM parameter store UI as my-ssm-key = ssm-value.

I have the following YAML template for CF built on Serverless:

service: redirect-test

provider:
  name: aws
  runtime: python3.8

  environment:
    ssm_value: '{{resolve:ssm:my-ssm-key:1}}'
    ssm_value_is_correct: !If [SSM_KEY_IS_CORRECT, yes, no]

functions:
  hello:
    handler: handler.hello

resources:
  Conditions:
    SSM_KEY_IS_CORRECT:
      !Equals
        - '{{resolve:ssm:my-ssm-key:1}}'
        - 'ssm-value'

On deploying the stack, environment variables are being set to ssm_value = ssm-value and ssm_value_is_correct = no.

Why is the conditional statement resolving to "no" instead of "yes"? What is the correct way to use SSM parameter store values in conditionals?

Param store screenshot: SSM param store screenshot Env variables screenshot: Env variables screenshot

Upvotes: 7

Views: 3877

Answers (1)

rohithpr
rohithpr

Reputation: 6330

I was able to resolve the issue by using this CF template:

service: redirect-test

provider:
  name: aws
  runtime: python3.8

  environment:
    ssm_value: !Ref MySSMValue
    ssm_value_is_correct: !If [SSM_KEY_IS_CORRECT, yes, no]

functions:
  hello:
    handler: handler.hello

resources:
  Conditions:
    SSM_KEY_IS_CORRECT:
      !Equals
        - !Ref MySSMValue
        - ssm-value

  Parameters:
    MySSMValue:
      Description: My SSM Value
      Type: AWS::SSM::Parameter::Value<String>
      Default: my-ssm-key

Environment variables with correct expected values

Upvotes: 1

Related Questions