Thursday, October 16, 2025

AWS CDK Set Parameter Store Value

Problem:

There are multiple ways to set a parameter store value. Which one should I use?

Solution:

1. Set parameter at deploy time:

Use this pattern when you want to create or update a plain-text SSM parameter during deployment. Creates an AWS::SSM::Parameter resource that resolves at deploy time.

new ssm.StringParameter(this, 'Param', {
  parameterName: '/my/plain/parameter',
  stringValue: 'my-value',
});

2.   Set parameter with a JSON value:

Use when you need to store structured data (e.g., configuration or mapping) as a JSON string.
The object is serialized with JSON.stringify() before being written to SSM.

new ssm.StringParameter(this, 'JsonParam', {
  parameterName: '/app/config',
  stringValue: JSON.stringify({
    apiUrl: 'https://api.example.com',
    featureFlags: { enableNewUI: true, betaAccess: false },
  }),
});

To read it back, retrieve the value and parse it:.

const config = JSON.parse(ssm.StringParameter
  .fromStringParameterAttributes(this, 'JsonParamRef', {
    parameterName: '/app/config',
  })
  .stringValue);

3.  Set parameter with a lazy value:

Use when the parameter value depends on another construct (e.g., a pipeline name) and must be evaluated dynamically at synth. cdk.Lazy defers the value computation until synthesis.

new ssm.StringParameter(this, 'LazyParam', {
  parameterName: '/pipeline/name',
  stringValue: cdk.Lazy.string({
    produce: () => pipeline.pipeline.pipelineName,
  }),
});