Digvijay Sawant
Digvijay Sawant

Reputation: 1079

Creating bash script files to create S3 bucket

I wrote these commands to create s3 bucket:

bucketname=test1234
AWS_ACCESS_KEY_ID=*** AWS_SECRET_ACCESS_KEY=*** REGION=us-east-1 aws s3 mb "s3://$bucketname"

This successfully creates a bucket. but when I copy this in a file, passing the bucket name as an argument and run the script file I get an error:

Script in the file:
bucketname=$1
AWS_ACCESS_KEY_ID=*** AWS_SECRET_ACCESS_KEY=*** REGION=us-east-1 aws s3 mb "s3://$bucketname"

Bash file : createbucket.txt Command used: ./createbucket.txt buckettest1234

    Error:

         Parameter validation failed:ettest1234
    ": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"

It even takes out the first 4 letters for some reason.

Upvotes: 1

Views: 5497

Answers (1)

MatrixManAtYrService
MatrixManAtYrService

Reputation: 9161

The error message you're getting indicates that something is happening to the bucket name before it is making it to the aws command. You're expecting buckettest1234 but you're getting something like ettest1234.

To make it easier to see why this is happening, try wrapping your secrets in single quotes, your variable references in double quotes, and initializing your variables on their own lines, and printing the intermediate value, like so:

createbucket.sh:

#! /usr/bin/env bash
AWS_ACCESS_KEY_ID='***'
AWS_SECRET_ACCESS_KEY='***'
REGION='us-east-1' 
aws s3 mb "s3://$1"
aws s3 cp "s3://frombucket/testfile.txt" "s3://$1/testfile.txt"

Upvotes: 1

Related Questions