Home > Net >  How to update existing SSM Parameter with AWS CDK
How to update existing SSM Parameter with AWS CDK

Time:03-04

I would like to update SSM Parameter using AWS CDK.

My use case: In first stack I am creating SSM parameter. In the second stack want to update(change) it. One of solution that I came across was using lambda, and i would like to avoid it.

Is the a way updating existing parameter via CDK, maybe something along cfn_param.set_value

First stack:

    class ParamSetupStack(Stack):
    
        def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
            super().__init__(scope, construct_id, **kwargs)
    
    
    
            ssm.StringParameter( self,
                                f'PIPELINE-PARAM-1',
                                parameter_name="parma-1",
                                string_value="SOME STRING VALUE",
                                description="Desctiption of ...."
                                )

Second stack:

    class UpdateParamStack(Stack):
    
        def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
            super().__init__(scope, construct_id, **kwargs)
    
            template = cfn_inc.CfnInclude(self, "Template",
                                      template_file="PATH/TO/ParamSetupStack.json",
                                      preserve_logical_ids=False)
    
            cfn_param = template.get_resource("PIPELINE-PARAM-1")
    
            cfn_param.set_value("NEW VALUE")

CodePudding user response:

Your best option is to avoid this requirement. Cross-stack resource modification makes your IaaC hard to reason about and introduces deploy-time side-effects. And it makes the CDK best-practice gods cross.

If you absolutely, positively must, you can use a Custom Resource to change a resource defined in another stack. The AwsCustomResource construct (uses a lambda behind the scenes) can execute arbitrary SDK commands during the deployment lifecycle.

Notes:

  1. Class methods like from_string_parameter_name are the idiomatic way to import read-only versions of existing resources. These won't help in your case.
  2. Parsing a CDK-generated template, as in the OP, is an anti-pattern and anyway won't solve your problem.
  • Related