How to pass script to UserData field in EC2 creation on AWS Lambda?

Question:

I’m trying to pass a script in Userdata field of a new EC2 instance created by an AWS Lambda (using AWS SDK for Javascript, Node.js 6.10):

I tried several things like:
– create a string and pass the string to the UserData – not working
– create a string and encode it to base64 and pass the string to the UserData – not working
– paste base64 encoded string – not working

Could you help me understanding how to pass a script in the UserData? The AWS SDK documentation is a bit lacking.

Is it also possible to pass a script put in an S3 bucket to the UserData?

Answer:

Firstly, base64 encoding is required in your example. Although the docs state that this is done for you automatically, I always need it in my lambda functions creating ec2 instances with user data. Secondly, as of ES6, multi-line strings can make your life easier as long as you add scripts within your lambda function.

So try the following:

Leave a Reply