GitLab pipeline to upload a file to AWS S3
Create S3 bucket
- go to S3 and click Create bucket
- enter Bucket name, for example dmitri-11102022, select AWS region, for example us-east-1, note this values, leave other options as default and click Create bucket
Create IAM programmatic account:
- go to IAM
- go to Users and click Add user and type a name, for example gitlab. Select Access key - Programmatic access
- select Attach existing policies directly, search for AmazonS3FullAccess and select it
- no tags are needed at next step
- click Create user and save the Access key ID and Secret access key from the next page
Add the keys to GitLab:
- go to Settings->CICD->Variables
- add variable AWS_ACCESS_KEY_ID and uncheck Protect variable and Mask variable
- add variable AWS_SECRET_ACCESS_KEY and uncheck Protect variable and check Mask variable - to hide this variable
- add variable AWS_DEFAULT_REGION and uncheck Protect variable and Mask variable, put the value saved from Create bucket step, for example us-east-1
- add variable AWS_S3_BUCKET with value from Create bucket step (example - dmitri-11102022) and uncheck Protect variable and Mask variable
Create
.gitlab-ci.yaml in GitLab with the following contents:
deploy to s3:
image:
name: amazon/aws-cli
entrypoint: [""]
script:
- aws --version
- echo "Hello S3" > file.txt
- aws s3 cp file.txt s3://$AWS_S3_BUCKET/file.txt
Commit and wait for pipeline to execute. Now you have your file on S3.
In other cases this option also could be used:
aws sync source_dir s3://bucket_name --delete