Custom the ec2 user data when instance launch
Usually, user data scripts are only run the first time the instance is started, however this can be changed using cloud-init to run every time the instance restarts.
Create the cloud config file
1Content-Type: multipart/mixed; boundary="//"
2MIME-Version: 1.0
3
4--//
5Content-Type: text/cloud-config; charset="us-ascii"
6MIME-Version: 1.0
7Content-Transfer-Encoding: 7bit
8Content-Disposition: attachment; filename="cloud-config.txt"
9
10#cloud-config
11cloud_final_modules:
12 - [scripts-user, always]
13runcmd:
14 - [ mkdir, /test-cloudinit ]
15write_files:
16 - path: /test-cloudinit/cloud-init.txt
17 content: Created by cloud-init
18
19--//
20Content-Type: text/x-shellscript; charset="us-ascii"
21MIME-Version: 1.0
22Content-Transfer-Encoding: 7bit
23Content-Disposition: attachment; filename="userdata.txt"
24
25#!/bin/bash
26**commands here**
27--//
Encode the file content to base64
1base64 file.txt > file.b64.txt
2# windows os should use following command
3certutil -encode file.txt tmp.b64 && findstr /v /c:- tmp.b64 > file.b64.txt
4# apply to ec2 instance
5aws ec2 modify-instance-attribute \
6--instance-id=xxx \
7--attribute userData \
8--value file://file.b64.txt