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:

Part of the setup instructions require running this script (

set -eo pipefail
ARTIFACT_BUCKET=$(cat bucket-name.txt)
if [ $1 ]
  if [ $1 = mvn ]
    mvn package
  gradle build -i

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:

$ ./ mvn
  ... # Maven output

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.


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.