How to pass parameter to UserData in AWS template

Stanley Meng
Mar 28, 2022

For instance, you have a parameter in the template:

"EnableWebServer": {
"Default": "No",
"Description": "Enable Web Server in the Ubuntu or not",
"Type": "String"
}

You want to pass it to the Shell Scripts in UserData to run some commands based on this parameter. Here is an sample:

"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -xe\n",
"if [[ \"Yes\" == ",
{
"Ref": "EnableWebServer"
},
" ]]; then ",
"echo yes > /tmp/1.txt; ",
"else ",
"echo no > /tmp/1.txt;",
"fi\n",
]
]
}
}

--

--