i have a code that reads a value from SSM parameter which is going to be built in codebuild project. For the first time ever run, the value will not exist yet, so i wrote a logic that will catch the error and it is supposed to continue, but even with the logic, i'm still getting
SSM parameter not available in account 1234, region us-east-1: /api-endpoint error stops the codebuild project error
below is the code
let apiEndpoint: string;
try {
apiEndpoint = ssm.StringParameter.valueFromLookup(this, `api-endpoint`)
} catch (error: any) {
if (error.message.includes('ParameterNotFound')) {
// Handle the case where the parameter doesn't exist yet
console.warn('SSM parameter "api-endpoint" not found. Using default value.');
apiEndpoint = 'default-value';
} else {
// Handle other errors
throw error;
}
}
this cannot work. Note that the line
ssm.StringParameter.valueFromLookup(this, 'api-endpoint')
does not need theawait
statement. This is because this line does not actually execute any requests to AWS to validate if the parameter exists. For this reason it does not actually throw any errors. Your CDK process only builds a CloudFormation template that inserts contains a reference to this parameter. Then the actual error comes from a whole separate process that is completely outside of your control.If you want to import an SSM value on your stack, the value must exist beforehand. One way to achieve this could be to create a whole separate stack that initializes the parameter so that then you can import it here.