AWS
InvalidRequest - Invalid Request
Getting an **InvalidRequest** error means your AWS API request has invalid structure or format—the request might be malformed, missing required elements, or have syntax errors. This client-side error (4xx) happens when AWS validates request structure. Most common when JSON request bodies are malformed, but also appears when request structure is invalid, required elements are missing, request format doesn't match API requirements, or request validation fails.
#Common Causes
- →Identity: IAM policy request restrictions. Service Control Policy (SCP) blocks request format.
- →Network: VPC endpoint request validation. API Gateway request format restrictions.
- →Limits: Malformed JSON request body. Invalid request structure. Missing required elements. Invalid request format. Request validation failed.
✓Solutions
- 1Step 1: Diagnose - Check exact error message: AWS usually specifies which part of request is invalid. Review request format. Check JSON syntax if using JSON.
- 2Step 2: Diagnose - Validate JSON syntax: echo REQUEST_JSON | jq '.'. Check for syntax errors. Verify request structure matches API requirements.
- 3Step 3: Diagnose - Check request structure: Verify all required elements are present. Check nested parameter structure. Validate parameter types.
- 4Step 4: Fix - Validate request format: Use AWS CLI which handles formatting automatically: aws ec2 run-instances --image-id ami-xxxxx --instance-type t2.micro. Or fix JSON syntax manually.
- 5Step 5: Fix - Review API documentation: Check AWS API Reference for exact request format. Verify request structure. Test with minimal valid request first.
</>Code Examples
Validate JSON Request Syntax
1#!/bin/bash
2# Validate JSON request body
3REQUEST_JSON='{"ImageId":"ami-xxxxx","InstanceType":"t2.micro","MinCount":1,"MaxCount":1}'
4
5echo "=== Validating JSON Request ==="
6echo "${REQUEST_JSON}"
7
8# Check JSON syntax
9if command -v jq &> /dev/null; then
10 echo "${REQUEST_JSON}" | jq '.' > /dev/null 2>&1
11 if [ $? -eq 0 ]; then
12 echo "✓ JSON syntax valid"
13 echo "${REQUEST_JSON}" | jq '.'
14 else
15 echo "✗ Invalid JSON syntax"
16 echo "Fix JSON errors before sending request"
17 fi
18else
19 echo "jq not installed - cannot validate JSON"
20 echo "Install: sudo apt-get install jq (Linux) or brew install jq (macOS)"
21fi
22
23# Better: Use AWS CLI which handles formatting
24echo "\n=== Better Approach: Use AWS CLI ==="
25echo "AWS CLI handles request formatting automatically:"
26echo "aws ec2 run-instances --image-id ami-xxxxx --instance-type t2.micro --count 1"Check Request Structure and Required Elements
1#!/bin/bash
2# Validate request has required elements
3echo "=== Checking Request Structure ==="
4
5# Example: EC2 run-instances requires ImageId and InstanceType
6REQUIRED_PARAMS=("ImageId" "InstanceType")
7
8# Check if using AWS CLI (recommended)
9echo "Using AWS CLI ensures correct request structure:"
10echo "aws ec2 run-instances \"
11echo " --image-id ami-xxxxx \"
12echo " --instance-type t2.micro \"
13echo " --count 1"
14
15# If using JSON directly, validate structure
16echo "\n=== If Using JSON Directly ==="
17echo "Ensure request has:"
18for param in "${REQUIRED_PARAMS[@]}"; do
19 echo " - ${param}"
20done
21
22# Test with minimal valid request
23echo "\n=== Testing Minimal Valid Request ==="
24aws ec2 run-instances \
25 --image-id ami-xxxxx \
26 --instance-type t2.micro \
27 --count 1 \
28 --dry-run 2>&1 | head -3Fix Invalid Request Format
1#!/bin/bash
2# Common request format issues and fixes
3
4# Issue 1: Malformed JSON
5BAD_JSON='{"ImageId":"ami-xxxxx" "InstanceType":"t2.micro"}' # Missing comma
6echo "=== Fixing Malformed JSON ==="
7echo "Bad: ${BAD_JSON}"
8GOOD_JSON='{"ImageId":"ami-xxxxx","InstanceType":"t2.micro"}'
9echo "Good: ${GOOD_JSON}"
10
11# Issue 2: Missing required elements
12echo "\n=== Adding Missing Required Elements ==="
13INCOMPLETE='{"ImageId":"ami-xxxxx"}' # Missing InstanceType
14echo "Incomplete: ${INCOMPLETE}"
15COMPLETE='{"ImageId":"ami-xxxxx","InstanceType":"t2.micro","MinCount":1,"MaxCount":1}'
16echo "Complete: ${COMPLETE}"
17
18# Best practice: Use AWS CLI
19echo "\n=== Best Practice: Use AWS CLI ==="
20echo "Instead of building JSON manually, use:"
21echo "aws ec2 run-instances \"
22echo " --image-id ami-xxxxx \"
23echo " --instance-type t2.micro \"
24echo " --count 1"↗Related Errors
Provider Information
This error code is specific to AWS services. For more information, refer to the official AWS documentation.