dingus
dingus

Reputation: 1001

Name an SSM parameter from a stack parameter within an SSM parameter name in AWS CDK

I have a CDK app with a CloudFormation Stack Parameter something like:

project_name_param = cdk.CfnParameter(
    self,
    "ProjectName",
    default="MyCoolProject",
)

Since multiple instances of this stack can be deployed, I'd like to create an SSM Parameter with name based on this project name, to keep things organized.

In plain CloudFormation, this could be achieved achieved by e.g:

  MyCoolResourceArnParam:
    Type: 'AWS::SSM::Parameter'
    Properties:
      Description: ARN of this project's MyCoolResource
      Name: !Sub '/Projects/${ProjectName}/MyCoolResource'
      Type: String
      Value: !GetAtt MyCoolResourceArn

...But I'm struggling to figure out how I'd use the project_id_param object in CDK to achieve the same thing. For e.g. have tried and failed with various combinations similar to:

input_bucket_ssm_param = ssm.StringParameter(
    self,
    "MyCoolResourceSSMParam",
    string_value=my_cool_resource.arn,
    description="...",
    parameter_name=cdk.Fn.sub(
        f"/Projects/{project_name_param.value_as_string}/MyCoolResource"
    ),
)

Probably I'm missing something basic as still pretty new to using CFn parameters in CDK - can anybody enlighten me on how it's supposed to work?

Upvotes: 3

Views: 1288

Answers (1)

dingus
dingus

Reputation: 1001

Whoops, found the problem!

cdk.Fn is unnecessary (the CDK should resolve the dynamic "token" anyway in string formatting), but as noted in the StringParameter doc the simple_name parameter is required, when using a tokenized parameter_name.

input_bucket_ssm_param = ssm.StringParameter(
    self,
    "MyCoolResourceSSMParam",
    string_value=my_cool_resource.arn,
    description="...",
    parameter_name=f"/Projects/{project_name_param.value_as_string}/MyCoolResource",
    simple_name=False,  # < Need this too!
)

Upvotes: 3

Related Questions