How to pass variable inside the json input for aws cli command?

Hi,
I am using “RUN AN AWS CLI SCRIPT” step template to run following code

$lamdaArn = aws ssm get-parameter --name “MyStringParameter”
$lamdaArn = $lamdaArn.Parameter.Value
aws s3api put-bucket-notification-configuration --bucket #{testBucketName} --notification-configuration file://lambdaNotificationSettings.json

The Json file is as below -

{
	"LambdaFunctionConfigurations": 
		[
			{
				"LambdaFunctionArn":<USE LAMBDA ARN OCTOPUS Variable HERE>,
				"Events": 
					[
						"s3:ObjectCreated:*"
					]
			}
		]
}

As you can see I want to use Octopus variable inside the json file, how can I achieve this?

Hi @harsh_tech,

Welcome to the community!

That is a great question which will have multiple answers!

I’ve reached out to our Solutions team for some advice about the best approach to this and we’ll keep you updated!

I believe the difficulty is targeting a file on the local system, if the JSON file was in a package uploaded to the Octopus Built-in Feed, then you could leverage ‘Structured configuration variables’ or ‘Substitute variables in templates’ to achieve the variable substitution. Both of these are configurable features for the deployment process found at the bottom of the page.

Feel free to let me know if you have any questions!

Best Regards,

Thanks @finnian.dempsey for your reply, can I then add one step before this to dynamically push the json file/object with variable name in it to the octo package? but how would the local variable value be replaced in json? variable would need to populated by reading SSM Parameter store…

Hi @harsh_tech,

Finnian asked me to take a look at your request.

Before talking about changing your process, I’d like to start basic to make sure I understand all the pieces of the puzzle. Where does that .json file currently live? How does that file make it to a location for that step to access?

Best regards

Thanks @Bob_Walker for looking into this. The .json file is currently in the shared folder and I am passing the shared path in the aws cli command like below -

$testSSMParameter = aws ssm get-parameter --name “testLambdaArn”
$testLambdaArn = $testSSMParameter.Parameter.Value
aws s3api put-bucket-notification-configuration --bucket #{testBucketName} --notification-configuration file:////shared.dev01.com/notificationSettings.json

I manually created and copied that file in the shared path because I can not hold/store file in Octopus deploy(based on my knowledge).

Hi @harsh_tech,

Great, thank you for providing that additional context.

A couple of options for you.

Option 1

  1. Copy that shared file to the local directory (working directory) in a step prior to your script.
  2. Use the structure variable replacement to change the value you wish to change. The path you provide to the target files can be either relative or absolute. The default is relative.

Option 2

  1. Store the JSON content of that file as an Octopus Variable. Variables can be multi-line values. That variable will have the octostache syntax for the lambda ARN, IE #{YourLambaArnValue}
{
	"LambdaFunctionConfigurations": 
		[
			{
				"LambdaFunctionArn":#{YourLambdaArnValue},
				"Events": 
					[
						"s3:ObjectCreated:*"
					]
			}
		]
}
  1. In your script you save that variable as a file.
$lambdaArnFileContents = $OctopusParamaters["Your Variable Name"]
Set-Content -Path .\lambdaNotificationSetting.json -Value $lambdaArnFileContents

I hope that helps!

1 Like

Thanks @Bob_Walker both the solutions sounds pretty good…

Great, let me know if you have any other questions!

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.