[2021] How to upload a file or a folder to AWS S3 from GitLab CI using AWS CLI v2
In this short tutorial, I will quickly go over building a GitLab CI pipeline that can upload a file (an artifact) or an entire folder to AWS S3.
How to use the AWS CLI v2
For a long time, AWS did not release an official Docker image containing the AWS Command Line Interface (AWS CLI) tool. Starting with AWS CLI v2, AWS now offers an official Docker image called amazon/aws-cli.
Let’s give it a try to see if it works properly.
upload to s3:
image:
name: amazon/aws-cli
entrypoint: [""]
script:
- aws --version
We are getting the AWS CLI version in the job logs, so everything is working fine.
Getting the AWS credentials
To get everything to run, you need to have an AWS user with programmatic access to the S3 bucket you want to use.
Make sure you define the following variables in your GitLab CI project:
AWS_S3_BUCKET
— the exact name of the S3 bucket)AWS_ACCESS_KEY_ID
— provided by AWS within IAMAWS_SECRET_ACCESS_KEY
— provided by AWS within IAMAWS_DEFAULT_REGION
—…