How to download files from S3 in SaltStack

How to download files stored in S3 and manage it in SaltStack

SaltStack - file.managed

To manage a file using salt stack configuration we can use file.managed state. This state downloads the file from salt master server or HTTP / FTP server or it can point to local system file as well.

It also supports downloading files from S3 URL’s

Downloading files from S3

There are two things which needs to be done to download files from S3

  • Give S3 URL in source parameter of file.managed state in your salt states file
  • Configure required AWS keys in minion config so that SaltStack can use those keys to access files store in AWS S3 bucket and download it.

S3 URL in source parameter

In your state file you have to give S3 URL in source parameter of file.managed state as below.

Here replace <bucketname> with your the actual bucketname and <path-to-file> with the path of the file under bucket for example setup/demo.jar.

Let say your bucket name is myBucket then your final URL will be s3://myBucket.s3.amazonaws.com/setup/demo.jar

download_file_from_s3:
  file.managed:
    - name: /opt/file_from_s3.jar
    - source: s3://<bucketname>.s3.amazonaws.com/<path-to-file>
    - user: root
    - group: root
    - mode: 755

AWS S3 key configuration in minion config

You have to add below details in your minion config file, change keyid and key as per your account and location should match the location where you have created your AWS S3 bucket.

Refer salt.module.s3 for more available options.

# AWS access keys to download files from S3 bucket
s3.keyid: XXXXXXXXXXXXXXXXXXXXXX
s3.key: XXXXXXXXXXXXXXXXXXX
s3.location: us-east-1

This blog is open-source on Github.