35 types
This module is part of the AWS Cloud Development Kit project.
You can reference existing SSM Parameter Store values that you want to use in
your CDK app by using ssm.StringParameter.fromStringParameterAttributes:
parameter_version = AWSCDK::Token.as_number({Ref: "MyParameter"})
# Retrieve the latest value of the non-secret parameter
# with name "/My/String/Parameter".
string_value = AWSCDK::SSM::StringParameter.from_string_parameter_attributes(self, "MyValue", {
parameter_name: "/My/Public/Parameter",
}).string_value
string_value_version_from_token = AWSCDK::SSM::StringParameter.from_string_parameter_attributes(self, "MyValueVersionFromToken", {
parameter_name: "/My/Public/Parameter",
# parameter version from token
version: parameter_version,
}).string_value
# Retrieve a specific version of the secret (SecureString) parameter.
# 'version' is always required.
secret_value = AWSCDK::SSM::StringParameter.from_secure_string_parameter_attributes(self, "MySecureValue", {
parameter_name: "/My/Secret/Parameter",
version: 5,
})
secret_value_version_from_token = AWSCDK::SSM::StringParameter.from_secure_string_parameter_attributes(self, "MySecureValueVersionFromToken", {
parameter_name: "/My/Secret/Parameter",
# parameter version from token
version: parameter_version,
})
You can also reference an existing SSM Parameter Store value that matches an AWS specific parameter type:
AWSCDK::SSM::StringParameter.value_for_typed_string_parameter_v2(self, "/My/Public/Parameter", AWSCDK::SSM::ParameterValueType::AWS_EC2_IMAGE_ID)
To do the same for a SSM Parameter Store value that is stored as a list:
AWSCDK::SSM::StringListParameter.value_for_typed_list_parameter(self, "/My/Public/Parameter", AWSCDK::SSM::ParameterValueType::AWS_EC2_IMAGE_ID)
You can also use an existing parameter by looking up the parameter from the AWS environment. This method uses AWS API calls to lookup the value from SSM during synthesis.
string_value = AWSCDK::SSM::StringParameter.value_from_lookup(self, "/My/Public/Parameter")
The result of the StringParameter.valueFromLookup() operation will be written to a file
called cdk.context.json. You must commit this file to source control so
that the lookup values are available in non-privileged environments such
as CI build steps, and to ensure your template builds are repeatable.
To customize the cache key, use the additional_cache_key property of the options parameter.
This allows you to have multiple lookups with the same parameters
cache their values separately. This can be useful if you want to
scope the context variable to a construct (ie, using additionalCacheKey: this.node.path),
so that if the value in the cache needs to be updated, it does not need to be updated
for all constructs at the same time.
string_value = AWSCDK::SSM::StringParameter.value_from_lookup(self, "/My/Public/Parameter", nil, {additional_cache_key: @node.path})
When using value_from_lookup an initial value of 'dummy-value-for-$parameterName'
(dummy-value-for-/My/Public/Parameter in the above example)
is returned prior to the lookup being performed. This can lead to errors if you are using this
value in places that require a certain format. For example if you have stored the ARN for a SNS
topic in a SSM Parameter which you want to lookup and provide to Topic.fromTopicArn()
arn_lookup = AWSCDK::SSM::StringParameter.value_from_lookup(self, "/my/topic/arn")
AWSCDK::SNS::Topic.from_topic_arn(self, "Topic", arn_lookup)
Initially arn_lookup will be equal to dummy-value-for-/my/topic/arn which will cause
Topic.fromTopicArn to throw an error indicating that the value is not in arn format.
For these use cases you need to handle the dummy-value in your code. For example:
arn_lookup = AWSCDK::SSM::StringParameter.value_from_lookup(self, "/my/topic/arn")
arn_lookup_value = nil
if arn_lookup.includes("dummy-value")
arn_lookup_value = self.format_arn({
service: "sns",
resource: "topic",
resource_name: arn_lookup,
})
else
arn_lookup_value = arn_lookup
end
AWSCDK::SNS::Topic.from_topic_arn(self, "Topic", arn_lookup_value)
Alternatively, if the property supports tokens you can convert the parameter value into a token to be resolved after the lookup has been completed.
arn_lookup = AWSCDK::SSM::StringParameter.value_from_lookup(self, "/my/role/arn")
AWSCDK::IAM::Role.from_role_arn(self, "role", AWSCDK::Lazy.string({produce: () => arnLookup}))
AWS Systems Manager (SSM) Parameter Store supports cross-account sharing of parameters using the AWS Resource Access Manager (AWS RAM)
service. In a multi-account environment, this feature enables accounts (referred to as "consuming accounts") to access and retrieve
parameter values that are shared by other accounts (referred to as "sharing accounts"). To reference and use a shared SSM parameter
in a consuming account, the from_string_parameter_arn() method can be employed.
The from_string_parameter_arn() method provides a way for consuming accounts to create an instance of the StringParameter
class from the Amazon Resource Name (ARN) of a shared SSM parameter. This allows the consuming account to retrieve and utilize the
parameter value, even though the parameter itself is owned and managed by a different sharing account.
sharing_parameter_arn = "arn:aws:ssm:us-east-1:1234567890:parameter/dummyName"
shared_param = AWSCDK::SSM::StringParameter.from_string_parameter_arn(self, "SharedParam", sharing_parameter_arn)
Things to note:
In summary, the process involves three main steps:
This cross-account sharing mechanism allows for centralized management and distribution of configuration data (stored as SSM parameters) across multiple AWS accounts within an organization or between different organizations.
Read Working with shared parameters for more details.
You can create either ssm.StringParameter or ssm.StringListParameters in
a CDK app. These are public (not secret) values. Parameters of type
SecureString cannot be created directly from a CDK application; if you want
to provision secrets automatically, use Secrets Manager Secrets (see the
aws-cdk-lib/aws-secretsmanager package).
AWSCDK::SSM::StringParameter.new(self, "Parameter", {
allowed_pattern: ".*",
description: "The value Foo",
parameter_name: "FooParameter",
string_value: "Foo",
tier: AWSCDK::SSM::ParameterTier::ADVANCED,
})
# Grant read access to some Role
role = nil # AWSCDK::IAM::IRole
# Create a new SSM Parameter holding a String
param = AWSCDK::SSM::StringParameter.new(self, "StringParameter", {
# description: 'Some user-friendly description',
# name: 'ParameterName',
string_value: "Initial parameter value",
})
param.grant_read(role)
# Create a new SSM Parameter holding a StringList
list_parameter = AWSCDK::SSM::StringListParameter.new(self, "StringListParameter", {
# description: 'Some user-friendly description',
# name: 'ParameterName',
string_list_value: ["Initial parameter value A", "Initial parameter value B"],
})
When specifying an allowed_pattern, the values provided as string literals
are validated against the pattern and an exception is raised if a value
provided does not comply.
When using CDK Tokens in parameter name,
you need to explicitly set the simple_name property. Setting simple_name to an incorrect boolean
value may result in unexpected behaviours, such as having duplicate '/' in the parameter ARN
or missing a '/' in the parameter ARN.
simple_name is used to indicates whether the parameter name is a simple name. A parameter name
without any '/' is considered a simple name, thus you should set simple_name to true.
If the parameter name includes '/', set simple_name to false.
require 'aws-cdk-lib'
func = nil # AWSCDK::Lambda::IFunction
simple_parameter = AWSCDK::SSM::StringParameter.new(self, "StringParameter", {
# the parameter name doesn't contain any '/'
parameter_name: "parameter",
string_value: "SOME_VALUE",
simple_name: true,
})
non_simple_parameter = AWSCDK::SSM::StringParameter.new(self, "StringParameter", {
# the parameter name contains '/'
parameter_name: "/#{func.function_name}/my/app/param",
string_value: "SOME_VALUE",
simple_name: false,
})