Need help fixing salt stack code to copy file from s3 bucket

331 views Asked by At

I wrote this code to pull file from S3 bucket, change the file permission and execute the code. However, it's not working for me.

download_file_from_s3:
  file.managed:
    - name: /opt/agent_installer.sh
    - source: s3://bucket_name/install.sh

change_file_permission:
  file.managed:
    - source: /opt/install.sh
    - user: root
    - group: root
    - mode: 0744

run_rapid7_script:
  cmd.run:
    - name: /opt/install.sh
1

There are 1 answers

1
seshadri_c On

There are a couple of changes I can suggest looking at your code.

You are saving the file from S3 as /opt/agent_installer.sh with file.managed, let's consider that there is no issue with this.

Now, the first thing that we obviously need to change in subsequent tasks, is to use this. Not /opt/install.sh. Also file.managed can be used once to download the file, change ownership, and permissions. So your SLS can look like:

download_file_from_s3:
  file.managed:
    - name: /opt/agent_installer.sh
    - source: s3://bucket_name/install.sh
    - user: root
    - group: root
    - mode: 0744

run_rapid7_script:
  cmd.run:
    - name: /opt/agent_installer.sh

There is also a cmd.script state which can be used directly with the S3 URL as source, so there is no need to have file.managed at all.

So, just 1 state like below should be sufficient:

run_rapid7_script:
  cmd.script:
    - source: s3://bucket_name/install.sh

If you do have issue with downloading file from S3, then see the documentation on how to configure it correctly.