How to modify CfnParameter for objects inside the stack #27187
Unanswered
Cameronsplaze
asked this question in
Q&A
Replies: 1 comment 5 replies
-
This is the best thing we've come up with so far: Create CfnParameter for visibility but don't use it directly. # you can also have the default be in cdk.json, this is just for an example
def get_param(stack: Stack, key: str, default: str=None, param_type: str="String", description: str="") -> str:
val = self.node.try_get_context(key) or default
# If you don't pass in a default, make it a required variable:
if val is None:
raise ValueError(f"Missing required parameter: '{key}', and no default is set.")
# This is just to see what the stack was deployed with in the console/cfn-parameters tab:
CfnParameter(
stack,
key,
type=param_type,
default=val,
# Since changing it in the console won't do anything, don't let them:
allowed_values=[val],
description=f"{description}{' ' if description else ''}(Re-deploy to Edit)"
)
# If you're expecting a number, change it away from a string:
# (CfnParameter *wants* the input as a string, but nothing else does)
if param_type == "Number":
val = float(val) if "." in val else int(val)
return val
class CfnparameterTestStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
self.maturity = get_param(self, "maturity", "TEST")
hello_bucket = s3.Bucket(
self, 'HelloBucket',
bucket_name=f"{self.maturity.lower()}-hello-bucket",
versioned=False,
public_read_access=False,
removal_policy=RemovalPolicy.DESTROY,
) Deploy with: The main cons I see are:
And the main pros:
Any thoughts? |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We have a CfnParameter that's the prefix for the stack that we use in multiple places (i.e
PROD
). The problem is using it in a s3 bucket name will throw, since upper-case characters aren't allowed in s3 bucket names.We could use a Map, but would like it to be more dynamic than that. (Feature/devel stacks could have all sorts of prefixes).
Another idea was to use Lazy Tokens, but they don't populate by then either:
Similar to aws/jsii#807
The token isn't resolved yet, so the above will always return None. Since this is something we pass into the stack, it seems really weird it can't be accessed until after the stack is deploying.
Another option is to use Fn.transform:
But this requires you to deploy the 'String" macro to your account first. Plus that's a LOT of code for a single
.lower()
operation, it won't scale well.My final thought is to use
--context
instead. I'd really like to have the Cloudformation Console populated with the input params still though, so maybe I can create the CfnParams from context directly, then never touch them again? Then I can call the .lower()/etc operations on the context values themselves. My initial hunch is its hacky, so I wonder if there's a simpler solution I'm missing.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions