1

Right now I hardcoded the bucket name to cp report file to that bucket but I want to create an environment variable which can pull a specific bucket from s3 bucket list and then cp my report file to the same bucket. i have read many articles but non helped me. can someone help with this?

Hardcoded buildspec.yml file

version: 0.2

phases:
  pre_build:
    commands:
      - docker-compose up -d
  build:
    commands:
      - mvn clean verify

  post_build:
    commands:
      - ls -lrt target
      - aws s3 cp target/site/report/Summary.html s3://test-reports-s3
      - docker-compose down

New buildspec.yml file i tried

version: 0.2

env:
  variables:
    S3_BUCKET: aws s3 ls | grep "my-bucket-identifier"

phases:
  pre_build:
    commands:
      - docker-compose up -d
  build:
    commands:
      - mvn clean verify

  post_build:
    commands:
      - ls -lrt target
      - aws s3 cp target/site/report/Summary.html $S3_BUCKET
      - docker-compose down

Note: I am parameterizing this because we cannot create same name of the S3 bucket in different aws account. As S3 Bucket names must be globally Unique. i have different environments example QA, Demo and Stage each will have different bucket names

  • ` aws s3 ls | grep "my-bucket-identifier"` what this is supposed to do? Your question is unclear and lacks details. – Marcin May 02 '23 at 09:41
  • All i doing here is listing all s3 buckets which contain a given string and then save the bucket name in a variable **S3_BUCKET**. i refer to this [https://stackoverflow.com/questions/67645517/how-do-i-get-the-full-name-of-an-s3-bucket-which-contains-a-given-string-using-a] – AutoMationKing May 02 '23 at 09:53
  • I want to pull my aws account s3 bucket name which starts with String "report" and then save that bucket name in the environment variable **S3_BUCKET** for later use. – AutoMationKing May 02 '23 at 09:55

1 Answers1

1

You can't have commands in variables. But you can do that in other sections,e.g.:

  post_build:
    commands:
      - ls -lrt target
      - |
        S3_BUCKET=`aws s3 ls | grep "my-bucket-identifier"`
        aws s3 cp target/site/report/Summary.html $S3_BUCKET
      - docker-compose down
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • This is answering my question. but when I tried i got the error **An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied** instead of listing buckets by string identifier. can we directly copy the file to the matching bucket? i am planning to create separate bucket names like **report_stage** **report_qa** **report_demo** if you can notice all bucket name starts with **report** – AutoMationKing May 02 '23 at 10:14
  • 1
    @AutoMationKing This is a new problem. You haven't given permissions to the codebuild to access S3. Make new question for that if you don't know how to do it. – Marcin May 02 '23 at 10:26