Closed hssyoo closed 1 year ago
@peterwoodworth I can take this.
This is happening because the StringParameter.parameterName
is a token that the value isn't known until deploy time. Is it possible for you to refactor your code such that you're passing in the same value to the parameterName
and value
?
@msambol I'm not sure which solution you would have in mind for this, but if you have one in mind that fixes it then go for it, but I'm not sure how we'd handle this on our end.
@peterwoodworth I spent some time on it..couldn't think of anything.
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.
Describe the bug
A CodeBuild Project can take a prop
environmentVariables
, which is of typeBuildEnvironmentVariable
.BuildEnvironmentVariable
has a propvalue
, which can take SSM Parameter names. Per the docs:I'm trying to create a
StringParameter
in my stack with the name/foo/bar
. Let's call itfooBar
:And when I'm defining my CodeBuild Project prop's
environmentVariables
, I want to passfooBar.parameterName
(per the docs) into thevalue
field:I'm able to successfully build and deploy my app, but when I check the
ssm:GetParameters
policy attached to the project role, I see that the resource ARN contains double forward slashes:This causes my CodeBuild job to fail since the project role has been given permissions to the wrong resource:
I noticed that when the CDK serializes environment variables, it has logic that strips SSM parameter names of the leading slash if it contains one. However, because I'm passing in the
parameterName
property of a construct, the string value is a reference to the parameter resource name and not the actual name itself. One can see it in their generated CloudFormation template:Because the value is a reference and not the name itself (
/foo/bar
), the leading slash is not detected. This results in a malformed resource ARN at runtime, leading to the bad policy.Expected Behavior
The project role should have a policy attached that has the correct resource ARN for the string parameters without the double slash:
Current Behavior
It contains double slashes, resulting in a failed CodeBuild job:
Reproduction Steps
Create a
StringParameter
in your stack with aparameterName
that contains a forward slash:Create a CodeBuild
Project
that passes infooBar.parameterName
as thevalue
forBuildEnvironmentVariableType
when defining theProject
'senvironmentVariables
property:Possible Solution
The root cause is that when removing leading forward slashes from the parameter name, it's not taken into account that the value could be an unresolved reference to the parameter name and not the actual parameter name itself. This results in the actual parameter name never being stripped of its leading forward slash.
I'm not sure about a fix but a workaround is to pass the parameter name as a hardcoded string rather than using the
parameterName
property of aStringParameter
. While this is a straightforward workaround, I'd love to see a long-term fix since from an interface-perspective, it's awkward to not be able to reference theStringParameter
construct that I made that already encapsulates and exposes the parameter name. It's also inconsistent with the docs since it specifically says to use theparameterName
property as the value.Additional Information/Context
No response
CDK CLI Version
2.77.0
Framework Version
No response
Node.js Version
18.16.3
OS
Amazon Linux 2
Language
Typescript
Language Version
No response
Other information
No response