Skip to content

Inconsisent S3 parameter validation error when running bash script in Ubuntu WSL

I’m experimenting with AWS Lambda by following along with the instructions here: https://github.com/awsdocs/aws-lambda-developer-guide/tree/main/sample-apps/java-basic.

Part of the setup instructions require running this script (2-deploy.sh):

#!/bin/bash
set -eo pipefail
ARTIFACT_BUCKET=$(cat bucket-name.txt)
TEMPLATE=template.yml
if [ $1 ]
then
  if [ $1 = mvn ]
  then
    TEMPLATE=template-mvn.yml
    mvn package
  fi
else
  gradle build -i
fi

echo $TEMPLATE #debug
echo $ARTIFACT_BUCKET #debug
aws cloudformation package --template-file $TEMPLATE --s3-bucket $ARTIFACT_BUCKET --output-template-file out.yml
aws cloudformation deploy --template-file out.yml --stack-name java-basic --capabilities CAPABILITY_NAMED_IAM

When I run the script in WSL I get a parameter validation error:

$ ./2-deploy.sh mvn
  ... # Maven output

template-mvn.yml              
lambda-artifacts-<alphanumeric string>                                    
Unable to upload artifact target/java-basic-1.0-SNAPSHOT.jar referenced by CodeUri parameter of function resource.
Parameter validation failed:                                                     "
": Bucket name must match the regex "^[a-zA-Z0-9.-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:(s3|s3-object-lambda):[a-z-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9-]{1,63}$|^arn:(aws).*:s3-outposts:[a-z-0-9]+:[0-9]{12}:outpost[/:][a-zA-Z0-9-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9-]{1,63}$"

However, when I manually run the last two lines of the file with the debug output it works correctly:

[email protected]:/mnt/c/Users/Ben/IdeaProjects/LakeStockingAlerter/sample-apps/java-basic$ aws cloudformation package --template-file template-mvn.yml --s3-bucket lambda-artifacts-<alphanumeric string>--output-template-file out.yml         
Uploading to ... 260754 / 260754.0  (100.00%)
Successfully packaged artifacts and wrote output template to file out.yml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /mnt/c/Users/Ben/IdeaProjects/LakeStockingAlerter/sample-apps/java-basic/out.yml --stack-name <YOUR STACK NAME>
[email protected]:/mnt/c/Users/Ben/IdeaProjects/LakeStockingAlerter/sample-apps/java-basic$ aws cloudformation deploy --template-file out.yml --stack-name java-basic --capabilities CAPABILITY_NAMED_IAM

Waiting for changeset to be created...
Waiting for stack create/update to complete
Successfully created/updated stack - java-basic  

Why does this fail when run as a bash script? None of the characters stored in the variables are special to bash so I don’t see an issue with the variable substitution. The script is also using Unix-style line endings.

Answer

Based on the comments.

The issue was that the bucket-name.txt file contained an extra line. Subsequently $ARTIFACT_BUCKET contained the new line character, leading to the error.