Skip to content

Custom AWS Systems manager document sns:Publish error - Invalid parameter: Subject

0

I created the following AWS Systems manager document. When I run it, I got the "Step fails when it is Execute/Cancelling action. An error occurred (InvalidParameter) when calling the Publish operation: Invalid parameter: Subject" failure message. Why?



{
  "schemaVersion": "0.3",
  "description": "Publish SNS Notification",
  "assumeRole": "{{ AutomationAssumeRole }}",
  "parameters": {
    "TopicArn": {
      "type": "String",
      "description": "(Required) The ARN of the SNS topic to publish the notification to."
    },
    "Message": {
      "type": "String",
      "description": "(Required) The message to include in the SNS notification."
    },
    "AutomationAssumeRole": {
      "type": "String",
      "description": "(Optional) Role ARN to assume"
    },
    "Subject": {
      "type": "String",
      "description": "(Optional) The subject of the SNS notification."
    }
  },
  "mainSteps": [
    {
      "action": "aws:executeAwsApi",
      "isEnd": true,
      "inputs": {
        "Service": "sns",
        "Api": "Publish",
        "TopicArn": "{{TopicArn}}",
        "Message": "{{Message}}",
        "Subject": "{{Subject}}"
      },
      "name": "PublishSNSNotification"
    }
  ]
}

1 Answer
0

The error you're encountering is due to the way the "Subject" parameter is being handled in your AWS Systems Manager document. The issue arises because you've defined "Subject" as an optional parameter, but you're always including it in the SNS Publish API call, even when it might not be provided.

When the "Subject" parameter is not provided, the Systems Manager document tries to pass an empty or null value for the "Subject" to the SNS Publish API, which results in the "Invalid parameter: Subject" error.

To resolve this issue, you should modify your document to only include the "Subject" in the API call when it's actually provided. You can do this by using the "aws:executeAwsApi" action's ability to conditionally include parameters.

Here's how you can modify the "inputs" section of your "aws:executeAwsApi" step:

"inputs": {
    "Service": "sns",
    "Api": "Publish",
    "TopicArn": "{{TopicArn}}",
    "Message": "{{Message}}",
    "Subject": "{{ Subject | default('') | trim | length > 0 ? Subject : omit }}"
}

This modification uses a conditional expression to only include the "Subject" parameter if it has a non-empty value. If "Subject" is not provided or is an empty string, it will be omitted from the API call entirely.

By making this change, your Systems Manager document should work correctly whether or not a "Subject" is provided, avoiding the "Invalid parameter: Subject" error.
Sources
AWS Service Catalog Service Actions - AWS Service Catalog
SendCommand - AWS Systems Manager

answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.