This question is in reference to Atlassian Documentation: Variables for deployment environments
Bamboo does not parse variable content in any way, i.e. the content is always just a string. The problem you are encountering is passing a JSON string as a parameter to AWS CloudFormation via the AWS CloudFormation Stack task, which expects those parameters to be passed into the Parameters field as a JSON string in turn, e.g.:
[ { "ParameterKey": "LambdaPolicy", "ParameterValue": "${bamboo.lambda.LambdaPolicy}" } ]
When ${bamboo.lambda.LambdaPolicy}
contains just the original JSON, the resulting nested JSON is invalid and triggers the encountered MalformedJsonException
.
In order to pass a JSON string inside another JSON string in such a scenario, you need to escape it (e.g. manually via JSON Escape/Unescape).
Given your example JSON input:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "apigateway:*" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "iam:PassRole", "iam:GetServerCertificate" ], "Resource": "*" } ] }
You would need to pass the following escaped JSON string instead (i.e. as the value of ${bamboo.lambda.LambdaPolicy}
in your scenario):
{\n \"Version\": \"2012-10-17\",\n \"Statement\": [\n {\n \"Effect\": \"Allow\",\n \"Action\": [\n \"logs:CreateLogGroup\",\n \"logs:CreateLogStream\",\n \"logs:PutLogEvents\"\n ],\n \"Resource\": \"arn:aws:logs:*:*:*\"\n },\n {\n \"Effect\": \"Allow\",\n \"Action\": [\n \"apigateway:*\"\n ],\n \"Resource\": \"*\"\n },\n {\n \"Effect\": \"Allow\",\n \"Action\": [\n \"iam:PassRole\",\n \"iam:GetServerCertificate\"\n ],\n \"Resource\": \"*\"\n }\n ]\n}
This will yield the same results as passing the unescaped JSON via the AWS Management Console, where the problem does not exist due to its more advanced UI, which accepts each parameter via an individual text input field.
Here's the corresponding Bamboo log output for a variable:
Substituting variable: ${bamboo.lambda.LambdaPolicy} with { \"Version\": \"2012-10-17\", \"Statement\": [ { \"Effect\": \"Allow\", \"Action\": [ \"logs:CreateLogGroup\", \"logs:CreateLogStream\", \"logs:PutLogEvents\" ], \"Resource\": \"arn:aws:logs:*:*:*\" }, { \"Effect\": \"Allow\", \"Action\": [ \"apigateway:*\" ], \"Resource\": \"*\" }, { \"Effect\": \"Allow\", \"Action\": [ \"iam:PassRole\", \"iam:GetServerCertificate\" ], \"Resource\": \"*\" } ] }
And here's the resulting Bamboo log output after the variable has been used inside the Parameters field:
... bamboo.custom.aws.cfn.stack.resources.tst-1-TST-AWSB-CFNPJ-21-2.parameters.LambdaPolicy: { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "apigateway:*" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "iam:PassRole", "iam:GetServerCertificate" ], "Resource": "*" } ]}
In other words, the escaped JSON input properly ends up in CloudFormation as an unescaped JSON parameter.
I realize that this escaping requirement poses a notable usability issue (the Parameters field inline dialog examples have a a small hint that "Reserved JSON characters in Bamboo variable content need to be escaped at runtime", however, it had been missing in the task documentation, and either way this is not easily discovered). I've filed UAA-203 to contemplate how to improve the usability in this regard (suggestions welcome).
Initial Answer
We have outlined a more generic solution for Utoolity's Bamboo add-ons in our KB article How to supply task input from an external source like a file - here's a slightly adjusted version for your JSON use case:
You want to inject a JSON object into a Bamboo variable:
cloudformation.parameters
.${bamboo.cloudformation.parameters}
.${bamboo.cloudformation.parameters}
into a task's input field.This workaround has the limitation that the included content cannot use Bamboo variables in turn, because Bamboo doesn't yet support nested/recursive expansion of variables - see https://jira.atlassian.com/browse/BAM-8266.
Hi Stephen,
Thanks for your reply.
I think regardless of where the inputs are coming from (file, plan variables, etc.), bamboo is still parsing the input before sending it to AWS or any other tool. So the parsing is happening after the value is supplied to the parameters. So I am not sure whether this will still work. I could be wrong but I will still give it a go. Thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ramesh Ramalingam - thank you for using our add-ons.
I've just confirmed that our resp. integration test for the referenced How to still work as documented. Accordingly, the MalformedJsonException
you are encountering is hinting on this being caused by an actual JSON syntax error, specifically a missing brace ('Unterminated object') - I can think of the following possible causes:
Hope this helps - if not, could you please make the offending JSON available so that we can try to reproduce this (and of course, feel free to create a support request, if you'd prefer to take this offline)?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Steffen, thanks for your reply.
I was using the quotes (trail and error) to check whether the bamboo will accept the json as string or not. Obviously the json is correct as I have tested directly on AWS cloudformation and validated already even before writing the cloudformation. I have copied the JSON below.
Sorry I am not sure if I have explained you the problem clearly.
There is a parameter in AWS cloudformation which accepts string value. I am passing the json as string value for that parameter and AWS cloudformation accepts as string when I create the stack directly from AWS. But when I do that on bamboo, it determines the string as JSON while parsing and send it across as JSON to AWS instead of string. So I need to make sure I pass the below JSON as string to AWS.
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "apigateway:*" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "iam:PassRole", "iam:GetServerCertificate" ], "Resource": "*" } ] }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ramesh Ramalingam - thanks for clarifying, I think I'm on track now.
You have encountered an easy to trip over usability flaw with passing nested JSON objects via task fields that expect any input formatted as JSON in turn. The TL;DR is that you need to escape the nested JSON object before passing it inside the JSON task field - I've updated my answer with a more detailed description of the problem and solution.
As mentioned, I realize that this comprises a notable usability issue, sorry you had to discover it the hard way. Unfortunately I'm not quite sure how to address this with a reasonable amount of effort, insofar the Bamboo task UI doesn't offer any UI widgets out of the box that would allow to mirror the more advanced AWS Management Console UI. Regardless, I've filed UAA-203 to contemplate whether there might be any other options to improve on the current approach (suggestions welcome).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Stephen, Thanks for your time again.
I did try with unescaped JSON couple of days ago and didn't work for me.
Let me give it a go again because I was adding \ next to the quotes manually. Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Stephen, It's working after I converted it through JSON Editor Online.
Thanks for your help. Appreciate your time and effort.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ramesh Ramalingam
you can find related here JsonObject article please go through this.
might be it helps you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Yogesh. I will read through and see if I can get anything out of it. Thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.