How to send UserData for an EC2 instance

aws

#1

I’m trying to use the resource section to include a CloudFormation setup for a custom EC2 configuration. Yeah, I know, it’s not serverless. Sue me, we can’t always do things the way we want to.

Question is: How do I specify the UserData script for the EC2 instance without having the serverless framework try to interpret any variable declarations in there (it’s a Bash script).

Any clues?


#2

In case anyone finds this and wants to know how, here’s what I did:

You can use the ‘UserData’ property when defining an EC2 instance in serverless.yml to send the user data. The value of that property can be a file using the ${file(myfile.txt)} syntax.

However, the contents of the file sent have to be base64 encoded because you’re using the bare cloudformation syntax, not serverless syntax.

So, that’s OK. Build a script. Let’s call it “startup.sh”. You can fill it with whatever you need.
Then, base64 encode it using your favorite command line tool into a new file, maybe called “startup.sh.base64”.

Now, in the UserData property, use this: “${file(startup.sh.base64)}” and you should get the intended result.