Thursday, October 6, 2022

[SOLVED] How can I assign the raw Name of an S3 bucket to a variable and then use that variable

Issue

I'm trying to create a an S3 bucket and then use that bucket in another AWS service:

I'm doing this way:

I have a variable named $AccountAlias which I'll use that to make my buckets unique, so I'm running this:

bucketName=$(aws s3 mb s3://mytestbkt-${AccountAlias})

The bucket is created successfully, but I'm expecting that $bucketName will store the Name of the bucket, then use that variable to another service that expects the Bucket Name:

aws shield associate-drt-log-bucket --log-bucket $bucketName

After running this command I get the error:

make_bucket:: command not found    
aws: error: argument --log-bucket: expected one argument

And It makes sense, because if I run echo $bucketName I get: make_bucket: mytestbkt-testacc123

So, How can I assign just the value of the bucket name to my variable to use that in another CLI command?


Solution

Create a variable for the bucket name initially and then use it in both places:

#!/bin/bash
bucketName="mytestbkt-$AccountAlias"
aws s3 mb "s3://$bucketName"
aws shield associate-drt-log-bucket --log-bucket "$bucketName"


Answered By - Paolo
Answer Checked By - Cary Denson (WPSolving Admin)