Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSM: StringParameter.valueFromLookup should return undefined if SSM parameter not available #22064

Closed
2 tasks
astef opened this issue Sep 15, 2022 · 10 comments
Closed
2 tasks
Assignees
Labels
@aws-cdk/aws-ssm Related to AWS Systems Manager bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p1 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@astef
Copy link

astef commented Sep 15, 2022

Describe the feature

When using ssm.StringParameter.valueFromLookup, it crashes (not sure, but error is not catchable), when the parameter doesn't exist. It makes it unusable for scenarios with a fallback parameter.

Use Case

For a short-lived infrastructure it's both important to be configurable (create a parameter & deploy), and have a nice fallback value (just deploy). SSM parameter is a perfect solution, since it can be resolved at synth time. It's very unfortunate, that currently it's impossible not to crash, when the value doesn't exist.

Proposed Solution

Return undefined, instead of crashing, or expose a better API, with multiple fallback parameter names to check.

Other Information

This issue was also discussed here: #7259

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2

Environment details (OS name and version, etc.)

Any

@astef astef added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Sep 15, 2022
@github-actions github-actions bot added the @aws-cdk/aws-ssm Related to AWS Systems Manager label Sep 15, 2022
@mascur mascur added bug This issue is a bug. p1 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Sep 21, 2022
@mascur
Copy link
Contributor

mascur commented Sep 21, 2022

Thank you for the ticket, I've labeled this as both a bug and feature-request since it seems to be a bit of both. One approach you could take would be to use a custom resource as mentioned here: #7259 (comment) to handle fallback values.

@sammcj
Copy link

sammcj commented Dec 19, 2022

We just got hit by this as well, it certainly seems like a bug.

@otaviomacedo
Copy link
Contributor

@astef Just to clarify, when you say "it crashes", do you mean that the CLI prints an error message like this:

[Error at /MyStack] SSM parameter not available in account 12345689, region us-east-1: foo

Found errors

and returns exit code 1?

If that's the case, the template is still generated. To know whether the parameter exists at runtime, check whether the value starts with "dummy-value-for-".

@otaviomacedo otaviomacedo added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 22, 2023
@github-actions
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels May 22, 2023
@andrew-glenn
Copy link

@otaviomacedo Thanks - but that doesn't solve the exit code 1 problem when dealing with automation.

@tyrauber
Copy link

This exit code 1 is a problem for SST.

It prevents conditionally creating resources by testing whether an ssm value exists.

Take a look as the aws-cdk-lib ssm documentation #lookup-existing-parameters.

const arnLookup = ssm.StringParameter.valueFromLookup(this, '/my/topic/arn');
sns.Topic.fromTopicArn(this, 'Topic', arnLookup);

Initially arnLookup 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.

This is no longer true. In actuality, .valueFromLookup throws the exit code 1 itself preventing further use. Take for example the following code built with SST.

import * as cdk from 'aws-cdk-lib';
import * as iam from "aws-cdk-lib/aws-iam";
import * as ssm from 'aws-cdk-lib/aws-ssm';

import { StackContext, attachPermissionsToRole  } from 'sst/constructs';

export function Role({ stack, app }: StackContext) {
  let role;
  let parameterName = `/${app.name}/Config/RoleArn`;
  let roleArn = ssm.StringParameter.valueFromLookup(stack, parameterName);

  stack.addOutputs({
    ROLEARN: role?.roleArn
  });

  return { role };
}

Just looking up the value with valueFromLookup throws an exit code 1 causing SST deployment to fail.

Error: Could not resolve context values for ssm:account=00000000000:parameterName=/sst-iam/Config/RoleArn:region=us-east-1
 > LIFECYCLE  Command failed with exit code 1.

@T-Guerrero
Copy link

This is no longer true. In actuality, .valueFromLookup throws the exit code 1 itself preventing further use. Take for example the following code built with SST.

Wrong, this remains true. In your case, I think the issue is that SSM is not being able to fetch your stack scope properly. From the method description:

     * Reads the value of an SSM parameter during synthesis through an
     * environmental context provider.
     *
     * Requires that the stack this scope is defined in will have explicit
     * account/region information. Otherwise, it will fail during synthesis.

@tyrauber
Copy link

Ah, thank you for the correction / clarification, @T-Guerrero.

@ivantichy
Copy link

ivantichy commented Sep 15, 2023

Just faced this issue too, exit code 1.
Account and region set up correctly.
Got error message:
[Error at /Stack/MyStack] SSM parameter not available in account xxxxxxxxx, region eu-central-1: /myparam

Template is generated with dummy value however exit code 1 hurts e.g. automated pipeline.

@Jonathan-Joransen
Copy link

One issue can be if you aren't passing the stack props to the super constructor. This will cause the stack to not detect the env even though it is specified in the stack props.

Here is a C# example:
Correct
internal CDKStack(Construct scope, string id, StackProps props) : base(scope, id, props)
Incorrect
internal CDKStack(Construct scope, string id, StackProps props) : base(scope, id)

Just faced this issue too, exit code 1. Account and region set up correctly. Got error message: [Error at /Stack/MyStack] SSM parameter not available in account xxxxxxxxx, region eu-central-1: /myparam

Template is generated with dummy value however exit code 1 hurts e.g. automated pipeline.

mergify bot pushed a commit that referenced this issue Sep 13, 2024
…arameter (#31415)

Updates StringParameter.valueFromLookup with an optional "defaultValue" When specified this value will be used:
 - in place of the standard dummyValue
 - to signal that an Error should not be raised during synthesis if the parameter is not found in the account.

Test are updated to prove that this works

### Issue # (if applicable)

Resolves #7051 

There are some closed issues which also benefit from this change:
- #22064 
- #7259

### Reason for this change

We have a library which has a fixed set of SSM parameters on which it depends.  The values from those parameters are made available as attributes of a custom Stack.  We have many users in many different AWS accounts, and not all of the parameters are guaranteed to exist.  This is okay.  In general, teams would simply not use those values and be happy with that outcome.  Unfortunately, CDK crashes when you look up an SSM parameter that does not exist in the account.  This is unacceptable.

### Description of changes

To address the issue described above, I implemented an optional parameter on the `valueFromLookup` method: `defaultValue`.  The idea is that if this value is specified, and we fail to look up a parameter in the account, we will return this value and suppress the Error that is currently raised when a parameter is not found.

To implement that functionality, I added a field to the `GetContextValueOptions` interface which is used to flag that we're not going to raise the error.  Then, in `valueFromLookup`, I set that flag to `true` if the `dummyValue` is specified.  `valueFromLookup` then calls `ContextProvider.getValue` passing along those values.

`ContextProvider.getValue` is modified so that when it calls `stack.reportMissingContextKey` it passes a modified set of `props` which include the `defaultValue` and the `ignoreErrorOnMissingContext` flag.

These finally land in the `aws-cdk` context provider for `ssm-parameter`.  That code has been updated so that if the value is not found in SSM, and we're told to suppress the error, then we'll simply return the `defaultValue` that was passed in. 

### Description of how you validated changes


I added a unit tests which covers when the default value is set.  I also updated the original unit test as the `props` now contain some additional field.

I added an integration test which calls `valueFromLookup` with a `defaultValue` set and then confirms that no exception is raised and that `valueFromLookup` returned the `defaultValue`

**NOTE**
I considered that the changes made _might_ need to be a part of the `cloud-assembly-schema` but chose to work around that for now.  I'm open to incorporating them there if that's a more correct path.

**NOTE 2**
I'm unsure about how to update API documentation for this change.  This does alter the public API for `valueFromLookup` and the function doesn't appear to have a proper `TSDoc` header on it.  Please let me know if there's a proper way for me to update the documentation.


### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ssm Related to AWS Systems Manager bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p1 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

10 participants